Android Exoplayer-用于不同视频质量的不同URL并手动更改

时间:2019-07-04 09:19:42

标签: android video-streaming exoplayer2.x videoquality

我在应用程序中使用exoplayer。我正在用它来播放URL中的视频。我想做的是,对于同一视频,我有三个不同的高,中,低质量URL,并且我希望用户可以手动更改视频质量。

{
     "lowQualityUrl":"string url",
     "mediumQualityUrl":"string url",
     "highQualityUrl":"string url"
}

在JWplayer中,有一个选项可以为不同的质量添加不同的源/ URL。在exoplayer中是否可以做类似的事情??

编辑:我不想一个接一个地播放视频。我只想切换到同一视频的不同质量,例如在youtube中。但是我没有为源使用单个URL,而是为同一视频的3种质量(低,中,高)使用 3个不同的URL

1 个答案:

答案 0 :(得分:0)

我找到了解决该问题的方法,或者说是一种解决方法。我在recylcerview内部使用exoplayer。这段代码可能需要一些优化。

从另一个答案中得到了这个想法,该答案位于该问题下,this github link,但是我认为作者删除了它。 我所做的是创建一个用于保留特定视频的所有网址的类。然后在exoplayer上方显示一个Spinner,然后当用户选择特定质量时,我准备带有新URL的exoplayer,然后搜寻到以前的播放位置。您可以忽略StringUtils方法。

VideoPlayerConfig.kt

object VideoPlayerConfig {
//Minimum Video you want to buffer while Playing
val MIN_BUFFER_DURATION = 3000
//Max Video you want to buffer during PlayBack
val MAX_BUFFER_DURATION = 5000
//Min Video you want to buffer before start Playing it
val MIN_PLAYBACK_START_BUFFER = 1500
//Min video You want to buffer when user resumes video
val MIN_PLAYBACK_RESUME_BUFFER = 5000
} 

VideoQuality.kt

您可以根据需要更改此类。我需要为低,中和高质量网址分别存储3个网址。而且我还需要在微调器中按顺序显示它们。

class VideoQuality {
private val videoQualityUrls = HashMap<String, String>()

companion object {
    val LOW = getStringResource(R.string.low)
    val MEDIUM = getStringResource(R.string.medium)
    val HIGH = getStringResource(R.string.high)
}

val qualityArray
    get() = arrayListOf<String>().apply {
        if (hasQuality(LOW)) add(LOW)
        if (hasQuality(MEDIUM)) add(MEDIUM)
        if (hasQuality(HIGH)) add(HIGH)
    }
var defaultVideoQuality: String? = HIGH

var lowQuality: String?
    set(value) {
        setVideoQualityUrl(LOW, value)
    }
    get() = videoQualityUrls[LOW] ?: ""
var mediumQuality: String?
    set(value) {
        setVideoQualityUrl(MEDIUM, value)
    }
    get() = videoQualityUrls[MEDIUM] ?: ""
var highQuality: String?
    set(value) {
        setVideoQualityUrl(HIGH, value)
    }
    get() = videoQualityUrls[HIGH] ?: ""

private fun setVideoQualityUrl(quality: String?, url: String?) {
    if (url != null && quality != null) {
        videoQualityUrls[quality] = url
    }
}

private fun hasQuality(quality: String?): Boolean {
    if (videoQualityUrls[quality] == null) {
        return false
    }
    return true
}

fun getVideoQualityUrl(quality: String?): String? {
    return videoQualityUrls[quality]
}
}

用于exoplayer的方法

private fun initializePlayer() {
    if (exoPlayer == null) {

        val loadControl = DefaultLoadControl.Builder()
            .setBufferDurationsMs(2 * VideoPlayerConfig.MIN_BUFFER_DURATION, 2 * VideoPlayerConfig.MAX_BUFFER_DURATION, VideoPlayerConfig.MIN_PLAYBACK_START_BUFFER, VideoPlayerConfig.MIN_PLAYBACK_RESUME_BUFFER)
            .createDefaultLoadControl()
        //Create a default TrackSelector
        val videoTrackSelectionFactory = AdaptiveTrackSelection.Factory()
        val trackSelector = DefaultTrackSelector(videoTrackSelectionFactory)
        exoPlayer = ExoPlayerFactory.newSimpleInstance(itemView.context, DefaultRenderersFactory(itemView.context), trackSelector, loadControl)
        exoPlayer!!.addListener(PlayEventListener())
        val videoQualityInfo:VideoQuality = videoListVideoDataHolderData!!.videoQualityUrls //Just an object that i created and stored in a dataHolder for this view.
        val url = videoQualityInfo.getVideoQualityUrl(videoQualityInfo.defaultVideoQuality) ?: ""
        preparePlayer(url)
    }
}

private fun preparePlayer(url: String) {

    if (url.isNotEmpty()) {
        val mediaSource = buildMediaSource(StringUtils.makeHttpUrl(url))
        exoPlayer?.prepare(mediaSource)
        videoView.player = exoPlayer
    } else {
        Log.d(APPTAG, "NO DEFAULT URL")
    }
}
private fun buildMediaSource(url: String): ProgressiveMediaSource {
    val mUri: Uri = Uri.parse(url)
    val dataSourceFactory = DefaultDataSourceFactory(
        itemView.context,
        Util.getUserAgent(itemView.context, getStringResource(R.string.app_name))
    )
    val videoSource = ProgressiveMediaSource.Factory(dataSourceFactory)
        .createMediaSource(mUri)
    return videoSource
}

然后在Spinner / QualitySelector的OnItemSelectedListener中

videoQualitySpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
        override fun onNothingSelected(parent: AdapterView<*>?) {

        }

        override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
            val currentTime = exoPlayer?.currentPosition
            val isReadyToPlay = exoPlayer?.playWhenReady
            val urlToBuild = when (videoQualityUrls.qualityArray[position]) {
                VideoQuality.LOW -> videoQualityUrls.lowQuality
                VideoQuality.MEDIUM -> videoQualityUrls.mediumQuality
                else -> videoQualityUrls.highQuality
            }
            Log.d(APPTAG, "VIDEO DETAILS :::: ${currentTime} ${isReadyToPlay} ${urlToBuild}")
            if (!urlToBuild.isNullOrEmpty()) {
                val mediaSource = buildMediaSource(StringUtils.makeHttpUrl(urlToBuild))
                exoPlayer?.prepare(mediaSource)
                exoPlayer?.playWhenReady = isReadyToPlay ?: false
                exoPlayer?.seekTo(currentTime ?: 0)
            }

        }
    }