【问题标题】:In ExoPlayer, how exactly to use SimpleExoPlayer.setVideoScalingMode to be like in ImageView center-crop?在 ExoPlayer 中,究竟如何使用 SimpleExoPlayer.setVideoScalingMode 就像在 ImageView center-crop 中一样?
【发布时间】:2018-07-05 14:59:56
【问题描述】:

背景

我正在尝试以中心裁剪方式显示视频(例如在 ImageView 上)。

我也希望我能找到如何以其他方式进行扩展,就像在 this library 上所做的那样。

问题

我将其用于此目的:

    player!!.videoScalingMode = C.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING

但由于某种原因,它不进行中心裁剪。

我尝试过的

查看文档,我看到了:

“请注意,缩放模式仅适用于启用基于 MediaCodec 的视频渲染器并且输出表面由 SurfaceView 拥有的情况。”

所以我尝试在 XML 文件中进行设置:

     app:surface_type="surface_view"

但它也没有帮助。我也没有在代码中看到它。

我试图用“renderer”或“codec”为这个类搜索任何东西,但都不存在。

这是我使用的代码(基于此:https://github.com/yusufcakmak/ExoPlayerSample):

class MainActivity : AppCompatActivity() {
    val VIDEO_URL = "https://www.sample-videos.com/video/mp4/720/big_buck_bunny_720p_1mb.mp4"
    private var player: SimpleExoPlayer? = null

    private var window: Timeline.Window? = null
    private var mediaDataSourceFactory: DataSource.Factory? = null
    private var trackSelector: DefaultTrackSelector? = null
    private var shouldAutoPlay: Boolean = false
    private var bandwidthMeter: BandwidthMeter? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        shouldAutoPlay = true
        bandwidthMeter = DefaultBandwidthMeter()
        mediaDataSourceFactory = DefaultDataSourceFactory(this, Util.getUserAgent(this, "mediaPlayerSample"), bandwidthMeter as TransferListener<in DataSource>)
        window = Timeline.Window()
    }

    private fun initializePlayer() {
        if (player != null)
            return
        simpleExoPlayerView.requestFocus()
        val videoTrackSelectionFactory = AdaptiveTrackSelection.Factory(bandwidthMeter)
        trackSelector = DefaultTrackSelector(videoTrackSelectionFactory)
        player = ExoPlayerFactory.newSimpleInstance(this, trackSelector)
        simpleExoPlayerView.player = player
        player!!.repeatMode = Player.REPEAT_MODE_ALL
        player!!.playWhenReady = shouldAutoPlay
        player!!.videoScalingMode = C.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING
        val extractorsFactory = DefaultExtractorsFactory()
        val mediaSource = ExtractorMediaSource(Uri.parse(VIDEO_URL), mediaDataSourceFactory, extractorsFactory, null, null)
        player!!.prepare(mediaSource)
    }

    private fun releasePlayer() {
        if (player != null) {
            shouldAutoPlay = player!!.getPlayWhenReady()
            player!!.release()
            player = null
            trackSelector = null
        }
    }

    public override fun onStart() {
        super.onStart()
        if (Util.SDK_INT > 23) {
            initializePlayer()
        }
    }

    public override fun onResume() {
        super.onResume()
        if (Util.SDK_INT <= 23 || player == null) {
            initializePlayer()
        }
    }

    public override fun onPause() {
        super.onPause()
        if (Util.SDK_INT <= 23) {
            releasePlayer()
        }
    }

    public override fun onStop() {
        super.onStop()
        if (Util.SDK_INT > 23) {
            releasePlayer()
        }
    }
}

activity_main.xml

<com.google.android.exoplayer2.ui.SimpleExoPlayerView
    android:id="@+id/simpleExoPlayerView" xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"
    android:focusable="true" app:surface_type="surface_view"/>

清单

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

gradle 文件

implementation 'com.google.android.exoplayer:exoplayer:r2.5.2'

问题

  1. 我做错了什么?

  2. 第一个要求到底是什么?

  3. 我觉得这个版本的SDK有点老了。我应该如何更新它?也许这是一个错误,它得到了修复?

【问题讨论】:

    标签: android scale exoplayer2.x


    【解决方案1】:

    好的,我明白了。不得不更新 SDK 并且出于某种原因使用支持库而不是它的 Android-X 版本。

    有一些可用的缩放类型,称为“ResizeMode”(here):

    • 缩放 = 与中心裁剪相同
    • fit = 与 fit-center 相同
    • fill = 与 fit-xy 相同(拉伸,不保持纵横比)

    代码如下:

    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()
            player = ExoPlayerFactory.newSimpleInstance(this, DefaultTrackSelector())
            playerView.player = player
            val dataSourceFactory = DefaultDataSourceFactory(this, getUserAgent(this))
            val mediaSource = ExtractorMediaSource.Factory(dataSourceFactory).createMediaSource(Uri.parse("https://www.sample-videos.com/video/mkv/720/big_buck_bunny_720p_2mb.mkv"))
            player!!.prepare(mediaSource)
            playerView.useController=false
            player!!.playWhenReady = true
            player!!.repeatMode = Player.REPEAT_MODE_ALL
        }
    
        override fun onStop() {
            super.onStop()
            playerView.player = null
            player!!.release()
            player = null
        }
    
        fun getUserAgent(context: Context): String {
            val packageName = context.packageName
            val info = context.packageManager.getPackageInfo(packageName, 0)
            val appName = info.applicationInfo.loadLabel(context.packageManager).toString()
            val versionName = info.versionName
            return "$appName/$versionName (Linux;Android ${Build.VERSION.RELEASE}) ${ExoPlayerLibraryInfo.VERSION_SLASHY}"
        }
    }
    

    清单

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

    布局

    <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">
    
        <!--zoom=center-crop fit=center-inside fill-stretch-->
    
        <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>
    

    分级

    dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
        implementation"org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
        implementation 'com.android.support:appcompat-v7:28.0.0-alpha3'
        implementation 'com.android.support.constraint:constraint-layout:1.1.2'
        testImplementation 'junit:junit:4.12'
        androidTestImplementation 'com.android.support.test:runner:1.0.2'
        androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
        implementation 'com.google.android.exoplayer:exoplayer-core:2.8.1'
        implementation 'com.google.android.exoplayer:exoplayer-ui:2.8.1'
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-27
      • 1970-01-01
      • 2012-11-28
      • 1970-01-01
      相关资源
      最近更新 更多