gpt4 book ai didi

android - 在 ExoPlayer 中,如何使用 SimpleExoPlayer.setVideoScalingMode 就像在 ImageView center-crop 中一样?

转载 作者:行者123 更新时间:2023-11-29 23:47:26 30 4
gpt4 key购买 nike

背景

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

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

问题

我为此目的使用它:

    player!!.videoScalingMode = C.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING

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

我尝试过的

查看文档,我看到了这个:

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

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

     app:surface_type="surface_view"

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

我试图为这个类搜索任何带有“渲染器”或“编解码器”的东西,但都不存在。

这是我使用的代码(基于此: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"/>

list

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

gradle 文件

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

问题

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

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

  3. 我觉得这个版本的 SDK 有点旧。我应该如何更新它?也许这是一个错误并且已修复?

最佳答案

好的,我知道了。必须更新 SDK 并出于某种原因使用支持库而不是它的 Android-X 版本。

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

  • zoom = 与 center-crop 相同
  • fit = 与 fit-center 相同
  • fill = 与 fit-xy 相同(拉伸(stretch),不保持纵横比)

代码如下:

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

list

<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'
}

关于android - 在 ExoPlayer 中,如何使用 SimpleExoPlayer.setVideoScalingMode 就像在 ImageView center-crop 中一样?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51194516/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com