简体   繁体   English

Android SDK:媒体播放器 - 从HTTP网址加载视频流

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

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. 我有一个带有以下代码的MediaPlayerActivity:此代码基本上尝试从http url获取视频流并加载它但由于某种原因它会一直崩溃。

public class MediaPlayerActivity extends Activity {

    @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); 

        MediaPlayer mp = MediaPlayer.create(this, Uri.parse("http://stream-url.com/playlist.m3u8")); 
        mp.setDisplay(holder);
        //mp.setAudioStreamType(2); 
        try {
            //mp.prepare();
            mp.start();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }

}

video_player.xml: video_player.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
> 
<SurfaceView android:id="@+id/surface_video" 
android:layout_width="250px" 
android:layout_height="250px"> 
</SurfaceView> 
<LinearLayout 
android:orientation="horizontal" 
android:layout_height="wrap_content" 
android:layout_width="fill_parent" 
android:padding="10dip" 
> 
</LinearLayout> 
</LinearLayout> 

When I go to this activity using the following code it crashes: 当我使用以下代码进行此活动时,它会崩溃:

Intent myIntent = new Intent(HomeActivity.this, MediaPlayerActivity.class);
                    HomeActivity.this.startActivity(myIntent);

What am I doing wrong? 我究竟做错了什么?

Without logs, two suggestions: 没有日志,有两个建议:

  1. try implementing SurfaceHolder.Callback.surfaceCreated() . 尝试实现SurfaceHolder.Callback.surfaceCreated()
  2. try using MediaPlayer.create() that accepts SurfaceHolder 尝试使用接受SurfaceHolder MediaPlayer.create()

Details of (1) 细节(1)

Maybe your surface is not yet created when you call start() . 调用start()时,可能还没有创建表面。 You should use MediaPlayer.setDisplay() and MediaPlayer.start() only after surface is created. To do this, you should add override MediaPlayer.start() only after surface is created. To do this, you should add override应使用MediaPlayer.setDisplay()MediaPlayer.start() only after surface is created. To do this, you should add override MediaPlayer.start() only after surface is created. To do this, you should add override SurfaceHolder.Callback.surfaceCreated()`. MediaPlayer.start() only after surface is created. To do this, you should add override SurfaceHolder.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) 细节(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) 似乎有其他MediaPlayer.create()接受SurfaceHolder作为参数之一 - 您可以尝试它: http://developer.android.com/reference/android/media/MediaPlayer.html#create(android.content.Context, android.net.Uri, android.view.SurfaceHolder)http://developer.android.com/reference/android/media/MediaPlayer.html#create(android.content.Context, android.net.Uri, android.view.SurfaceHolder) MediaPlayer.create() SurfaceHolder http://developer.android.com/reference/android/media/MediaPlayer.html#create(android.content.Context, android.net.Uri, android.view.SurfaceHolder)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM