I'm trying to have a video shown in center-crop manner (like on ImageView).
I also hope I can find how to scale in other ways, as done on this library.
I use this for this purpose :
player!!.videoScalingMode = C.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING
But for some reason it doesn't do center-cropping.
Looking at the docs, I see this:
"Note that the scaling mode only applies if a MediaCodec-based video Renderer is enabled and if the output surface is owned by a SurfaceView."
So I tried to set this in the XML file:
app:surface_type="surface_view"
but it also didn't help. I don't see it available in code, either.
I tried to search for anything with "renderer" or "codec" for this class, but none exist.
Here's the code I use (based on this: 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"/>
manifest
<uses-permission android:name="android.permission.INTERNET"/>
gradle file
implementation 'com.google.android.exoplayer:exoplayer:r2.5.2'
What's wrong in what I did?
What is exactly the first requirement?
I think this version of the SDK is a bit old. How should I update it ? Maybe it's a bug and it got fixed ?
OK I got it. Had to update the SDK and for some reason use support library instead of Android-X version of it.
There are some scaling types available, called "ResizeMode" (here) :
Here's the code:
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}"
}
}
manifest
<uses-permission android:name="android.permission.INTERNET"/>
layout
<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>
gradle
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'
}