ExoPlayer taking too long to play video - android

I am trying to play a video with exoplayer but it's taking too long to play.
How do I fix this issue?
PS - internet speed is not (so no speed issue)
private fun exoPlayerSetupVideo() {
var trackSelector = DefaultTrackSelector()
//var loadControl = DefaultLoadControl()
var exoPlayer = ExoPlayerFactory.newSimpleInstance(this, trackSelector)
simpleExoPlayerView.player = exoPlayer
var dataSourceFactory = DefaultDataSourceFactory(this, Util.getUserAgent(this, "VideoPlayer"))
var videoSource = ExtractorMediaSource.Factory(dataSourceFactory).createMediaSource(Uri.parse(url1))
exoPlayer.prepare(videoSource)
exoPlayer.playWhenReady = true
}

Have you tried some video urls inside the assets folder that the exoplayer demo project provide? If they play normally means that exoplayer is not the problem.

Related

How to use ExoPlayer in Android

In my application I want use exoplayer to play video and for this I added this dependency:
implementation 'com.google.android.exoplayer:exoplayer:2.18.0'
with simple way I can play video with this code:
player = ExoPlayer.Builder(this).build()
binding.player.player = player
// Build the media item.
val mediaItem: MediaItem = MediaItem.fromUri(url)
// Set the media item to be played.
player.setMediaItem(mediaItem)
// Prepare the player.
player.prepare()
// Start the playback.
player.play()
But I want add Header for video and for this I write below codes:
val dataSourceFactory: DataSource.Factory = DataSource.Factory {
val dataSource: HttpDataSource = httpDataSourceFactory.createDataSource()
// Set a custom authentication request header.
dataSource.setRequestProperty("Header", "Value")
dataSource
}
But in my code not found httpDataSourceFactory.
I used this link tutorial : https://exoplayer.dev/customization.html#customizing-server-interactions
How can I add header for exoplayer?
val dataSourceFactory = DefaultHttpDataSource.Factory()
.setDefaultRequestProperties(hashMapOf("Header" to "Value"))
val audioSource: MediaSource = ProgressiveMediaSource.Factory(dataSourceFactory)
Use the "DefaultHttpDataSource"

Exoplayer a little gap in playlist when using ConcatenatingMediaSource

I am using ConcatenatingMediaSource to make hls urls playlist for my ExoPlayer.
when playing media sources by local files, it works fine without gap.
However, when playing media sources by hls urls, It noticeably shows gap while transition (first video to second one)
I want my media source transit smoothly in ConcatenatingMediaSource.
How can I achieve this?
Guide me please.
This below is my init code.
val playerView = findViewById<PlayerView>(R.id.playerView)
val concatenatedSource = ConcatenatingMediaSource()
val trackSelector = DefaultTrackSelector(mContext!!)
trackSelector.parameters = trackSelector.buildUponParameters().build()
val dataSourceFactory = DefaultHttpDataSource.Factory()
val hlsMediaSource: HlsMediaSource = HlsMediaSource.Factory(dataSourceFactory)
.setAllowChunklessPreparation(true)
.createMediaSource(MediaItem.fromUri("https://multiplatform-f.akamaihd.net/i/multi/april11/sintel/sintel-hd_,512x288_450_b,640x360_700_b,768x432_1000_b,1024x576_1400_m,.mp4.csmil/master.m3u8"))
concatenatedSource.addMediaSource(hlsMediaSource)
val player = ExoPlayer.Builder(mContext)
.setTrackSelector(trackSelector)
.build()
player.setMediaSource(concatenatedSource)
player.addAnalyticsListener(EventLogger(trackSelector))
player.addListener(object : Player.Listener {
override fun onPlayerError(error: PlaybackException) {
super.onPlayerError(error)
}
})
playerView?.player = player
playerView?.player?.prepare()
playerView?.player?.playWhenReady = true
For those who have this kind of issues, you should try CacheDataSource while building MediaItem objects. This resolved my issues. Thanks

ExoPlayer not loading subtitles from .srt file

So I am trying to show subtitles from my .srt file in exoplayer but it does not work.
Do I need to use a seperate SubtitleView to get my subtitles to show up?
Is the subtitleView in the PlayerView is not enough?
I use PlayerView btw.
The exoplayer version I use is 2.14.0.
The addTextOutput method.
simpleExoPlayer.addTextOutput(cues -> {
playerView.getSubtitleView().setCues(cues);
playerView.getSubtitleView().setVisibility(View.VISIBLE);
playerView.getSubtitleView().onCues(cues);
assert cues.get(0).text != null;
Log.d("subtitles", cues.get(0).text.toString());
});
I also tried to implement to TextOutput but that did not work too.
The contents of my sample.srt file:
1
00:00:00,000 --> 00:00:01,500
For www.forom.com
2
00:00:01,500 --> 00:00:02,500
<i>Tonight's the night.</i>
3
00:00:03,000 --> 00:00:15,000
<i>And it's going to happen
again and again --</i>
The function I use to load my subtitles:
public void buildMediaSourceV3(Uri uri){
String subtitlesUri = sharedPreferencesSubtitles.getString(videoName.getText().toString(), "");
DataSource.Factory factory = new DefaultDataSourceFactory(VideoPlayer.this, getPackageName(), new DefaultBandwidthMeter());
MediaItem mediaItem = MediaItem.fromUri(uri);
MediaSource videoSource = new ProgressiveMediaSource.Factory(factory).createMediaSource(mediaItem);
if(subtitlesUri.equals(""))
{
simpleExoPlayer.addMediaSource(videoSource);
}
else
{
MediaItem.Subtitle subtitle = new MediaItem.Subtitle(Uri.parse(subtitlesUri), MimeTypes.APPLICATION_SUBRIP, "en");
MediaSource textMediaSource = new SingleSampleMediaSource.Factory(factory).createMediaSource(subtitle, C.TIME_UNSET);
textMediaSource.getMediaItem().mediaMetadata.subtitle.toString();
MergingMediaSource mergingMediaSource = new MergingMediaSource(videoSource, textMediaSource);
simpleExoPlayer.addMediaSource(mergingMediaSource);
}
}
I know you've already implemented a workaround.
I've just encountered the same issue, and found a solution:
trackSelector.setPreferredTextLanguage
This helped me. Full context:
val trackSelector = DefaultTrackSelector(context)
trackSelector.setParameters(trackSelector.buildUponParameters().setPreferredTextLanguage("en"))
Update:
I was wrong. That solution shows only video embedded captions, not the sideloading ones.
This works for me now:
val mediaItemBuilder = MediaItem.Builder()
.setUri(uri))
videoCaption?.vtt?.let {
val uriSubtitle = Uri.parse(it)
val mediaItemSubtitle = MediaItem.Subtitle(uriSubtitle,
MimeTypes.TEXT_VTT,
"en",
C.SELECTION_FLAG_DEFAULT)
mediaItemBuilder.setSubtitles(mutableListOf(mediaItemSubtitle))
}
val mediaItem = mediaItemBuilder.build()
val mediaSource = DefaultMediaSourceFactory(dataSourceFactory, extractoryFactory).createMediaSource(mediaItem)
Here is minimal example for video/mp4 with SRT subtitles. For XML layout is used com.google.android.exoplayer2.ui.StyledPlayerView and there is no need for additional subtitle layout. Files are located in assets directory inside the project.
val playerView = view.findViewById<StyledPlayerView>(R.id.video_pv)
val exoPlayer = ExoPlayer.Builder(requireActivity()).build()
playerView.player = exoPlayer
val assetSrtUri = Uri.parse(("file:///android_asset/subtitle.srt"))
val subtitle = SubtitleConfiguration.Builder(assetSrtUri)
.setMimeType(MimeTypes.APPLICATION_SUBRIP)
.setLanguage("en")
.setSelectionFlags(C.SELECTION_FLAG_DEFAULT)
.build()
val assetVideoUri = Uri.parse(("file:///android_asset/video.mp4"))
val mediaItem = MediaItem.Builder()
.setUri(assetVideoUri)
.setSubtitleConfigurations(ImmutableList.of(subtitle))
.build()
exoPlayer.setMediaItem(mediaItem)
exoPlayer.prepare()
exoPlayer.play()
Gradle dependencies:
implementation "com.google.android.exoplayer:exoplayer:2.17.1"
implementation "com.google.android.exoplayer:exoplayer-core:2.17.1"
implementation "com.google.android.exoplayer:exoplayer-ui:2.17.1"

ExoPlayer initialize player deprecated

I'm currently working with the latest ExoPlayer update and I'm getting calls that it is deprecated, could someone help me?
private fun initializePlayer () {
if (simpleExoPlayer == null) {
val trackSelector = DefaultTrackSelector(this)
val loadControl = DefaultLoadControl()
simpleExoPlayer = E̶x̶o̶P̶l̶a̶y̶e̶r̶F̶a̶c̶t̶o̶r̶y̶.̶n̶e̶w̶S̶i̶m̶p̶l̶e̶I̶n̶s̶t̶a̶n̶c̶e̶(this, trackSelector, loadControl)
}
}
Setting Up Exoplayer version(2.11.8) :
Sep 2020 Update:
//Setting Up Exoplayer
private void SetupPlayer(){
SimpleExoPlayer simpleExoPlayer;
// Create a data source factory.
dataSourceFactory =
new DefaultHttpDataSourceFactory(Util.getUserAgent(this
, getApplicationInfo().loadLabel(getPackageManager()).toString()));
// Passing Load Control
loadControl = new DefaultLoadControl.Builder()
.setBufferDurationsMs(25000, 50000, 1500, 2000).createDefaultLoadControl();
#DefaultRenderersFactory.ExtensionRendererMode int extensionRendererMode = DefaultRenderersFactory.EXTENSION_RENDERER_MODE_PREFER;
renderersFactory = new DefaultRenderersFactory(this) .setExtensionRendererMode(extensionRendererMode);
// Create a progressive media source pointing to a stream uri.
mediaSource = new ProgressiveMediaSource.Factory(dataSourceFactory)
.createMediaSource(Uri.parse(url_to_stream));
// Create a player instance.
simpleExoPlayer = new SimpleExoPlayer.Builder(this,renderersFactory).setLoadControl(loadControl).build();
// Prepare the player with the media source.
simpleExoPlayer.prepare(mediaSource, true, true);
}
It's worked. You should use this version:
implementation 'com.google.android.exoplayer:exoplayer:2.18.2'
exoPlayer = ExoPlayer.Builder(this).build()
exoPlayer?.playWhenReady = true
binding.playerView.player = exoPlayer
val defaultHttpDataSourceFactory = DefaultHttpDataSource.Factory()
val mediaItem =
MediaItem.fromUri(URL)
val mediaSource =
HlsMediaSource.Factory(defaultHttpDataSourceFactory).createMediaSource(mediaItem)
exoPlayer?.setMediaSource(mediaSource)
exoPlayer?.seekTo(playbackPosition)
exoPlayer?.playWhenReady = playWhenReady
exoPlayer?.prepare()

Exoplayer 2 Android not playing HLS

So I was trying to play some HLS with ExoPlayer 2. It was working fine but suddenly I have 403 error.
When I ran the url ("http://open.live.bbc.co.uk/mediaselector/5/redir/version/2.0/vpid/b0bbnbp9/mediaset/audio-syndication/proto/http") on safari it's working fine but not when I try it with the Android app.
val bandwidthMeter = DefaultBandwidthMeter()
val extractorsFactory = DefaultExtractorsFactory()
val trackSelectionFactory = AdaptiveTrackSelection.Factory(bandwidthMeter)
val trackSelector = DefaultTrackSelector(trackSelectionFactory)
val userAgent: String = Util.getUserAgent(applicationContext, "mediaPlayerSample")
val httpDataSourceFactory = DefaultHttpDataSourceFactory(
userAgent,
null,
DefaultHttpDataSource.DEFAULT_CONNECT_TIMEOUT_MILLIS,
DefaultHttpDataSource.DEFAULT_READ_TIMEOUT_MILLIS,
true
)
val mediaSource = ExtractorMediaSource.Factory(httpDataSourceFactory).setExtractorsFactory(extractorsFactory).createMediaSource(Uri.parse(url))
mPlayer = ExoPlayerFactory.newSimpleInstance(this, trackSelector)
mPlayer.seekTo(contentPosition)
mPlayer.prepare(mediaSource)
mPlayer.playWhenReady = true
Error Message:
07-24 12:19:41.784 28569-29226/com.twoversion E/ExoPlayerImplInternal: Source error.
com.google.android.exoplayer2.upstream.HttpDataSource$InvalidResponseCodeException: Response code: 403
at com.google.android.exoplayer2.upstream.DefaultHttpDataSource.open(DefaultHttpDataSource.java:211)
at com.google.android.exoplayer2.upstream.DataSourceInputStream.checkOpened(DataSourceInputStream.java:102)
at com.google.android.exoplayer2.upstream.DataSourceInputStream.open(DataSourceInputStream.java:65)
at com.google.android.exoplayer2.upstream.ParsingLoadable.load(ParsingLoadable.java:129)
at com.google.android.exoplayer2.upstream.Loader$LoadTask.run(Loader.java:308)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:761)
Your code seem's perfect, Just you need to change your code:
val mediaSource = ExtractorMediaSource.Factory(httpDataSourceFactory).setExtractorsFactory(extractorsFactory).createMediaSource(Uri.parse(url))
To, Update below code:
HlsMediaSource hlsMediaSource = new HlsMediaSource(Uri.parse(url),httpDataSourceFactory, 1800000,new Handler(), null);
mPlayer.prepare(hlsMediaSource)
The player is giving a source error as the url you are giving to the player is not a valid hls url.
Safari is able to play the file because it can figure out the actual redirect url and play that.
The url you are giving is actually redirecting to :
http://cp401492-vh.akamaihd.net/i/prod_af_mp4_heaacv1_48/iplayerstream/l2o/b0bbnbp9_22abe4cc-c52b-4cf5-a785-e7803e1fdd9a.mp4/master.m3u8?hdnea=st=1532432045~exp=1532453645~acl=/b0bbnbp9_22abe4cc-c52b-4cf5-a785-e7803e1fdd9a.mp4~hmac=0fe4da8ddc4bc2dacd92865be29097d0a251cfa3489a7c56603563a268fe2c21
This is a valid hls url (has a m3u8 file). Try playing this on exoplayer and it should work
EDIT:
you can enable following of cross-protocol redirects in ExoPlayer by passing allowCrossProtocolRedirects=true to the DefaultUriDataSource constructor.

Categories

Resources