SimpleExoPlayer black screen bug? - android

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

Related

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 - Plays same audio over and over on reopening the activity

I'm creating an exoplayer instance and adding a stream URL as progressive media source, preparing the player and playing the audio. When I go back to previous activity and open the player activity again, another instance of player is running the same audio (Two instances playing same audio simultaneously). Also I have a mute button. It works as expected when I open the activity for the first time. On reopening the activity, the mute button mutes only the ExoPlayer instance of the current activity
I tried Moving the ExoPlayer code to another class and calling them with public functions, but didn't work
val dataSourceFactory = DefaultDataSourceFactory(this, Util.getUserAgent(this, packageName))
val newMediaSource = ProgressiveMediaSource.Factory(dataSourceFactory).createMediaSource(Uri.parse("STREAM URL"));
val exoPlayer = ExoPlayerFactory.newSimpleInstance(this);
exoPlayer.prepare((newMediaSource));
exoPlayer.playWhenReady = true;
muteButton.setOnClickListener {
if(muteButton.tag == "muted")
{
exoPlayer.volume = 1f;
muteButton.tag = "unmuted";
}
else
{
exoPlayer.volume = 0f;
muteButton.tag = "muted";
}
}
I want the same ExoPlayer to run on reopening the activity and also mute that particular instance
I ended up using shared preference to save the service's state. So whenever the service is started, the boolean in shared preference is set to true. So I can start the service only when the boolean is false. Also I am using Broadcast receiver in both activity and service to exchange messages.

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)

Exoplayer not playing video only audio plays

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

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