Exoplayer not playing video only audio plays - android

ExoPlayer is not showing the video. I can listen the audio but video is not playing. I am using the Exoplayer in the Recyclerview.I only can see the black screen and listen to audio.I am not able to track the problem. I am playing the HLS video in the ExoPlayer.

I was also getting same issue few days back, now posting it here so that it can make someone's life easy since this problem appears very often when we use Exoplayer with RecyclerView.
Cause of the issue (in my case):
PlayerView was getting changed every time I came on the screen (due to the presence of RecyclerView)
I handled it by setting the player each time on the PlayerView object inside showLivePlayer() method which is called each time recycler view enabled screen opens to play the video.
public void showLivePlayer(PlayerView playerView, String videoURL, String tokenURL, ProgressBar progressBar){
mPlayerView = playerView;
if(player != null)
mPlayerView.setPlayer(player); //THIS IS THE FIX
mProgressBar = progressBar;
//register event bus
if (!EventBus.getDefault().isRegistered(this))
EventBus.getDefault().register(this);
shouldAutoPlay = true;
bandwidthMeter = new DefaultBandwidthMeter();
mediaDataSourceFactory = new DefaultDataSourceFactory(mContext,
Util.getUserAgent(mContext, mContext.getString(R.string.app_name)), bandwidthMeter);
window = new Timeline.Window();
getLiveVideoToken(tokenURL, videoURL);
}

my bad was I was using PlayerControlView instead of PlayerView

Related

SimpleExoPlayer black screen bug?

im using the SimpleExoPlayer in my android project and got this problem. i have two activities, naming a and b, that a jumps to b and. Both a and b have their own playerview but using the same player(by singleton). The app starts with a activity and the player works with video and sound, and the same with b when it jumps to b. However when jumping back, the playerview in a activity only plays sounds with black screen. How to fix this bug? Begging for anwsers
Code for init player
DataSource.Factory dataSourceFactory =
new DefaultDataSourceFactory(requireContext(), requireContext().getPackageName());
ProgressiveMediaSource mediaSource = new ProgressiveMediaSource.Factory(dataSourceFactory)
.createMediaSource(Uri.parse(upload.getUploads().getUrl() + ".mp4"));
LoopingMediaSource loopingSource = new LoopingMediaSource(mediaSource);
Log.e("///////////", "VIDEO URL=" + upload.getUploads().getUrl());
player.prepare(loopingSource);
player.setPlayWhenReady(true);
In your onPause method add player.setPlayWhenReady(false); and in your onResume again initialize player and use

Android exo player instance not working properly?

I am using ExoPLayer to play video in android. Suppose I have two ExoPlayer instance. I initialize first player and then assign it to second player, then I release first player. When I release the first player it also stops the second player. I want to continue playing videos with second player. Hope you understand. Below is the sample code...
private fun switchParameters() {
// SimpleExoPlayer instance
currentPlayer = temporaryPlayer
// PlayerView instance
playerView = temporaryPlayerView
if (temporaryPlayer != null) {
temporaryPlayer!!.release()
temporaryPlayer = null
mediaSource = null
temporaryPlayerView!!.onPause()
temporaryPlayerView = null
}
}
When I release temporaryPlayer , currentPlayer stops working. I don't want this.

ExoPlayer: play sound in the background with a video file

Playing a video, and when the app goes to background it should continue playing the audio, and when I reopen it should resume the video respecting where we are in audio.
I'm using exoplayer in a service, I was able to play the audio in background, but when I do the same for video, the audio is playing but when I come back to the app the video is just a black screen, and if I repeat (going to background and coming back to app) the step again it will continue playing video.
As I understood, exoplayer is buffering next frames and the player view got stuck unable to render all frames at once.
I have one instance of exoplayer:
public class ExoPlayerWrapper {
private static SimpleExoPlayer exoPlayer;
public static SimpleExoPlayer getExoPlayer(Context context){
if(exoPlayer == null){
exoPlayer = ExoPlayerFactory.newSimpleInstance(context, new DefaultTrackSelector());
}
return exoPlayer;
}
public static void release() {
if(exoPlayer != null) {
exoPlayer.release();
exoPlayer = null;
}
}
}
The service runs once the view is in foreground
Finally, I did it with, in on pause set playerView.setPlayer(null) and in on resume set playerView.setPlayer(exoplayer_instance)

RecyclerView with ExoPlayer/VideoView bad Performance

I want to play Videos in my RecyclerView Items, kind of like Instagram. There is only one Item expanded at a time so the Videos don't have to be played at the same time.
Im currently using the ExoPlayer. I have a PlayerView inside my item for the RecyclerView.
The problem is, with the Player I have bad performance which is very bad when scrolling and expanding. The Performany is already bad with just having the Player in the item without loading and playing a Video but it gets even worse with loading it.
I have tried VideoView too but it doesn't seem to make a big difference.
To achieve a better performance I already do:
recyclerView.setHasFixedSize(true);
recyclerView.setItemViewCacheSize(10);
recyclerView.setDrawingCacheEnabled(true);
recyclerView.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
adapter.hasStableIds();
This helps a little bit but its doesn't solve the performance problem.
Any Idea what the problem can be? I appreciate and thoughts.
EDIT:
this is the method I setup the ExoPlayer:
public void showTutuorItems(Context context, Boolean show, String resource) {
if (show) {
seperator.setVisibility(View.VISIBLE);
lytTutor.setVisibility(View.VISIBLE);
playerView = itemView.findViewById(R.id.exoPlayer);
playerView.setUseController(false);
playerView = itemView.findViewById(R.id.exoPlayer);
playerView.setUseController(false);
SimpleExoPlayer player = ExoPlayerFactory.newSimpleInstance(
new DefaultRenderersFactory(context),
new DefaultTrackSelector(), new DefaultLoadControl());
playerView.setPlayer(player);
player.setPlayWhenReady(true);
player.seekTo(0, 0);
player.setRepeatMode(Player.REPEAT_MODE_ALL);
player.setVolume(0);
Uri uri = Uri.parse(resource);
MediaSource mediaSource = buildMediaSource(uri);
player.prepare(mediaSource, true, false);
player.addListener(new Player.EventListener() {
//...
}
} else {
seperator.setVisibility(View.GONE);
lytTutor.setVisibility(View.GONE);
}
}
private MediaSource buildMediaSource(Uri uri) {
return new ExtractorMediaSource.Factory(
new DefaultHttpDataSourceFactory("exoplayer-codelab")).
createMediaSource(uri);
}
and this is in my OnCreateViewHolder to call this method:
if (workout.getCurrentExercsise().getTutor() != null) {
holder.showTutuorItems(activity, true, workout.getCurrentExercsise().getTutor().getItems().get(0).getResource());
} else {
holder.showTutuorItems(activity, false, null);
}
In my OnBindViewHolder is some UI stuff and handling the expand/collapse function. This shouldn't cause the problem because without the ExoPlayer it works just fine.

ExoPlayer - play 10 files one after another

I have 10 video i need to play, once one is done, the next one starts to play.
I'm using Google's ExoPlayer, I use the example in the DEMO # GitHub.
I can play 1 video but if i try to play the next one, it wont start.
If i try to reInit the player, and the start playing again, it crashes.
private void loadvideo() {
Uri uri = Uri.parse(VIDEO_LIBRARY_URL + currentVideo + ".mp4");
sampleSource = new FrameworkSampleSource(this, uri, null, 2);
// 1. Instantiate the player.
// 2. Construct renderers.
videoRenderer = new MediaCodecVideoTrackRenderer(sampleSource, MediaCodec.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING);
audioRenderer = new MediaCodecAudioTrackRenderer(sampleSource);
// 3. Inject the renderers through prepare.
player.prepare(videoRenderer, audioRenderer);
// 4. Pass the surface to the video renderer.
surface = surfaceView.getHolder().getSurface();
player.sendMessage(videoRenderer, MediaCodecVideoTrackRenderer.MSG_SET_SURFACE, surface);
// 5. Start playback.
player.setPlayWhenReady(true);
player.addListener(new ExoPlayer.Listener() {
#Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
Log.d(TAG, "onPlayerStateChanged + " + playbackState);
if (playbackState == ExoPlayer.STATE_ENDED) {
currentVideo++;
loadNextVideo();
}
}
#Override
public void onPlayWhenReadyCommitted() {
}
#Override
public void onPlayerError(ExoPlaybackException error) {
}
});
}
What am i doing wrong?
How can i play videos continuity?
Thanks.
You can reuse the ExoPlayer up until the point that you call release(), and then it should no longer be used.
To change the media that it is currently playing, you essentially need to perform the following steps:
// ...enable autoplay...
player.stop();
player.seekTo(0L);
player.prepare(renderers);
Creating the renderers is a little bit more involved, but that's the flow you should follow and the player should be able to play back to back videos.
I'm using Exoplayer change mp4 video success. I use the example in the DEMO.
1.DEMO project in DemoPlayer.java:
private final RendererBuilder rendererBuilder;
//remove final,then modify that:
private RendererBuilder rendererBuilder;
//and add the set method:
public void setRendererBuilder(RendererBuilder rendererBuilder){
this.rendererBuilder = rendererBuilder;
}
//finally,add stop method
public void stop(){
player.stop();
}
2.DEMO project in PlayerActivity.java:
add method:
private void changeVideo(){
player.stop();
player.seekTo(0L);
//you must change your contentUri before invoke getRendererBuilder();
player.setRendererBuilder(getRendererBuilder());
player.prepare();
playerNeedsPrepare = false;
}
remember change param contentUri before invoke changeVideo method.
Use ConcatenatingMediaSource to play files in sequence.
For example, for playing 2 media Uris (firstVideoUri and secondVideoUri), use this code:
MediaSource firstSource =
new ExtractorMediaSource.Factory(...).createMediaSource(firstVideoUri);
MediaSource secondSource =
new ExtractorMediaSource.Factory(...).createMediaSource(secondVideoUri);
ConcatenatingMediaSource concatenatedSource =
new ConcatenatingMediaSource(firstSourceTwice, secondSource);
And then use concatenatedSource to play media files sequentially.
OK, Answering my own question.
on the example, google init the ExoPlayer at OnResume().
i had to re-init for every video like that:
player = ExoPlayer.Factory.newInstance(2, 1000, 5000);
if someone has a better idea, please let me know.
There is another solution, you could refer to ConcatenatingMediaSource to achieve auto play next media.
In Demo App example :
1. Launch ExoPlayer
2. Select Playlists
3. Choose Cats->Dogs

Categories

Resources