美文网首页
ExoPlayer的简单使用

ExoPlayer的简单使用

作者: 放羊娃华振 | 来源:发表于2021-03-04 11:44 被阅读0次

    一、概述

    ExoPlayer谷歌源码地址:https://github.com/google/ExoPlayer;ExoPlayer是构建在Android低水平媒体API之上的一个应用层媒体播放器。和Android内置的媒体播放器相比,ExoPlayer有许多优点。ExoPlayer支持内置的媒体播放器支持的所有格式外加自适应格式DASH和SmoothStreaming。ExoPlayer可以被高度定制和扩展以适应不同的使用场景。

    二、使用

    1.添加依赖

    添加完整依赖

    implementation 'com.google.android.exoplayer:exoplayer:2.X.X'
    

    也可以根据需求添加依赖

    implementation 'com.google.android.exoplayer:exoplayer-core:2.X.X'
    implementation 'com.google.android.exoplayer:exoplayer-dash:2.X.X'
    implementation 'com.google.android.exoplayer:exoplayer-ui:2.X.X'
    

    整个ExoPlayer库包括5个子库,依赖了整个ExoPlayer库和依赖5个子库是等效的。
    exoplayer-core:核心功能 (必要)
    exoplayer-dash:支持DASH内容
    exoplayer-hls:支持HLS内容
    exoplayer-smoothstreaming:支持SmoothStreaming内容
    exoplayer-ui:用于ExoPlayer的UI组件和相关的资源。

    2.Xml中添加组件
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity">
    
        <com.google.android.exoplayer2.ui.PlayerView
            android:id="@+id/playerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    
    </LinearLayout>
    
    3.Java中实现播放
    public class MainActivity extends AppCompatActivity {
    
        private PlayerView playerView;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            //找到播放组件
            playerView = findViewById(R.id.playerView);
            //设置播放器
            SimpleExoPlayer player = new SimpleExoPlayer.Builder(this).build();
            playerView.setPlayer(player);
    
            //播放视频
            String videoUri = "https://v-cdn.zjol.com.cn/276982.mp4";
            // Build the media item.
            MediaItem mediaItem = MediaItem.fromUri(videoUri);
            // Set the media item to be played.
            player.setMediaItem(mediaItem);
            // Prepare the player.
            player.prepare();
            // Start the playback.
            player.play();
        }
    }
    

    三、总结

    这里就简单的介绍下Exoplayer的简单使用,其余的一些高级用法,有空再整理下,有兴趣的也可以自己看看文档:https://exoplayer.dev/hello-world.html。demo仓库地址:https://github.com/stormdzh/TestExopLayer

    参考

    文档:https://exoplayer.dev/hello-world.html
    测试视频地址:
    https://v-cdn.zjol.com.cn/280443.mp4
    https://v-cdn.zjol.com.cn/276982.mp4
    https://v-cdn.zjol.com.cn/276984.mp4
    https://v-cdn.zjol.com.cn/276985.mp4
    http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4
    https://media.w3.org/2010/05/sintel/trailer.mp4

    相关文章

      网友评论

          本文标题:ExoPlayer的简单使用

          本文链接:https://www.haomeiwen.com/subject/rgbkgctx.html