3

I have downloaded m3u8 and .ts files in internal directory of my app and when i try to play m3u8 file using Nanohttpd it shows a dialog "Can't play this Video". I have added my code below. Is it possible to play from app storage or do I need to store in files in storage which can be accessed by other apps.

public class VideoPlayerActivity extends Activity {

    @BindView(R.id.video_view)
    VideoView videoView;
    private MediaController mediaController;
    String url = "";
    Server server;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_player);
        ButterKnife.bind(this);
        mediaController = new MediaController(this);
        videoView.setMediaController(mediaController);
        url = getIntent().getStringExtra("url");
        Uri uri = Uri.parse(url);
        server = new Server();
        try {
            server.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
        videoView.setVideoURI("http://localhost:8080");
        videoView.requestFocus();
        videoView.start();
        videoView.setZOrderOnTop(true);


   }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(server!= null) {
            server.stop();
        }
    }

    private class Server extends NanoHTTPD {

        public Server() {
            super(8080);
        }

        @Override
        public Response serve(IHTTPSession session) {
            String answer = "";
            FileInputStream fileInputStream = null;
            try {
                // Open file from SD Card
                FileReader index = new FileReader( url);//where url is path of m3u8 file in app storage
                BufferedReader reader = new BufferedReader(index);
                String line = "";
                while ((line = reader.readLine()) != null) {
                    answer += line;
                }
                reader.close();

            } catch (IOException ioe) {
                Log.w("Httpd", ioe.toString());
            }

            return new Response(Response.Status.OK, "application/x-mpegURL", answer);
        }
    }
}
Deepak John
  • 967
  • 1
  • 7
  • 19

1 Answers1

0

I've found similar issue 2 month ago.
As I've found after a month of experiments - NanoHTTPD local server does not work properly with files from internal storage (e.g. context.getFilesDir()). Instead, you need to store your files inside Environment.getExternalStorageDirectory() and provide read/write permissions for your app.
I've tried Android MediaPlayer and ViaccessOrca player (the last one internally uses NanoHTTPD) to play offline video from localhost.

Darkmike
  • 163
  • 2
  • 9