Could exoplayer access webplayer video? - android

I'm still new with kotlin and exoplayer. i tried to set this link as my video's source but it showed some error. Is it possible to use this kind of link? Since, i thought the problem was the unsupported format so i tried using HLS but either it didnt work or i lost some step there. Please need help
exoPlayer= ExoPlayer.Builder(this)
.setSeekBackIncrementMs(5000)
.setSeekForwardIncrementMs(5000)
.build()
playerView.player = exoPlayer
playerView.keepScreenOn = true
exoPlayer.addListener(object: Player.Listener{
override fun onPlayerStateChanged(playWhenReady: Boolean, playbackState: Int)
{
if(playbackState == Player.STATE_BUFFERING)
{
progressBar.visibility = View.VISIBLE
}
else if(playbackState == Player.STATE_READY)
{
progressBar.visibility = View.GONE
}
if(!exoPlayer.playWhenReady)
{
handler.removeCallbacks(updateProgressAction)
}
else
{
onProgress()
}
}
})
val videoSource = Uri.parse("https://vanfem.com/v/8qqlyh8lmxgkn5y")
val mediaItem = MediaItem.fromUri(videoSource)
exoPlayer.setMediaItem(mediaItem)
exoPlayer.prepare()
exoPlayer.play()
}

The link in your example is not to a video - in fact it seems to link to web ads.
ExoPlayer won't be able to play that as it's not a valid media source.

Related

Media3 ExoPlayer - DefaultTimeBar cannot be cast to TimeBar

Im migrating a Video screen from XML to Compose., I need to display Ads with the IMA plugin.
Right now, I'm using this dependencies:
implementation "androidx.media3:media3-ui:1.0.0-beta02"
implementation 'androidx.media3:media3-exoplayer:1.0.0-beta02'
implementation "androidx.media3:media3-exoplayer-ima:1.0.0-beta02"
And im implementing the player as follows:
#Composable
fun VideoPlayer(videoUri: String) {
val context = LocalContext.current
val exoPlayer = remember {
ExoPlayer.Builder(context)
.build()
.apply {
val defaultDataSourceFactory = DefaultDataSource.Factory(context)
val dataSourceFactory: DataSource.Factory = DefaultDataSource.Factory(
context,
defaultDataSourceFactory
)
val source = ProgressiveMediaSource.Factory(dataSourceFactory)
.createMediaSource(MediaItem.fromUri(videoUri))
setMediaSource(source)
prepare()
}
}
DisposableEffect(
AndroidView(factory = {
PlayerView(context).apply {
hideController()
player = exoPlayer
}
})
) {
onDispose { exoPlayer.release() }
}
}
But, when I run my app and navigate to the ExoPlayer screen, I get this error:
java.lang.ClassCastException: com.google.android.exoplayer2.ui.DefaultTimeBar cannot be cast to androidx.media3.ui.TimeBar
I find this odd, but perhaps in ExoPlayer3 you can customize the TimeBar and choose what class to use. Has someone has faced this before?

Android Chromecast Sender won't update title and images

I've followed the instruction from Google on how to cast media metadata to chromecast, the initial loading is fine, it will show the title, image and play the stream, but my problem is that I am streaming a live audio stream and need to update the metadata from time to time without having to buffer the audio again.
This is a sample of my code:
override fun loadMediaLoadRequestData(request: PlatformBridgeApis.MediaLoadRequestData?)
{
if (request == null) return
val remoteMediaClient: RemoteMediaClient = remoteMediaClient ?: return
val mediaLoadRequest = getMediaLoadRequestData(request)
remoteMediaClient.load(mediaLoadRequest)
}
fun getMediaLoadRequestData(request: PlatformBridgeApis.MediaLoadRequestData): MediaLoadRequestData {
val mediaInfo = getMediaInfo(request.mediaInfo)
return MediaLoadRequestData.Builder()
.setMediaInfo(mediaInfo)
.setAutoplay(request.shouldAutoplay)
.setCurrentTime(request.currentTime)
.build()
}
fun getMediaInfo(mediaInfo: PlatformBridgeApis.MediaInfo?): MediaInfo? {
if (mediaInfo == null) return null
val streamType = getStreamType(mediaInfo.streamType)
val metadata = getMediaMetadata(mediaInfo.mediaMetadata)
val mediaTracks = mediaInfo.mediaTracks.map { getMediaTrack(it) }
val customData = JSONObject(mediaInfo.customDataAsJson ?: "{}")
return MediaInfo.Builder(mediaInfo.contentId)
.setStreamType(streamType)
.setContentType(mediaInfo.contentType)
.setMetadata(metadata)
.setMediaTracks(mediaTracks)
.setStreamDuration(mediaInfo.streamDuration)
.setCustomData(customData)
.build()
}
Does anyone have any suggestion on how to modify loadMediaLoadRequestData in order to trigger the Chromecast receiver to update only the MediaMetadata and not have the stream buffer again?

Small laggy when prepare exoplayer

In my application, I had an infinite animation. Actually, it's a TextSwitcher animation running from left to right. However, whenever I tried to prepare the player, I got a jitter issue with the text's animation. Although it's just a few milliseconds, it caused my texts was like jumping from left to right. Here's the prepare video method:
private fun prepareVideo(uri: Uri): SimpleExoPlayer? {
val simpleExoPlayer = SimpleExoPlayer
.Builder(this) // .setLoadControl(defaultLoadControl)
.build()
val mediaItem = MediaItem.fromUri(uri)
simpleExoPlayer.setMediaItem(mediaItem)
val eventListener: Player.EventListener = object : Player.EventListener {
override fun onPlaybackStateChanged(state: Int) {
if (state == ExoPlayer.STATE_READY) {
playVideo(simpleExoPlayer)
}
}
}
simpleExoPlayer.addListener(eventListener)
simpleExoPlayer.playWhenReady = false
simpleExoPlayer.prepare()
showToast("Prepare video")
return simpleExoPlayer
}
Do you guys have any idea to fix it?
If you thinks problem in this part of code i can recommend you to try initialize your SimpleExoPlayer object in application class and use it's instance all across application.
Try with this code
var exoPlayer: SimpleExoPlayer? = null
private fun setupVideo(uriString: String) {
val appName = R.string.app_name
val trackSelector: TrackSelector = DefaultTrackSelector()
val userAgent = context.let { Util.getUserAgent(it, it.getString(appName)) }
val sourceFactory = DefaultDataSourceFactory(requireContext(), userAgent)
val uri = Uri.parse(uriString)
val mediaSource = ProgressiveMediaSource.Factory(sourceFactory).createMediaSource(uri)
exoPlayer = ExoPlayerFactory.newSimpleInstance(context, trackSelector)
exoPlayer?.prepare(mediaSource)
exoPlayer?.playWhenReady = false
pvPlayer.player = exoPlayer
}
After quite some time investigating, I found the issue is creating an ExoPlayer instance.
val simpleExoPlayer = SimpleExoPlayer
.Builder(this) // .setLoadControl(defaultLoadControl)
.build()
Just this simple blook took about 0.1 seconds to finish. As a result, it froze the animation for a short period of time. I decided to create it on splash screen and reused it whenever possible.

what's the difference between this two video url using exoplayer in android?

here is two urls. First is "https://28api.haii.io/media/video/video_60_1.mp4",and Second is "https://28api.haii.io/media/video/video_70_1.mp4". When I load first video url, it load and play well. But when I load second url, the second video is broken. I can here only audio. So I tried comparing this two video by download.. but I can't understand the difference between this two url. Both are mp4 encoded video. But Exoplayer only load well at first but not at second.... how can I solve this... help me...
private fun initializePlayer() {
if (player == null) {
val trackSelector = DefaultTrackSelector()
trackSelector.setParameters(
trackSelector.buildUponParameters().setMaxVideoSizeSd()
)
player = ExoPlayerFactory.newSimpleInstance(context,trackSelector)
binding.videoPlayer.player = player
binding.videoPlayer.useController=false
binding.playerControl.player = binding.videoPlayer.player
mediaSource = mediaSourceFactory.createMediaSource(Uri.parse(mediaList[currentIndex].url))
player!!.prepare(mediaSource,false,false)
player!!.seekTo(currentWindow, playbackPosition)
player!!.playWhenReady = playWhenReady
setAudioFocus()
player!!.addListener(object : Player.EventListener {
override fun onPlayerStateChanged(playWhenReady: Boolean, playbackState: Int) {
when (playbackState) {
Player.STATE_IDLE -> {
}
Player.STATE_BUFFERING -> {
}
Player.STATE_READY -> {
}
Player.STATE_ENDED -> {
showPlayButton()
player!!.seekTo(currentWindow, 0)
showThumbnailImage()
player!!.playWhenReady = false
}
else -> {
}
}
}
})
}
}
private fun getVideoSource(url :String){
videoUrl = url
var mediaSource = mediaSourceFactory.createMediaSource(Uri.parse(url))
player!!.prepare(mediaSource)
playbackPosition=0
player!!.seekTo(currentWindow, playbackPosition)
player!!.playWhenReady = false
}

ExoPlayer sometimes returns negative duration for an mp3 file from url

I'm making an app where I play .mp3 files from the URL. I'm using the latest version of ExoPlayer 2.11.4.
What I need to do is get the total duration of the audio from the url so I can use it in my custom audio player.
The urls I'm using are of this type: https://myserver.net/.../audio/439688e0c3626d39d3ef3.mp3?token=6oz-22-2sa-9yh-7e-2iak
The problem is that sometimes my code works correctly most of the time and returns the correct duration. But sometimes what I get is a negative number: -9223372036854775807
And that doesn't allow my code to work properly. My code where I get the duration is this:
fun getDuration(url: String, context: Context) {
exoPlayer?.release()
exoPlayer = SimpleExoPlayer.Builder(context).build()
val dataSourceFactory = DefaultDataSourceFactory(context, Util.getUserAgent(context, "ExoPlayer"))
val mediaSource = ProgressiveMediaSource.Factory(dataSourceFactory).createMediaSource(Uri.parse(url))
exoPlayer?.prepare(mediaSource)
exoPlayer?.addListener(object : Player.EventListener {
override fun onPlayerStateChanged(playWhenReady: Boolean, playbackState: Int) {
if (playbackState == ExoPlayer.STATE_READY) {
val realDurationMillis: Long? = exoPlayer?.getDuration()
currentDuration = realDurationMillis
if (currentDuration != null) {
if (currentDuration!! > 0) {
exoPlayer?.release()
}
}
}
}
})
}
C.TIME_UNSET is defined as Long.MIN_VALUE + 1 which is where your -9223372036854775807 comes from.
/**
* Special constant representing an unset or unknown time or duration. Suitable for use in any
* time base.
*/
public static final long TIME_UNSET = Long.MIN_VALUE + 1;

Categories

Resources