云音乐-精选视频

昨天没事又扫了一下,发现NeteaseCloudMusicApi有更新接口,还是视频的

精选视频添加起来还是很不错的,惊喜一,因为都是使用Kotlin已经不再使用Rxjava2了,在寻找定时触发替代品时发现了ticker,惊喜二,有钱就是了不起,播放在线不卡。。。我自己搭过阿里视频服务器4M宽带,播放时卡成狗

预览

netease_mlog
9-7c399480-dc43-11eb-9085-629d78835e8f.gif)

实现设计

框架

精选视频的的实现是通过ViewPager2+SimpleExoPlayer实现的

ViewPager2修改为纵向滑动

1
2
3
4
5
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" />

播放视频

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
private fun prepareVideoPlayer() {
simplePlayer = SimpleExoPlayer.Builder(requireContext()).build()
binding.playerView.player = simplePlayer

val upstreamFactory = DefaultDataSourceFactory(requireContext(), HttpHeaders.USER_AGENT)
cacheDataSourceFactory = CacheDataSource.Factory().apply {
setCache(NeteaseApplication.simpleCache)
setUpstreamDataSourceFactory(upstreamFactory)
setFlags(
CacheDataSource.FLAG_BLOCK_ON_CACHE or
CacheDataSource.FLAG_IGNORE_CACHE_ON_ERROR
)
}
}

private fun prepareMedia(linkUrl: String) {
val uri = Uri.parse(linkUrl)

val mediaSource = ProgressiveMediaSource.Factory(cacheDataSourceFactory).createMediaSource(
MediaItem.Builder().setUri(uri).setMimeType(MimeTypes.VIDEO_MP4).build()
)

simplePlayer.setMediaSource(mediaSource, true)
simplePlayer.repeatMode = Player.REPEAT_MODE_ONE
simplePlayer.playWhenReady = true
simplePlayer.prepare()
simplePlayer.addListener(playerCallback)

}

Slider

进度条更新采用ticker实现,很不错的方式,个人比较喜欢

  • 初始化

    1
    2
    3
    4
    5
    6
    7
      @ObsoleteCoroutinesApi
    private val tickerChannel = ticker(delayMillis = 300, initialDelayMillis = 0)

    // @ObsoleteCoroutinesApi
    // private fun tickCancel() {
    // tickerChannel.cancel()
    // }
  • 调用

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    lifecycleScope.launch {
    for (event in tickerChannel) {
    if (simplePlayer.playWhenReady) {
    val position = simplePlayer.currentPosition * 1f
    if (progress.valueTo > position)
    progress.value = position
    }
    }

    }

参考

https://stackoverflow.com/questions/54827455/how-to-implement-timer-with-kotlin-coroutines/54828055
https://codelabs.developers.google.com/codelabs/exoplayer-intro

文章作者: 二十I邊界
文章链接: https://xuie0000.com/post/2021-06-10-netease-09-mlog.html
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 二十I邊界