I have develop a audio recorder which is working perfectly in foreground as well as background.But there is a problem when i am running audio recorder in background and start another app which used the sound(like video recorder, music player, video player etc) so the app will crash. So i want to add a functionality in audio recorder that when the other app (like video recorder, music player, video player etc) which are using microphone or speaker are running in foreground so audio recorder automatically go to the pause state.Thank in advance.
If u are still not understand my problem so please used your mobile and play the music player than go to the home. So the music player is running.And open the another app which are not using mic & speaker so music player is still running but when u r open another app which are using mic & speaker(like video recorder, music player, video player etc) so music player will pause. This type functionality i wanted.Thanx in advance
Finally lot of search & googling i have got the answer of this question. I am using audiofocuschangelistener. The code is there
AudioManager.OnAudioFocusChangeListener audioFocusChangeListener = new AudioManager.OnAudioFocusChangeListener() {
public void onAudioFocusChange(int focusChange) {
switch(focusChange)
{
case AudioManager.AUDIOFOCUS_GAIN:
case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT:
case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK:
//resume your activity
break;
case AudioManager.AUDIOFOCUS_LOSS:
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
///pause your activity
break;
}
}
};
and do this in onCreate()
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
int result = audioManager.requestAudioFocus(audioFocusChangeListener, AudioManager.STREAM_MUSIC,AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
//start your activity
}
And for more information u can follow below link
http://developer.android.com/training/managing-audio/audio-focus.html
As far as I understood from your question, you need your application to stop whatever it's doing (sound recording probably?) when it's not the foreground application.
You need to override onPause method in your main activity so that when your activity goes into the background (the new activity comes) you can stop your recorder.
Furthermore, you can resume your activity's task when user starts interacting with your application again by overriding onResume method.
Related
I got a bug on my MediaPlayer.
When i click on an item of my playlist, it create a mp3Player. If i press back the music play in background (all is normal). But if i choose another song from my app, the app create a new mediaplayer:
Then i got 2 music playing !
I tried to use "singleTask", "singleInstance", and it's not working.
I tried "intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);"
None of this work!
Maybe if i could resume the previous Activity, i could destroy it and then create a nex mediaplayer no?
Can someone help please? :D
You play music in backround it means you use Service ?
Problem is you need reconnect with Service (Service play music).
How to reconnect service android
Or try. Media Playback , EG: Universal Android Music Player Sample
you can simply make your activity android:launchMode="singleInstance" in manifest.
and then in your activity overide below method
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
// do what you want here
}
in above method, you can reset value of media player
I'm creating a simple app that plays and stops music on button click. For the first time, when I click start music button, the music plays and stops when clicking it again. The problem is that when I click it again, it starts playing and doesn't stop on the next click anymore. Instead, it plays same music again in parallel (e.g. however many times I click, that many tracks are played in parallel).
The code for the button XML:
<Button
android:id="#+id/button3"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/textView1"
android:layout_toRightOf="#+id/imageView1"
android:background="#7FFF00"
android:text="Start Music"
android:onClick="music" />
and MainActivity.java
public void music(View v)
{
Button bu= (Button)findViewById(R.id.button3);
String ans=bu.getText().toString();
MediaPlayer mp = MediaPlayer.create(this, R.raw.my);
switch(ans)
{
case "Stop Music":
mp.setLooping(false);
mp.stop();
bu.setText("Start Music");
break;
case "Start Music":
mp.setLooping(true);
mp.start();
bu.setText("Stop Music");
break;
default:
break;
}
}
You are creating a new MediaPlayer instance on every button click. Just create it once in your activity's onCreate() and refer to that single instance in your music() method.
Remember to call mp.release() as well, when you exit your activity.
The above Image shows various states of MediaPlayer. The Key thing you need to notice here is that After the Starting the music (Start State) when you stop the music it goes to Prepared State. Now if You want to play your song again, You need to prepare() it first.
Hence before playing the music:
mp.reset();
mp.prepare();
mp.start();
Hence When you hit the play button after the stop button! It will prepare() then play(). reset() is simply resetting the song to start from beginning.
I want to play sound in my android app. And maybe I have to use service for this. But from this question I see on home button pressed and on screen lock onPause method called. But in case of home button pressed I want to stop sound in case of screen lock i dont want to stop sound. So how to do this and how to use onPause method in this case?
Please someone help me. Thanks...
Use MediaPlayer from Android. See: Android Example
MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.sound_file_1);
mediaPlayer.start();
I already have succeeded playing, pausing and stopping music in Android. The problem is that when I want to resume my music, if I press pause button and play, it will start the music from the beginning.
If you are using the android mediaplayer class.. the
mediaplayer.pause();<-- will pause the player
and
mediaplayer.start();<-- will resume the player from the point it was paused...
Here is the link to phonegap 1.5 Media API.
http://docs.phonegap.com/en/1.5.0/phonegap_media_media.md.html#media.pause
Hope this help
~K
<pre><code>
public void pauseAndResume() {
if (_mediaPlayer != null && _mediaPlayer.isPlaying()) {
_mediaPlayer.pause();
}else{
_mediaPlayer.start();
}
}
</code></pre>
I have a buttons called play and stop
if I press play its starting playing. after I press stop its going to home page. But I need to go home page automatically once playing is done ie. Without clicking stop button. How can I do it for android?
thanks in advance
hi shekhar you can set an oncomplete listener to the player object. The code follows:
mPlayer.setOnCompletionListener(new OnCompletionListener(){#Override public void onCompletion(MediaPlayer mp) { "your code comes here" }});
here the mPlayer is the object of the MediaPlayer which is currently running..