exoplayer如何播放资源raw和assets目录中的视频文件

其实与播放网络资源是一致的, 这是url的格式有点不同, 特此记录一下.

先生成正常的播放代码: 写的比较随意, 大体就是这个意思


lateinit var player: SimpleExoPlayer

 private fun initPlayer() {

        val bandwidthMeter = DefaultBandwidthMeter()
        val videoTrackSelectionFactory = AdaptiveTrackSelection.Factory(bandwidthMeter)
        var mTrackSelector = DefaultTrackSelector(videoTrackSelectionFactory)

        player = ExoPlayerFactory.newSimpleInstance(DefaultRenderersFactory(this),
                mTrackSelector)

        player_view.visibility = View.VISIBLE
        player_view.player = player
        player.addListener(listener)
        player.repeatMode = Player.REPEAT_MODE_ALL
        player.playWhenReady = true

        var videoUrl = videoPlayUrl


        var mVideoSource = ExtractorMediaSource.Factory(DefaultDataSourceFactory(this, "spx")).createMediaSource(Uri.parse(videoUrl)!!)
        player.prepare(mVideoSource)

    }

好了重点在这里:
如果是在assets目录下的视频文件, 需要这样写:

var videoPlayUrl =  "asset:///video.mp4"

如果是在raw目录下的视频文件, 需要这样写:

var videoPlayUrl =  "rawresource:///"+R.raw.video

你可能感兴趣的:(exoplayer如何播放资源raw和assets目录中的视频文件)