Streaming m3u8 playlist on Android 4.0.3

2019-04-02 11:57发布

问题:

I have created a following playlist:

#EXTM3U
#EXTINF:3,File - 1
http://pilatus.d1.comp.nus.edu.sg/~a0095695/video_repo/1.mp4
#EXTINF:3,File - 2
http://pilatus.d1.comp.nus.edu.sg/~a0095695/video_repo/2.mp4
#EXTINF:-1,File - 3
http://pilatus.d1.comp.nus.edu.sg/~a0095695/video_repo/3.mp4
#EXT-X-ENDLIST

Also I am using this code to play on my Android device:

MediaController mc = new MediaController(this);
VideoView videoview = (VideoView)findViewById(R.id.myvideoview);        
mc.setMediaPlayer(videoview);
videoview.setMediaController(mc);
videoview.setVideoURI(Uri.parse("http://pilatus.d1.comp.nus.edu.sg/~a0095695/video_repo/playlist.m3u8"));
videoview.requestFocus();
videoview.start();

I want Dash streaming, so would create another set of m3u8 files on top of it adapting to the bandwidth

The problem is that I am getting error like "Cannot play the file"

What am I doing wrong?...

Thanks

回答1:

that is a HLS streaming, and Android 4.0 don't have problems with this format. toyr code are wrong, try to use :

VideoView videoview = (VideoView)findViewById(R.id.myvideoview);        
videoview.setMediaController(new MediaController(this));
videoview.setVideoURI(Uri.parse("http://pilatus.d1.comp.nus.edu.sg/~a0095695/video_repo/playlist.m3u8"));
videoview.requestFocus();
videoview.setOnPreparedListener(new OnPreparedListener() {
        public void onPrepared(MediaPlayer mp) {
            mp.start();
        }
    });


回答2:

Android's support for M3U8 playlist is limited. Only newer devices supports the playlist. Some people mentioned they've had luck with devices 2.3.x. As far as I know, this feature was made available in Android 3.0.

See the new features documentation

If you have a supported device to test with and still experience issues, try using the httplive protocol

A mp4 file should play, however.



回答3:

This is an example of how to play .M3U8 Streaming in Android, but like other programmers says is not fully supported in all the Android devices

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <VideoView
        android:id="@+id/myVideoView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

Main.java

package com.grexample.ooyalalive;

import java.net.URL;
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;

public class Main extends Activity {

    private String urlStream;
    private VideoView myVideoView;
    private URL url;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_vv);//***************
            myVideoView = (VideoView)this.findViewById(R.id.myVideoView);
            MediaController mc = new MediaController(this);
            myVideoView.setMediaController(mc);         
            urlStream = "http://jorgesys.net/i/irina_delivery@117489/master.m3u8";
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    myVideoView.setVideoURI(Uri.parse(urlStream)); 
                }
            });
    }
}

I have seen a lot of people have problems playing .M3U8, it depends on the codecs used for the streaming and compatibility with the device, for example some of my .m3u8 files are only supported in devices with screens of 1200 x800 and higher.



标签: android http