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)
Related
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.
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.
I am working on a push to talk App, where the receiver will get a live-streamed audio message from a sender if he is connected to the internet. Now everything's working all fine except one issue that is: whenever any other applications like google music or youtube are playing audios, that time if I am getting an audio notification or message through the application, both the audios are playing in a parallel manner. I am using VoiceLayer library for the app and to play a message audio, they use VoiceLayerMessagePlayer. Is there any way I can pause the other media players when I am getting a notification or voice message in my Application? I looked through the internet and found out that OnAudioFocusChangeListener might be helpful but didn't get a proper example regarding its implementation. Please do let me know if you need any more information. Thanks in advance.
You should use AudioManager service to receive notification whether you receive/lost audio focus (Managing audio focus). I've done similar thing in a project where when my app starts playing, Google Play pause and vice versa. Use the following code where you are controlling your media playback like (activity or service)
Stop MediaPlayer when an other app play music
Please check below code it's working fine.
AudioManager am = null;
// Request focus for music stream and pass
AudioManager.OnAudioFocusChangeListener
// implementation reference
int result = am.requestAudioFocus(this, AudioManager.STREAM_MUSIC,
AudioManager.AUDIOFOCUS_GAIN);
if(result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED)
{
// Play
}
// Implements AudioManager.OnAudioFocusChangeListener
#Override
public void onAudioFocusChange(int focusChange)
{
if(focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT)
{
// Pause
}
else if(focusChange == AudioManager.AUDIOFOCUS_GAIN)
{
// Resume
}
else if(focusChange == AudioManager.AUDIOFOCUS_LOSS)
{
// Stop or pause depending on your need
}
}
Hope this helps you and solved your problem.
please try this code. it work with me
val audioManager = requireActivity().getSystemService(Context.AUDIO_SERVICE) as AudioManager
val focusRequest = AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN).run {
setAudioAttributes(AudioAttributes.Builder().run {
setUsage(AudioAttributes.USAGE_GAME)
setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
setOnAudioFocusChangeListener({ }, object : Handler() {})
build()
})
setAcceptsDelayedFocusGain(true)
build()
}
audioManager.requestAudioFocus(focusRequest)
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
so im trying to make a sound play through android. ive created a mediaplayer like this
MediaPlayer player; (outside onCreate())
player = MediaPlayer.create(this, R.raw.theninth); (inside onCreate())
and then i reference this player throughout the Activity. .start() on shake. .pause onclick etc.. it looks like this
if(mag >= 500) {
if(!started) {
mainView.setText(R.string.waitforit);
player.start();
started = true;
if(doThisOnce) {
timer.schedule(changeText, 26000);
doThisOnce = false;
}
}
player.setVolume((float)(2*mag/3.0),(float)(2*mag/3.0));
}
and
public void stopAudio(View v) {
mainView.setText(R.string.shakeme);
player.pause();
started = false;
}
and so the problem is the player doesnt pause when i tap the screen, instead it goes to the starting text of hte app, and and its like i reloaded the entire app, and i can shake it and then another intance of the same file from the same player will start playing.. what do?