Android SDK: Media Player - Load video stream from HTTP url

后端 未结 1 358
耶瑟儿~
耶瑟儿~ 2021-01-13 21:13

I have a MediaPlayerActivity with the following code: This code basically tries to get a video stream from a http url and load it but for some reason it keeps crashing.

相关标签:
1条回答
  • 2021-01-13 21:59

    Without logs, two suggestions:

    1. try implementing SurfaceHolder.Callback.surfaceCreated().
    2. try using MediaPlayer.create() that accepts SurfaceHolder

    Details of (1)

    Maybe your surface is not yet created when you call start(). You should use MediaPlayer.setDisplay() and MediaPlayer.start() only after surface is created. To do this, you should add overrideSurfaceHolder.Callback.surfaceCreated()`. For example, your code could look like this.

    public class MediaPlayerActivity extends Activity implements SurfaceHolder.Callback {
        MediaPlayer mp; 
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            this.setContentView(R.layout.video_player);
            SurfaceView v = (SurfaceView) findViewById(R.id.surface_video); 
            SurfaceHolder holder = v.getHolder(); 
            holder.setFixedSize(400,300);
            holder.addCallback(this). 
    
            mp = MediaPlayer.create(this, Uri.parse("http://stream-url.com/playlist.m3u8")); 
    
        @Override
        public void surfaceCreated(SurfaceHolder holder) {
    
            mp.setDisplay(holder); 
            try {
                mp.start();
            } catch (IllegalStateException e) {
                e.printStackTrace();
            }
        }
    }
    

    Details of (2)

    There seems to be other MediaPlayer.create() that accepts SurfaceHolder as one of the arguments - you could try it: http://developer.android.com/reference/android/media/MediaPlayer.html#create(android.content.Context, android.net.Uri, android.view.SurfaceHolder)

    0 讨论(0)
提交回复
热议问题