在ExoPlayer中,如何完全使用SimpleExoPlayer.setVideoScalingMode像在ImageView中心裁剪中一样?

时间:2018-07-05 14:59:57

标签: android scale exoplayer2.x

背景

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

我也希望我能找到如何像this library一样以其他方式进行缩放。

问题

我将其用于此目的:

     app:surface_type="surface_view"

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

我尝试过的

看文档,我看到了:

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

所以我试图在XML文件中设置它:

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()
        }
    }
}

但是它也没有帮助。我也看不到它在代码中可用。

我试图使用此类的“ renderer”或“ codec”搜索任何内容,但都不存在。

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

<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"/>

activity_main.xml

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

清单

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

渐变文件

keras

问题

  1. 我做的事有什么问题?

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

  3. 我认为此版本的SDK有点旧。我应该如何更新?也许是错误,并且已解决?

1 个答案:

答案 0 :(得分:1)

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

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

  • zoom =与中心裁剪相同
  • 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'
}