首页 > 解决方案 > 如何在播放时下载视频,使用 ExoPlayer?

问题描述

背景

我正在开发一个可以播放一些短视频的应用程序。

我想避免每次用户玩它们时都访问互联网,以使其更快并降低数据使用量。

问题

目前我只找到了如何播放或下载(它只是一个文件,所以我可以像下载任何其他文件一样下载它)。

这是从 URL 播放视频文件的代码(此处提供示例):

毕业典礼

...
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.android.exoplayer:exoplayer-core:2.8.4'
implementation 'com.google.android.exoplayer:exoplayer-ui:2.8.4'
...

显现

<manifest package="com.example.user.myapplication" xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools">

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

    <application
        android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"
        tools:ignore="AllowBackup,GoogleAppIndexingWarning">
        <activity
            android:name=".MainActivity" android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>

</manifest>

activity_main.xml

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" tools:context=".MainActivity">

    <com.google.android.exoplayer2.ui.PlayerView
        android:id="@+id/playerView" android:layout_width="match_parent" android:layout_height="match_parent"
        app:resize_mode="zoom"/>
</FrameLayout>

MainActivity.kt

class MainActivity : AppCompatActivity() {
    private var player: SimpleExoPlayer? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }

    override fun onStart() {
        super.onStart()
        playVideo()
    }

    private fun playVideo() {
        player = ExoPlayerFactory.newSimpleInstance(this@MainActivity, DefaultTrackSelector())
        playerView.player = player
        player!!.addVideoListener(object : VideoListener {
            override fun onVideoSizeChanged(width: Int, height: Int, unappliedRotationDegrees: Int, pixelWidthHeightRatio: Float) {
            }

            override fun onRenderedFirstFrame() {
                Log.d("appLog", "onRenderedFirstFrame")
            }
        })
        player!!.addListener(object : PlayerEventListener() {
            override fun onPlayerStateChanged(playWhenReady: Boolean, playbackState: Int) {
                super.onPlayerStateChanged(playWhenReady, playbackState)
                when (playbackState) {
                    Player.STATE_READY -> Log.d("appLog", "STATE_READY")
                    Player.STATE_BUFFERING -> Log.d("appLog", "STATE_BUFFERING")
                    Player.STATE_IDLE -> Log.d("appLog", "STATE_IDLE")
                    Player.STATE_ENDED -> Log.d("appLog", "STATE_ENDED")
                }
            }
        })
        player!!.volume = 0f
        player!!.playWhenReady = true
        player!!.repeatMode = Player.REPEAT_MODE_ALL
        player!!.playVideoFromUrl(this@MainActivity, "https://sample-videos.com/video123/mkv/720/big_buck_bunny_720p_1mb.mkv")
    }

    override fun onStop() {
        super.onStop()
        playerView.player = null
        player!!.release()
        player = null
    }


    abstract class PlayerEventListener : Player.EventListener {
        override fun onPlaybackParametersChanged(playbackParameters: PlaybackParameters?) {}
        override fun onSeekProcessed() {}
        override fun onTracksChanged(trackGroups: TrackGroupArray?, trackSelections: TrackSelectionArray?) {}
        override fun onPlayerError(error: ExoPlaybackException?) {}
        override fun onLoadingChanged(isLoading: Boolean) {}
        override fun onPositionDiscontinuity(reason: Int) {}
        override fun onRepeatModeChanged(repeatMode: Int) {}
        override fun onShuffleModeEnabledChanged(shuffleModeEnabled: Boolean) {}
        override fun onTimelineChanged(timeline: Timeline?, manifest: Any?, reason: Int) {}
        override fun onPlayerStateChanged(playWhenReady: Boolean, playbackState: Int) {}
    }

    companion object {
        @JvmStatic
        fun getUserAgent(context: Context): String {
            val packageManager = context.packageManager
            val info = packageManager.getPackageInfo(context.packageName, 0)
            val appName = info.applicationInfo.loadLabel(packageManager).toString()
            return Util.getUserAgent(context, appName)
        }
    }

    fun SimpleExoPlayer.playVideoFromUri(context: Context, uri: Uri) {
        val dataSourceFactory = DefaultDataSourceFactory(context, MainActivity.getUserAgent(context))
        val mediaSource = ExtractorMediaSource.Factory(dataSourceFactory).createMediaSource(uri)
        prepare(mediaSource)
    }


    fun SimpleExoPlayer.playVideoFromUrl(context: Context, url: String) = playVideoFromUri(context, Uri.parse(url))

    fun SimpleExoPlayer.playVideoFile(context: Context, file: File) = playVideoFromUri(context, Uri.fromFile(file))
}

我试过的

我已经尝试阅读文档,并获得了这些链接(通过在此处询问):

https://medium.com/google-exoplayer/downloading-streams-6d259eec7f95 https://medium.com/google-exoplayer/downloading-adaptive-streams-37191f9776e

可悲的是,目前我能想出的唯一解决方案是在另一个线程上下载文件,这将导致设备与它有 2 个连接,从而使用两倍的带宽。

问题

  1. 如何使用 ExoPlayer 播放视频文件,同时将其下载到某个文件路径?
  2. 有没有办法让 ExoPlayer 上的缓存机制(使用磁盘)被激活以达到完全相同的目的?

注意:为了清楚。我不想下载文件,然后才播放它。


编辑:我找到了一种从 API 缓存中获取和使用文件的方法(在这里写过),但似乎这被认为是不安全的(写在这里)。

所以,鉴于 ExoPlayer 的 API 支持的简单缓存机制,我目前的问题是:

  1. 如果文件被缓存,我怎样才能以安全的方式使用它?
  2. 如果一个文件被部分缓存(意味着我们已经下载了它的一部分),我如何继续准备它(没有实际播放它或等待整个播放完成)直到我可以使用它(当然是以安全的方式) ?

我在这里为此创建了一个 Github 存储库。你可以试试看。

标签: androidexoplayer

解决方案


我在这里查看了 erdemguven 的示例代码,似乎有一些可行的方法。这基本上是 erdemguven 写的,但我写入文件而不是字节数组并创建数据源。我认为,由于 ExoPlayer 专家 erdemguven 将此作为访问缓存的正确方法,因此我的 mod 也是“正确的”并且不违反任何规则。

这是代码。getCachedData是新的东西。

class MainActivity : AppCompatActivity(), CacheDataSource.EventListener, TransferListener {

    private var player: SimpleExoPlayer? = null

    companion object {
        // About 10 seconds and 1 meg.
//        const val VIDEO_URL = "https://sample-videos.com/video123/mp4/720/big_buck_bunny_720p_1mb.mp4"

        // About 1 minute and 5.3 megs
        const val VIDEO_URL = "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"

        // The full movie about 355 megs.
//        const val VIDEO_URL = "http://distribution.bbb3d.renderfarming.net/video/mp4/bbb_sunflower_1080p_60fps_normal.mp4"

        // Use to download video other than the one you are viewing. See #3 test of the answer.
//        const val VIDEO_URL_LIE = "http://file-examples.com/wp-content/uploads/2017/04/file_example_MP4_480_1_5MG.mp4"

        // No changes in code deleted here.

    //NOTE: I know I shouldn't use an AsyncTask. It's just a sample...
    @SuppressLint("StaticFieldLeak")
    fun tryShareCacheFile() {
        // file is cached and ready to be used
        object : AsyncTask<Void?, Void?, File>() {
            override fun doInBackground(vararg params: Void?): File {
                val tempFile = FilesPaths.FILE_TO_SHARE.getFile(this@MainActivity, true)
                getCachedData(this@MainActivity, cache, VIDEO_URL, tempFile)
                return tempFile
            }

            override fun onPostExecute(result: File) {
                super.onPostExecute(result)
                val intent = prepareIntentForSharingFile(this@MainActivity, result)
                startActivity(intent)
            }
        }.execute()
    }

    private var mTotalBytesToRead = 0L
    private var mBytesReadFromCache: Long = 0
    private var mBytesReadFromNetwork: Long = 0

    @WorkerThread
    fun getCachedData(
        context: Context, myCache: Cache?, url: String, tempfile: File
    ): Boolean {
        var isSuccessful = false
        val myUpstreamDataSource = DefaultHttpDataSourceFactory(ExoPlayerEx.getUserAgent(context)).createDataSource()
        val dataSource = CacheDataSource(
            myCache,
            // If the cache doesn't have the whole content, the missing data will be read from upstream
            myUpstreamDataSource,
            FileDataSource(),
            // Set this to null if you don't want the downloaded data from upstream to be written to cache
            CacheDataSink(myCache, CacheDataSink.DEFAULT_BUFFER_SIZE.toLong()),
            /* flags= */ 0,
            /* eventListener= */ this
        )

        // Listen to the progress of the reads from cache and the network.
        dataSource.addTransferListener(this)

        var outFile: FileOutputStream? = null
        var bytesRead = 0

        // Total bytes read is the sum of these two variables.
        mTotalBytesToRead = C.LENGTH_UNSET.toLong()
        mBytesReadFromCache = 0
        mBytesReadFromNetwork = 0

        try {
            outFile = FileOutputStream(tempfile)
            mTotalBytesToRead = dataSource.open(DataSpec(Uri.parse(url)))
            // Just read from the data source and write to the file.
            val data = ByteArray(1024)

            Log.d("getCachedData", "<<<<Starting fetch...")
            while (bytesRead != C.RESULT_END_OF_INPUT) {
                bytesRead = dataSource.read(data, 0, data.size)
                if (bytesRead != C.RESULT_END_OF_INPUT) {
                    outFile.write(data, 0, bytesRead)
                }
            }
            isSuccessful = true
        } catch (e: IOException) {
            // error processing
        } finally {
            dataSource.close()
            outFile?.flush()
            outFile?.close()
        }

        return isSuccessful
    }

    override fun onCachedBytesRead(cacheSizeBytes: Long, cachedBytesRead: Long) {
        Log.d("onCachedBytesRead", "<<<<Cache read? Yes, (byte read) $cachedBytesRead (cache size) $cacheSizeBytes")
    }

    override fun onCacheIgnored(reason: Int) {
        Log.d("onCacheIgnored", "<<<<Cache ignored. Reason = $reason")
    }

    override fun onTransferInitializing(source: DataSource?, dataSpec: DataSpec?, isNetwork: Boolean) {
        Log.d("TransferListener", "<<<<Initializing isNetwork=$isNetwork")
    }

    override fun onTransferStart(source: DataSource?, dataSpec: DataSpec?, isNetwork: Boolean) {
        Log.d("TransferListener", "<<<<Transfer is starting isNetwork=$isNetwork")
    }

    override fun onTransferEnd(source: DataSource?, dataSpec: DataSpec?, isNetwork: Boolean) {
        reportProgress(0, isNetwork)
        Log.d("TransferListener", "<<<<Transfer has ended isNetwork=$isNetwork")
    }

    override fun onBytesTransferred(
        source: DataSource?,
        dataSpec: DataSpec?,
        isNetwork: Boolean,
        bytesTransferred: Int
    ) {
        // Report progress here.
        if (isNetwork) {
            mBytesReadFromNetwork += bytesTransferred
        } else {
            mBytesReadFromCache += bytesTransferred
        }

        reportProgress(bytesTransferred, isNetwork)
    }

    private fun reportProgress(bytesTransferred: Int, isNetwork: Boolean) {
        val percentComplete =
            100 * (mBytesReadFromNetwork + mBytesReadFromCache).toFloat() / mTotalBytesToRead
        val completed = "%.1f".format(percentComplete)
        Log.d(
            "TransferListener", "<<<<Bytes transferred: $bytesTransferred isNetwork=$isNetwork" +
                    " $completed% completed"
        )
    }

    // No changes below here.
}

这是我为测试它所做的,这绝不是详尽的:

  1. 只需使用 FAB 通过电子邮件共享视频。我收到了视频并且能够播放它。
  2. 关闭物理设备上的所有网络访问(飞行模式 = 打开)并通过电子邮件共享视频。当我重新打开网络(飞行模式 = 关闭)时,我收到并能够播放视频。这表明由于网络不可用,视频必须来自缓存。
  3. 更改了代码,以便VIDEO_URL我指定VIDEO_URL_LIE应该复制而不是从缓存中复制。(该应用程序仍然只播放VIDEO_URL。)由于我没有下载视频VIDEO_URL_LIE,该视频不在缓存中,因此该应用程序必须出网获取视频。我通过电子邮件成功收到了正确的视频并且能够播放它。这表明如果缓存不可用,应用程序可以访问基础资产。

我绝不是 ExoPlayer 专家,所以你可以用你可能有的任何问题快速地难倒我。


以下代码将在视频被读取并存储在本地文件中时跟踪进度。

// Get total bytes if known. This is C.LENGTH_UNSET if the video length is unknown.
totalBytesToRead = dataSource.open(DataSpec(Uri.parse(url)))

// Just read from the data source and write to the file.
val data = ByteArray(1024)
var bytesRead = 0
var totalBytesRead = 0L
while (bytesRead != C.RESULT_END_OF_INPUT) {
    bytesRead = dataSource.read(data, 0, data.size)
    if (bytesRead != C.RESULT_END_OF_INPUT) {
        outFile.write(data, 0, bytesRead)
        if (totalBytesToRead == C.LENGTH_UNSET.toLong()) {
            // Length of video in not known. Do something different here.
        } else {
            totalBytesRead += bytesRead
            Log.d("Progress:", "<<<< Percent read: %.2f".format(totalBytesRead.toFloat() / totalBytesToRead))
        }
    }
}

推荐阅读