Android SDK中:媒体播放器 - 从HTTP URL加载视频流 [英] Android SDK: Media Player - Load video stream from HTTP url

查看:261
本文介绍了Android SDK中:媒体播放器 - 从HTTP URL加载视频流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下code一MediaPlayerActivity: 这code基本上是试图从一个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.

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> 

当我去这个活动使用下面的code它崩溃了:

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. 尝试实施 SurfaceHolder.Callback.surfaceCreated()
  2. 尝试使用 MediaPlayer.create()接受 SurfaceHolder
  1. try implementing SurfaceHolder.Callback.surfaceCreated().
  2. try using MediaPlayer.create() that accepts SurfaceHolder

也许当你调用启动面尚未创建()。您应该使用 MediaPlayer.setDisplay()并创建面 MediaPlayer.start()之后。要做到这一点,你应该增加覆盖 SurfaceHolder.Callback.surfaceCreated()`。例如,你的code可能看起来像这样。

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();
        }
    }
}

(2)

详细

似乎有其他 MediaPlayer.create()接受 SurfaceHolder 作为一个参数 - 你可以尝试一下: <$c$c>http://developer.android.com/reference/android/media/MediaPlayer.html#create(android.content.Context, android.net.Uri,android.view.SurfaceHolder)

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)

这篇关于Android SDK中:媒体播放器 - 从HTTP URL加载视频流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆