Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Android video issues when loading a Samba video file from router

  1. #1
    Junior Member
    Join Date
    Mar 2019
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Android video issues when loading a Samba video file from router

    Hey all I have the following Java code I did in Android Studio that I am needing some help with.

    I am needing to be able to load a shared file from my router (It's running OpenWRT/Luci) with the Samba file sharing turned on. I have tested the code out below to make sure it's finding the file and I can confirm It can find it when it checks for SMB file found? and it comes back true.

    When loading local file:


    However, the issue is that when I switch over to getting the SMB video it does not load like the other tests that I did - loading it up from local storage.

    When loading from SMB file:


    My full code that I am currently using:
        package com.example.smbvidoverhttp;
     
        import android.content.Context;
        import android.net.Uri;
        import android.os.Bundle;
        import android.util.Log;
        import android.view.View;
        import android.widget.Button;
        import android.widget.MediaController;
        import android.widget.VideoView;
     
        import androidx.annotation.Nullable;
        import androidx.appcompat.app.AppCompatActivity;
     
        import java.io.ByteArrayInputStream;
        import java.io.File;
        import java.io.FileInputStream;
        import java.io.FileNotFoundException;
        import java.io.IOException;
        import java.io.InputStream;
        import java.io.RandomAccessFile;
        import java.lang.reflect.InvocationTargetException;
        import java.lang.reflect.Method;
        import java.net.MalformedURLException;
        import java.util.HashMap;
        import java.util.Map;
     
        import fi.iki.elonen.NanoHTTPD;
        import jcifs.smb.NtlmPasswordAuthentication;
        import jcifs.smb.SmbAuthException;
        import jcifs.smb.SmbFile;
        import jcifs.smb.SmbFileInputStream;
        import jcifs.smb.SmbFileOutputStream;
     
        public class MainActivity2 extends AppCompatActivity {
            Button btn;
            Button btn2;
            VideoView videoView;
     
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
     
                btn = findViewById(R.id.button);
                btn2 = findViewById(R.id.button2);
                videoView = findViewById(R.id.videoView);
                MediaController mediaController = new MediaController(this);
                videoView.setMediaController(mediaController);
     
                btn.setOnClickListener(v -> {
                    try {
                        VideoServer blah = new VideoServer();
                        blah.start();
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                });
     
                btn2.setOnClickListener(v -> {
                    try {
                        videoView.setVideoURI(Uri.parse("http://127.0.0.1:7777/bun33s.mp4"));
                        //videoView.requestFocus();
                        videoView.start();
                    } catch (Exception e) {
                        Log.d("", "ERROR: " + e.getMessage());
                    }
                });
            }
     
            @Nullable
            @SuppressWarnings("JavaReflectionMemberAccess")
            public String findSDCardPath() {
                try {
                    final Class<?> surfaceControlClass = Class.forName("android.os.storage.StorageManager");
                    Object StorageServ = getApplicationContext().getSystemService(Context.STORAGE_SERVICE);
     
                    Method method1 = surfaceControlClass.getMethod("getVolumePaths");
                    method1.setAccessible(true);
                    String[] volumes = (String[]) method1.invoke(StorageServ);
     
                    if (volumes.length > 1) {
                        Log.d("smbTest", "External: " + volumes[1]);
                        return volumes[1];
                    } else {
                        Log.d("smbTest", "Internal: " + volumes[0]);
                        return volumes[0];
                    }
                } catch (ClassNotFoundException | InvocationTargetException | NoSuchMethodException |
                         IllegalAccessException ex) {
                    Log.d("smbTest", "ERROR: " + ex.getMessage());
                }
     
                return null;
            }
     
            public class VideoServer extends NanoHTTPD {
                String filePath = "";
     
                public VideoServer() {
                    super(7777);
                }
     
                @Override
                public Response serve(IHTTPSession session) {
                    final String theVidName = ((HTTPSession) session).getUri(); // /bun33s.mp4  
                    String range = null;
                    Map<String, String> headers = session.getHeaders();
     
                    for (String key : headers.keySet()) {
                        if ("range".equals(key)) {
                            range = headers.get(key);
                            break;
                        }
                    }
                    if (range != null) {
                        try {
                            return getPartialResponse("video/mp4", range);
                        } catch (IOException e) {
                            throw new RuntimeException(e);
                        }
                    } else {
                        return responseVideoStream(session, filePath);
                    }
                }
     
                public Response responseVideoStream(IHTTPSession session, String videopath) {
                    try {
                        NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(null, "user here", "password here");
                        SmbFile source = new SmbFile("smb://192.168.8.1/GL-Samba/bun33s.mp4", auth);
                        SmbFileInputStream inputStream = new SmbFileInputStream(source);
                        boolean b;
     
                        try {
                            b = source.exists();
                            Log.d("", "SMB file found? " + b);
                        }
                        catch(SmbAuthException e) {
                            Log.d("", "Exception caught while calling SmbFile#exists(): " + e.getMessage());
                        }
                        //InputStream in = source.getInputStream();    
                        //FileInputStream fis = new FileInputStream(videopath);
     
                        return newChunkedResponse(Response.Status.OK, "video/mp4", inputStream); 
                    } catch (MalformedURLException e) {
                        throw new RuntimeException(e);
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
     
                private Response getPartialResponse(String mimeType, String rangeHeader) throws IOException {
                    File file = new File(filePath);
                    String rangeValue = rangeHeader.trim().substring("bytes=".length());
                    long fileLength = file.length();
                    long start, end;
     
                    if (rangeValue.startsWith("-")) {
                        end = fileLength - 1;
                        start = fileLength - 1 - Long.parseLong(rangeValue.substring("-".length()));
                    } else {
                        String[] range = rangeValue.split("-");
                        start = Long.parseLong(range[0]);
                        end = range.length > 1 ? Long.parseLong(range[1]) : fileLength - 1;
                    }
     
                    if (end > fileLength - 1) {
                        end = fileLength - 1;
                    }
     
                    if (start <= end) {
                        long contentLength = end - start + 1;
                        FileInputStream fileInputStream = new FileInputStream(file);
     
                        fileInputStream.skip(start);
                        Response response = newFixedLengthResponse(Response.Status.PARTIAL_CONTENT, mimeType, fileInputStream, contentLength);
                        response.addHeader("Content-Length", contentLength + "");
                        response.addHeader("Content-Range", "bytes " + start + "-" + end + "/" + fileLength);
                        response.addHeader("Content-Type", mimeType);
     
                        return response;
                    } else {
                        return newChunkedResponse(Response.Status.INTERNAL_ERROR, "text/html", null);
                    }
                }
            }
        }

    For testing it out I'm just using the ADB with the correct device number to send a http request to the built in browser:

    adb -s 25131HFDD6222C shell am start -a android.intent.action.VIEW -d http://127.0.0.1:7777/bun33s.mp4
    And like I said, it plays when I test it out loading a local file on the device but wont play when using the SMB file.

    What am I missing?

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Android video issues when loading a Samba video file from router

    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jan 2024
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Android video issues when loading a Samba video file from router

    It seems that you are having difficulties receiving SMB video. The problem may be due to differences in the boot mechanism between the local computer and the SMB storage. Check that the paths and settings for accessing SMB resources are correct. I had a similar problem when I wanted to remove the background from a video, it’s good that later I found services depositphots.com, which, using AI tools, was able to remove the background without any problem. If these tips don't help you, you can still consider using debugging tools such as inference and logging to identify specific points of failure. If you have the code, please provide it for more detailed analysis.

  4. #4

    Default Re: Android video issues when loading a Samba video file from router

    Soya de-oiled cake emerges as a valuable byproduct in the extraction of soya oil, contributing to the sustainable utilization of soybeans. After the meticulous process of oil extraction, what remains is a protein-rich cake with reduced oil content. This residual cake becomes a versatile ingredient, finding applications in animal feed, agriculture, and various industrial uses. Packed with essential amino acids and proteins, soya de oiled cake serves as a nutritional supplement for livestock, enhancing their growth and health. In agriculture, it acts as a potent organic fertilizer, enriching the soil with nitrogen and promoting plant growth. Its significance extends to industrial sectors where it becomes a component in the formulation of diverse products. Soya de-oiled cake, with its nutrient density and multifaceted applications, exemplifies the sustainable and holistic approach to soybean processing.

Similar Threads

  1. video file
    By deependeroracle in forum AWT / Java Swing
    Replies: 1
    Last Post: April 13th, 2013, 01:54 PM
  2. Replies: 0
    Last Post: November 16th, 2012, 05:25 AM
  3. Upload live android webcam video to RTP/RTSP Server
    By Pantu in forum Android Development
    Replies: 0
    Last Post: November 1st, 2012, 05:11 AM
  4. Best way to stream video between two android devices
    By Pillar in forum Java Networking
    Replies: 0
    Last Post: September 22nd, 2012, 04:27 PM
  5. Replies: 2
    Last Post: March 16th, 2011, 06:32 AM