在android中播放m3u8视频

时间:2011-11-25 13:48:23

标签: android http-live-streaming

我希望直播视频,并采用m3u8格式。所以我尝试了下面的代码

public class StreamingPlayer extends Activity implements
OnBufferingUpdateListener, OnCompletionListener,
OnPreparedListener, OnVideoSizeChangedListener, SurfaceHolder.Callback{

    private static final String TAG = StreamingPlayer.class.getSimpleName();
    private int mVideoWidth;
    private int mVideoHeight;
    private MediaPlayer mMediaPlayer;
    private SurfaceView mPreview;
    private SurfaceHolder holder;
    private String path;

    private boolean mIsVideoSizeKnown = false;
    private boolean mIsVideoReadyToBePlayed = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mediaplayer_2);
        mPreview = (SurfaceView) findViewById(R.id.surface);
        holder = mPreview.getHolder();
        holder.addCallback(this);
        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    private void playVideo() {
        doCleanUp();
        try {

            /*
             * TODO: Set path variable to progressive streamable mp4 or
             * 3gpp format URL. Http protocol should be used.
             * Mediaplayer can only play "progressive streamable
             * contents" which basically means: 1. the movie atom has to
             * precede all the media data atoms. 2. The clip has to be
             * reasonably interleaved.
             * 
             */

            path = "httplive://xboodangx.api.channel.livestream.com/3.0/playlist.m3u8";

            if (path == "") {
                // Tell the user to provide a media file URL.
                Toast
                .makeText(
                        this,
                        "Please edit MediaPlayerDemo_Video Activity,"
                        + " and set the path variable to your media file URL.",
                        Toast.LENGTH_LONG).show();
            } 

            Log.e("PATH", "Path = " + path);
            // Create a new media player and set the listeners
            mMediaPlayer = new MediaPlayer();
            mMediaPlayer.setDataSource(path);
            mMediaPlayer.setDisplay(holder);
                    mMediaPlayer.setOnBufferingUpdateListener(this);
                    mMediaPlayer.setOnPreparedListener(this);
            mMediaPlayer.prepare();
            mMediaPlayer.setOnCompletionListener(this);
            mMediaPlayer.setOnVideoSizeChangedListener(this);
            mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

        } catch (Exception e) {
            Log.e(TAG, "error: " + e.getMessage(), e);
        }
    }

    public void onBufferingUpdate(MediaPlayer arg0, int percent) {
        Log.d(TAG, "onBufferingUpdate percent:" + percent);

    }

    public void onCompletion(MediaPlayer arg0) {
        Log.d(TAG, "onCompletion called");
    }

    public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
        Log.v(TAG, "onVideoSizeChanged called");
        if (width == 0 || height == 0) {
            Log.e(TAG, "invalid video width(" + width + ") or height(" + height + ")");
            return;
        }
        mIsVideoSizeKnown = true;
        mVideoWidth = width;
        mVideoHeight = height;
        if (mIsVideoReadyToBePlayed && mIsVideoSizeKnown) {
            startVideoPlayback();
        }
    }

    public void onPrepared(MediaPlayer mediaplayer) {
        Log.d(TAG, "onPrepared called");
        mIsVideoReadyToBePlayed = true;
        if (mIsVideoReadyToBePlayed && mIsVideoSizeKnown) {
            startVideoPlayback();
        }
    }

    public void surfaceChanged(SurfaceHolder surfaceholder, int i, int j, int k) {
        Log.d(TAG, "surfaceChanged called");

    }

    public void surfaceDestroyed(SurfaceHolder surfaceholder) {
        Log.d(TAG, "surfaceDestroyed called");
    }


    public void surfaceCreated(SurfaceHolder holder) {
        Log.d(TAG, "surfaceCreated called");
        playVideo();

    }

    @Override
    protected void onPause() {
        super.onPause();
        releaseMediaPlayer();
        doCleanUp();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        releaseMediaPlayer();
        doCleanUp();
    }

    private void releaseMediaPlayer() {
        if (mMediaPlayer != null) {
            mMediaPlayer.release();
            mMediaPlayer = null;
        }
    }

    private void doCleanUp() {
        mVideoWidth = 0;
        mVideoHeight = 0;
        mIsVideoReadyToBePlayed = false;
        mIsVideoSizeKnown = false;
    }

    private void startVideoPlayback() {
        Log.v(TAG, "startVideoPlayback");
        holder.setFixedSize(mVideoWidth, mVideoHeight);
        mMediaPlayer.start();
    }


}

在logcat中显示onBufferingUpdate percent:100但我看不到视频。

音频正在运转,但突然间它被击中了。

我试过这个视频链接http://devimages.apple.com/iphone/samples/bipbop/gear1/prog_index.m3u8它正在运行。但是我的视频链接无效,我更改了httplive://... instead of http://但没有用。

我也看到了这个答案Android video stream mms and m3u8

在上面的链接中显示视频无法播放的消息。

6 个答案:

答案 0 :(得分:15)

该视频已存在于http://www.livestream.com中。在这里有Mobile Api用于直播。

Api是:

http://www.livestream.com/userguide/index.php?title=Mobile_API#How_to_get_mobile_compatible_clips_from_a_channel.27s_library

在上面的链接中,有完整的移动兼容信息。要从频道获取rtsp链接以使用此链接

http://xproshowcasex.channel-api.livestream-api.com/2.0/getstream

替换您的频道名称而不是proshowcase。然后获得所有移动兼容网址,如iPhone,Android,Blackberry等,

使用该网址,您可以使用视频观看或媒体播放器在Android中流式传输视频。

有关详细信息,请阅读Mobile Api链接。

如果有人遇到同样的问题,我希望这个答案能帮到你。

祝你好运。

答案 1 :(得分:6)

播放视频流没问题:

videoView1.setVideoPath("http://***.net/livedemo/_definst_/stream3.stream/playlist.m3u8?wowzasessionid=773395207");
videoView1.start();

关于消息:

  

无法播放视频

也许您需要为Manifest文件添加权限:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

答案 2 :(得分:1)

我认为你应该这样做:

mMediaPlayer.setOnPreparedListener(this);

在致电prepare()

之前

答案 3 :(得分:1)

您是否尝试通过网络浏览器直接播放与原生播放器的链接? 如果您无法使用本机播放器播放,则表示您当前的Android版本不支持您的文件。 HTTP Live Streaming格式可能具有一些Android不能很好支持的特性,而它可以在IOS上播放。

答案 4 :(得分:0)

我尝试了超过6个月的m3u8视频格式并没有成功。它正在我的iPhone应用程序和本机应用程序中播放。我的流媒体服务器是Red5,它没有RTSP插件。它提供了RTMP流媒体,无法在Android中进行流式传输。我等了一年才获得支持RTSP流媒体的操作系统,但谷歌没有。我仍然使用带有Flash播放器的网络视图来传输实时视频(它没有太多清晰度)。我很遗憾地向我的客户说这个并继续搜索在Android默认播放器中播放实时流。

我认为您的视频网址可能不是RTSP。

答案 5 :(得分:0)

如何在Android中播放.M3U8流媒体

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

我见过很多人在玩.M3U8时遇到问题,这取决于用于流媒体的编解码器以及与设备的兼容性,例如我的.m3u8文件只支持屏幕为1200 x800的设备。更高。