I'm using a VideoView in my Android app to display the intro animation.
If the Google Music App is playing music in the background, calling videoview.start() stops music playing in Google Music App in the background.
Is there a way to make sure any music in the background will keep playing at the same time with my intro video? (it has no audio)
Thank you!
Turns out Google Music App and a few other apps will stop their music when any video starts playing.
In order to make sure I'm not interrupting the listening experience for my users I now skip the intro video if I determine that there is music playing in the background.
To do this:
AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
if (am.isMusicActive()) {
loadApp(); // skip video and go straight to the app
}
else {
videoView.start(); // play video
}
Using both the answers previously given, here is a solution that will resume music after your video ends:
final boolean music_was_playing = ((AudioManager) getSystemService(Context.AUDIO_SERVICE)).isMusicActive();
VideoView vv_Video = (VideoView) findViewById(R.id.intro_video_view);
// play the intro video
vv_Video.setOnCompletionListener( new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer m) {
// resume music if it was playing cause our intro video just paused it temporarily
if (music_was_playing) {
Intent i = new Intent("com.android.music.musicservicecommand");
i.putExtra("command", "play");
sendBroadcast(i);
}
// go to main menu
startActivity(new Intent(IntroActivity.this, MainMenuActivity.class));
}
});
Taken from openVideo() in VideoView.java
Intent i = new Intent("com.android.music.musicservicecommand");
i.putExtra("command", "pause");
mContext.sendBroadcast(i);
Related
I am brand new (47 minutes) into Android development and have a problem that I have is likely to be a simple one.
Anyway, here it is.
I would like to use a web service which, given a url, spits back an Mp3 (browser instantly starts downloading it).
MediaPlayer player = new MediaPlayer();
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 20, 0);
String url = "http://www.gimme-an-mp3.com/xyz";
try {
player.setDataSource(url);
} catch (IOException e) {
e.printStackTrace();
}
player.prepareAsync();
player.start();
For some reason, the media player does not play my Mp3 file. Why? Do I have to first download it and then pass it to the media player?
you have to call player.start() only after player has been prepared.
before that, the player is still preparing.
player.prepareAsync();
player.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
mediaPlayer.start();
}
});
more details can be found here http://developer.android.com/reference/android/media/MediaPlayer.html
and here Android: correct usage of PrepareAsync() in media player activity
My app runs as a service in background and can give notifications based on some events. Currently, to play notification files, I use MediaPlayer in my app :
MediaPlayer mPlay = MediaPlayer.create(this, R.raw.sound);
mPlay.start();
But, if a song is already playing on the device (Music player, radio etc), and my notification plays when receiving the event, both the sounds (Music audio & notification audio) is heard in parallel.
So, is there a way that I can suppress the system audio volume so that my notification is heard clearly and once notification audio is over, the system audio can playback again?
Thanks for any help
I got the answer to this question in the following link:
http://developer.android.com/training/managing-audio/audio-focus.html
We need to request for audio focus (transient/permanent) and then can play the app audio:
AudioManager am = (AudioManager) 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_TRANSIENT);
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
// Start playback.
Log.i(TAG, "...audiofocus granted....");
MediaPlayer mPlay = MediaPlayer.create(this, R.raw.sound);
mPlay.start();
}
OnAudioFocusChangeListener afChangeListener = new OnAudioFocusChangeListener() {
public void onAudioFocusChange(int focusChange) {
if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT) {
// Pause playback
Log.i(TAG, "....audiofocus loss transient in listener......");
} else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
// Resume playback
Log.i(TAG, "....audiofocus gain in listener......");
} else if (focusChange == AudioManager.AUDIOFOCUS_LOSS) {
Log.i(TAG, "....audiofocus loss in listener......");
//am.unregisterMediaButtonEventReceiver(RemoteControlReceiver);
am.abandonAudioFocus(afChangeListener);
// Stop playback
}
}
};
Hope it helps someone.
I sometimes (not every time) get this warning "Media player finalized before being released" in LogCat, and the sound is played only partially (cuts off).
I have a service CountdownService, and there I have:
MyApplication appActivity = (MyApplication)this.getApplication();
appActivity.playCountdownComplete();
stopSelf(); // I absolutely must stop the service at this point
And in the MyApplication (extends Application) activity I have:
public void playCountdownComplete() {
MediaPlayer mp = MediaPlayer
.create(getApplicationContext(), R.raw.sound_complete);
mp.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
mp.start();
}
I absolutely must stop the service at the point where I call stopSelf(); because my app depends on the service not running, so please do not offer a solution that changes that.
Why doesn't the sound play completely? - the application activity is playing the sound, so even if the service is stopped, it shouldn't matter, right?
EDIT: I also tried having the service send out a broadcast and the Application activity receiving it and playing the sound there, but still the same thing happens.
In my app, I use the following code to play a short notification sound using MediaPlayer with STREAM_NOTIFICATION. The issue is that, when the app plays the notification sound and at the same time music is played at background by music player app, the notification sound will interrupt (pause) the music playing. How can I make my app's notification sound to be played simultaneously with the background music playback? Thanks.
AssetFileDescriptor afd =
mResources.openRawResourceFd(resId);
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
afd.close();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
mediaPlayer.release();
}
});
mediaPlayer.prepare();
mediaPlayer.start();
The behaviour described in the question seems to be specific for Android 5.x and below. For Android 6.x and above AudioManager behavior has changed and AudioSystem.STREAM_NOTIFICATION flag doesn't interrupt music playback anymore.
As workaround I would recommend to replace
mediaPlayer.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);
by
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
P.S. Not sure what exactly changed in Android platform but according to
https://android.googlesource.com/platform/frameworks/base/+/android-5.1.1_r37/media/java/android/media/AudioAttributes.java and https://android.googlesource.com/platform/frameworks/base/+/android-6.0.0_r26/media/java/android/media/AudioAttributes.java they treat AudioSystem.STREAM_NOTIFICATION as AudioSystem.STREAM_SYSTEM for both Android 5.x & 6.x:
public Builder setInternalLegacyStreamType(int streamType) {
switch(streamType) {
/*...*/
case AudioSystem.STREAM_SYSTEM:
mContentType = CONTENT_TYPE_SONIFICATION;
break;
/*...*/
case AudioSystem.STREAM_NOTIFICATION:
mContentType = CONTENT_TYPE_SONIFICATION;
break;
/*...*/
default:
Log.e(TAG, "Invalid stream type " + streamType + " for AudioAttributes");
}
mUsage = usageForLegacyStreamType(streamType);
return this;
}
I am developing an audio record application in android. So if any background music is already playing in the device music player, that should be paused before it starts recording and the background music should resume whenever the recording is either stopped or paused. And the same should work while playing the recorded audio. Can anybody help me out to get through this scenario? Thanks in advance..:)
get audioManager and check
audioManager.isMusicActive()
if true
private void toggleNativePlayer(Context context) {
Intent intent = new Intent("com.android.music.musicservicecommand");
intent.putExtra("command", "togglepause");
context.sendBroadcast(intent);
}
and after you finish recording run this code again to start play musig again
You can stop other applications from playing music, by requesting AudioFocus. When you are granted AudioFocus, other apps stop playing music and then you can play/record as per your needs.
AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
// Request audio focus for playback
int result = am.requestAudioFocus(focusChangeListener,
// Use the music stream.
AudioManager.STREAM_MUSIC,
// Request permanent focus.
AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
// other app had stopped playing song now , so you can start recording.
}
But, 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) 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) :
//Lost focus. Stop recording
break;
case (AudioManager.AUDIOFOCUS_GAIN) :
//Gained AudioFocus. Start recording tasks
break;
default: break;
}
}
};