安卓播放视频MediaPlayer


示例代码中包括了两个实现视频播放的方法,13-15行是用了一个开源框架,非常流弊,开源库地址:https://github.com/lipangit/JieCaoVideoPlayer

开源库中也是用的MediaPlayer实现播放的,不过他整合了暂停继续,音量调节,进度调节,全屏等等很多强大的功能,这些功能自己开发起来会非常花费时间。

另一个就是用MediaPlayer实现的,只实现了个基本的播放,了解大概实现就行了,使用建议还是不重复造轮子,用节操那个开源库吧。


tips

一:MediaPlayer调用start播放前要异步线程调用prepare方法准备先,系统封装了prepareAsync这个方法,本身就是异步的,但是要增加个监听器setOnPreparedListener,在监听器中的onPrepared方法中调用start开始播放。

二:上一点调用start播放后,只有声音,并没有画面。。。。官方文档中也没有提到怎么显示画面,很尴尬的找了好久,需要给MediaPlayer设置显示画面的控件,setSurface,布局文件xml控件是TextureView,另外,TextureView未初始化完成或者未attached到window就设置到MediaPlayer中是会报错的,所以也是设置一个监听器,监听控件可用后再操作。textureView.setSurfaceTextureListener(this);


public class MainActivity extends AppCompatActivity implements MediaPlayer.OnPreparedListener, TextureView.SurfaceTextureListener {
    MediaPlayer mediaPlayer = new MediaPlayer();
    TextureView textureView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textureView = (TextureView) findViewById(R.id.textureView);
        textureView.setSurfaceTextureListener(this);

        JCVideoPlayerStandard jcVideoPlayerStandard = (JCVideoPlayerStandard) findViewById(R.id.videoplayer);
        jcVideoPlayerStandard.setUp("http://2449.vod.myqcloud.com/2449_22ca37a6ea9011e5acaaf51d105342e3.f20.mp4"
                , JCVideoPlayerStandard.SCREEN_LAYOUT_NORMAL, "嫂子闭眼睛");

    }

    @Override
    public void onBackPressed() {
        if (JCVideoPlayer.backPress()) {
            return;
        }
        super.onBackPressed();
    }

    @Override
    protected void onPause() {
        super.onPause();
        JCVideoPlayer.releaseAllVideos();
    }

    public void setMediaPlayerData(){
        mediaPlayer.setOnPreparedListener(this);
        try {
            mediaPlayer.setDataSource(this, Uri.parse("http://2449.vod.myqcloud.com/2449_22ca37a6ea9011e5acaaf51d105342e3.f20.mp4"));
            mediaPlayer.prepareAsync();
            mediaPlayer.setSurface(new Surface(textureView.getSurfaceTexture()));
        } catch (IOException e) {
        }
    }

    @Override
    public void onPrepared(MediaPlayer mediaPlayer) {
        mediaPlayer.start();
    }

    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
        setMediaPlayerData();
    }

    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
    }

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
        return false;
    }

    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture surface) {
    }
}


布局文件:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <fm.jiecao.jcvideoplayer_lib.JCVideoPlayerStandard
        android:id="@+id/videoplayer"
        android:layout_width="match_parent"
        android:layout_height="200dp"/>
    <TextureView
        android:id="@+id/textureView"
        android:layout_width="match_parent"
        android:layout_height="200dp" />
</LinearLayout>


展开阅读全文

没有更多推荐了,返回首页