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>
Related
My android application has two buttons. Each of them will play a sound of size 1.5 seconds if clicked. I have used MediaPlayer to achieve this. The problem is when i click one button and press another within less the 1.5 sec, the second sound wont play . My requirement is when i press the second button I want the first sound to be stopped and the second sound should start playing. How do i achieve this.
When you press the 2nd button and the music is still playing, you need to stop that music and then start playing the 2nd button's song.
so add this to your button
if (mPlayer.isPlaying()) {
mPlayer.stop();
mPlayer.reset();
mPlayer.release();
mPlayer = null;
}
mPlayer = MediaPlayer.create(this, YourMusicHere);
mPlayer.start();
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 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 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.
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..