Play audio and video at the same time in Android - android

I'm trying to play two different files at the same time.
I have tried to find players and tried to extend the default player achieving the same but couldn't get success in that. so please help me with it, by letting me know what's the best way to play audio file and video at the same time?
The reason I'm taking separate files is to save space, because the app will be localized, having multiple audio files for each language instead of having multiple videos saves space. That's important because android doesn't allow the download of app size above 50MB.
Any help in this would be extremely helpful. And providing me code for this would be a great help.
Thanks in advance.

You can handle this with Audio Focus. Two or more Android apps can play audio to the same output stream simultaneously. The system mixes everything together. While this is technically impressive, it can be very aggravating to a user. To avoid every music app playing at the same time, Android introduces the idea of audio focus. Only one app can hold audio focus at a time.
When your app needs to output audio, it should request audio focus. When it has focus, it can play sound. However, after you acquire audio focus you may not be able to keep it until you’re done playing. Another app can request focus, which preempts your hold on audio focus. If that happens your app should pause playing or lower its volume to let users hear the new audio source more easily.
Beginning with Android 8.0 (API level 26), when you call requestAudioFocus() you must supply an AudioFocusRequest parameter. To release audio focus, call the method abandonAudioFocusRequest() which also takes an AudioFocusRequest as its argument. The same AudioFocusRequest instance should be used when requesting and abandoning focus.
To create an AudioFocusRequest, use an AudioFocusRequest.Builder. Since a focus request must always specify the type of the request, the type is included in the constructor for the builder. Use the builder's methods to set the other fields of the request.
The following example shows how to use an AudioFocusRequest.Builder to build an AudioFocusRequest and request and abandon audio focus:
audioManager = (AudioManager) Context.getSystemService(Context.AUDIO_SERVICE);
playbackAttributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_GAME)
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.build();
focusRequest = new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN)
.setAudioAttributes(playbackAttributes)
.setAcceptsDelayedFocusGain(true)
.setOnAudioFocusChangeListener(afChangeListener, handler)
.build();
mediaPlayer = new MediaPlayer();
final Object focusLock = new Object();
boolean playbackDelayed = false;
boolean playbackNowAuthorized = false;
// ...
int res = audioManager.requestAudioFocus(focusRequest);
synchronized(focusLock) {
if (res == AudioManager.AUDIOFOCUS_REQUEST_FAILED) {
playbackNowAuthorized = false;
} else if (res == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
playbackNowAuthorized = true;
playbackNow();
} else if (res == AudioManager.AUDIOFOCUS_REQUEST_DELAYED) {
playbackDelayed = true;
playbackNowAuthorized = false;
}
}
// ...
#Override
public void onAudioFocusChange(int focusChange) {
switch (focusChange) {
case AudioManager.AUDIOFOCUS_GAIN:
if (playbackDelayed || resumeOnFocusGain) {
synchronized(focusLock) {
playbackDelayed = false;
resumeOnFocusGain = false;
}
playbackNow();
}
break;
case AudioManager.AUDIOFOCUS_LOSS:
synchronized(focusLock) {
resumeOnFocusGain = false;
playbackDelayed = false;
}
pausePlayback();
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
synchronized(focusLock) {
resumeOnFocusGain = true;
playbackDelayed = false;
}
pausePlayback();
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
// ... pausing or ducking depends on your app
break;
}
}
}
Hope this helps! Also you can check Android's official documentation. If this doesn't help, you can check this site and this site for more documentation.

To play audio: Audio Track reference
To play video: Media Player reference
And now you could start on the main thread by showing in a Video View the video you want, and when is the time to play the sound you start playing the Audio Track. The tricky part will be to syncronize the audio with the video

Related

Pause all the other media players and audios while Application media player starts playing

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)

Stopping background music player services like Soundcloud and Saavn using code

When I play 'Soundcloud' or 'Saavn' music playing app and I start my own app, the music in Saavn or Soundcloud is still playing in background. Need a way to stop it using code. How to do it ?
I tried this -
AudioManager mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
String SERVICECMD = "com.android.music.musicservicecommand";
String CMDNAME = "command";
String CMDSTOP = "stop";
if(mAudioManager.isMusicActive()) {
Intent i = new Intent(SERVICECMD);
i.putExtra(CMDNAME , CMDSTOP );
HomeActivity.this.sendBroadcast(i);
}
but it is only useful in stopping the music if Android's default music player is playing in background and not 3rd party apps like Saavn and Soundcloud.
One possible solution I found on this is using the AudioManager.requestAudioFocus(...) function
AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
Code example:
int result = am.requestAudioFocus(focusChangeListener, //Request audio focus for playback
AudioManager.STREAM_MUSIC, //Use the music stream.
AudioManager.AUDIOFOCUS_GAIN); //Request permanent focus.
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
// other app had stopped playing song now , so u can do u stuff now .
}
Audio focus is assigned in turn to each application that requests it. This means that if another application requests audio focus, your's will lose it.
The app is notified when there is a change on the AudioFocus, through the onAudioFocusChange event listener. This listener is the first parameter in the requestAudioFocus function.
This listener should look something like:
private OnAudioFocusChangeListener focusChangeListener = new OnAudioFocusChangeListener() {
public void onAudioFocusChange(int focusChange) {
AudioManager am =(AudioManager)getSystemService(Context.AUDIO_SERVICE);
switch (focusChange) {
case (AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK):
//Lower the volume while ducking (not sure what ducking means really)
break;
case (AudioManager.AUDIOFOCUS_LOSS_TRANSIENT) :
//TODO: pause audio
break;
case (AudioManager.AUDIOFOCUS_LOSS) :
//TODO: pause audio
break;
case (AudioManager.AUDIOFOCUS_GAIN):
//TODO: Return the volume to normal and resume if paused.
break;
default: break; //empty on default
}
}
};
I think it is not possible to stop a service of another app as you may not know the processid, You can stop the service of your app only if you know the Pid of the app using
android.os.Process.killProcess(processIdKillService)

How Do I Make My Music App ( MediaPlayer ) Work With External Equalizers like DSP Manager?

I'm currently working on an update for my music app, and I can't seem to make it compatible with external equalizers (specifically DSP Manager), similar to how Google Play Music and Apollo are. I've been Googling for the past hour but I can not seem to find anything. I think it may have something to do with my audio session id, but It did not seem to do anything.
This is how I initialize my Service that contains my MediaPlayer object, and help or tips are appreciated, thanks!
#Override
public void onCreate()
{
// create the service
super.onCreate();
// create mediaplayer, set wake lock and
bgmusic = new MediaPlayer();
bgmusic.setAudioStreamType(AudioManager.STREAM_MUSIC);
// No Wakelock might need it if music is dropping, dont forget to re-add
// permission
// bgmusic.setWakeMode(this.getApplicationContext(),
// PowerManager.PARTIAL_WAKE_LOCK);
playList = new ArrayList<File>();
index = 0;
volume = 1;
fading = false;
isPaused = false;
loopBool = false;
}

Stop a Mediaplayer that was started from another application

How do applications like Soundcloud stop another app's MediaPlayer? For example, when I am playing a song through Google Music and I open SoundCloud, the song from Google Music stops playing as soon as the Soundcloud song starts. I might be missing something obvious but I have always wondered how this is achieved.
Thanks,
Justin
Use Audio Focus. See Request the Audio Focus
AudioManager am = mContext.getSystemService(Context.AUDIO_SERVICE);
...
// Request audio focus for playback
int result = am.requestAudioFocus(afChangeListener,
// Use the music stream.
AudioManager.STREAM_MUSIC,
// Request permanent focus.
AudioManager.AUDIOFOCUS_GAIN);
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
am.unregisterMediaButtonEventReceiver(RemoteControlReceiver);
// Start playback.
}
Adding on to #TCA's answer:
There is only one AudioManager. So, Audio focus is assigned to each application that requests it one by one. This means that if another application requests audio focus, your application will lose it. You will be notified of the loss of audio focus through the onAudioFocusChange handler of the Audio Focus Change Listener (afChangeListener in TCA's answer) you registered when requesting the audio focus.
private OnAudioFocusChangeListener focusChangeListener =
new OnAudioFocusChangeListener() {
public void onAudioFocusChange(int focusChange) {
AudioManager am =(AudioManager)getSystemService(Context.AUDIO_SERVICE);
switch (focusChange) {
case (AudioManager.AUDIOFOCUS_LOSS_TRANSIENT) :
//Transient audio loss
break;
case (AudioManager.AUDIOFOCUS_LOSS) :
//Audio focus loss. Stop playback.
break;
case (AudioManager.AUDIOFOCUS_GAIN) :
//Audio focus gain. Start playback.
break;
default: break;
}
}
};

Control the playback speed of video in android

I am using a VideoView to play a video file kept in res/raw. I couldnt find a way to control the playback speed of the video. Basically i want to reduce and increase the playback while moving a scroll bar. Is there any work around for implementing this?
you can use this but it works on api 23 and above
mVideo.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
//works only from api 23
PlaybackParams myPlayBackParams = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
myPlayBackParams = new PlaybackParams();
myPlayBackParams.setSpeed(0.8f); //you can set speed here
mp.setPlaybackParams(myPlayBackParams);
}
}
});
No, you cannot change the playback rate by simply using VideoView. VideoView and MediaPlayer only provide limited media functions.
You have to use some third party library, e.g., PVPlayer, and implement that yourself.
That's also why good media players on Android are so valuable:)
I want to say than Mk Kamal's solution have an unexpected side effect: calling setPlaybackParams in OnPreparedListener will force VideoView to repeat the latest played video when the app was returned from the background.
I don't know is it a bug or a feature, but I found a way to avoid such behavior:
private float speed = 0.8f;
private final MediaPlayer.OnInfoListener listener = (mp, what, extra) -> {
if (what == MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START) {
mp.setPlaybackParams(mp.getPlaybackParams().setSpeed(speed));
return true;
}
return false;
};
videoView.setOnPreparedListener(
mp -> {
mp.setOnInfoListener(listener);
}
);
MEDIA_INFO_VIDEO_RENDERING_START will be sent only if the palyer was already started.
And I want to emphasize that getPlaybackParams is annotated as #NonNull, so it's not necessary to create a new PlaybackParams object.
Kotlin variant, API above 23
val playerView = itemView.findViewById<VideoView>(R.id.videoview)
playerView.setVideoURI(Uri.parse("android.resource://" + context.packageName + "/" + R.raw.123.mp4))
playerView.setOnPreparedListener { mediaPlayer ->
playerView.seekTo(1) // for video preview
mediaPlayer.playbackParams = mediaPlayer.playbackParams.apply {
speed = 0.6f
}
playerView.start()
}
DicePlayer works perfectly on my Asus Transformer.
It has a speed control onscreen display.
I'm not sure what res/raw is though.

Categories

Resources