when I lock my Android Screen,my app is still playing music(it means the mediaPlayer is working),but after 15 minutes,the music will be stoped,dou you know why?
(I did not use Service or BindService at all,just Use MediaPlayer Class)
Yes it happens, why?
because I think you are using MediaPlayer in Activity, and android release some untouched resources after some time to give space to other processes.
you have to use foreground Service to prevent this problem.
Here is example of MediaPlayer inside Service.
https://stackoverflow.com/a/8209975/6676466
Release the player memory by using onDestroy() method
#Override
public void onDestroy() {
if (mp!= null) mp.release();
}
Related
Example:
Activity 1:
main screen.
player = new media player()
player.start() //the sound began
now i have to equalize this same sound in another Activity...
Activity 2:
edition screen
the sound keeps playing and want to stop
example:
player.setVolume(0.0)
player.stop()
thank you
Declare player as public static in screen1
then you can access this media player in screen 2
like screen1
:
public static MediaPlayer player;
player=new MediaPlayer();
=================
===========
write your code
Screen 2 ::-
if you want to use media player in screen 2 use this code ::-
screen1.player.start(); screen1.player.stop();
You must create Service. Service to host the MediaPlayer and have your Activities communicate with the Service to play and stop songs. Don't forget to call release on the MediaPlayer when you are done. Bind the activity to service
For the sample Equalizer sample. The sample is not integrated with Service it just a seperate unit.
Obtain the sessionid of the MediaPlayer and pass it to equalizer.
Usually when we are using MediaPlayer, as playing music in itself doesnt really require to have a graphical interface, we usually use a service because only the sound resulting by the playback is needed.
- Create the mediaplayer in a service
- Send somes request to the Service after binding to it, or even by sending broadcasts to it so that it can play, stop, pause, set volume whatever you want.
I'm writing an Android alarm app. I can get the activity to come up all right when the alarm is triggered (it wakes the phone up, turns off the keyguard and shows the alarm view), but I can't for the life of me get the alarm to sound if the device is in sleep mode when the alarm goes off. (It does sound perfectly when the device is awake and the keyguard is on.) I am using a wake lock. I've tried using the MediaPlayer and the SoundPool with no success. Is there some kind of permission that I'm missing? (I already have WAKE_LOCK, DISABLE_KEYGUARD, and RECEIVE_BOOT_COMPLETED permissions.)
When debugging using the SoundPool I perform the load which returns a valid sound ID integer (1), but the onLoadComplete listener (this is where the sound gets played) is never fired. It fires just fine when the device is awake.
Anyone out there have any ideas or have run into the same problem?
Found the solution! I had the call to play the audio in the onCreate() method of the class. I moved it to onResume() as this is when you know the device is fully awake and the activity is visible, on top and in focus.
I recently encountered the same problem. The way I solved this was reading your answer, but also checking the activity flow by logging what was happening. Basically onCreate, onStart, onResume, onPause, onStop, onStart, onResume was being fired in that order. This was an activity that was started as an alarm screen.
I kept the initialization code in the onStart method, making sure to use .prepare() instead of prepareAsync() since I'm using local sounds.
try {
Log.d(LOG_TAG, "Setting media player URI: " + alarmTone.toString());
//mMediaPlayer = MediaPlayer.create(this, );
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setDataSource(getApplicationContext(), alarmTone);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
mMediaPlayer.setLooping(true);
mMediaPlayer.setVolume(100, 100);
mMediaPlayer.prepare();
} catch (Exception ex) {
Log.d(LOG_TAG, "Exception from media player: " + ex.getMessage());
}
Then inside the onPause and onStop methods, I added:
if (mMediaPlayer != null && mMediaPlayer.isPlaying()) {
mMediaPlayer.pause();
}
Inside the onResume method I added the media playing code:
if (mMediaPlayer != null && !mMediaPlayer.isPlaying()) {
Log.d(LOG_TAG, "Playing alarm through Media Player");
mMediaPlayer.start();
}
Huzzah, it works!
My android app creates a MediaPlayer() and plays a looping song. I need to have it stop playing when the user leaves the app. I also need to get at the volume buttons somehow, to let users adjust the songs volume... Any Ideas?
MediaPlayer mp;
public void setupMediaPlayer()
{
mp = MediaPlayer.create(context, R.raw.song);
mp.setLooping(true);
mp.start();
}
public void stopMediaPlayer()
{
mp.stop();
}
Per the first half of your question: you should get what you want if you call stopMediaPlayer() inside onPause() and onDestroy(). Example:
#Override
protected void onPause() {
super.onPause();
stopMediaPlayer();
}
Per the second half: Try taking a look at the AudioManager class (particularly AUDIO_FOCUS_GAIN), and see if that can handle what you're looking for.
Make sure the looping audio makes sense in the context of the app, though...if there's one thing I don't miss from the amateur websites of the mid-90's it's that awful MIDI background music that everyone seemed to put in them...
Also think about onResume(), to continue your looping song when the user comes back to your app.
stopMediaPlayer didn't work for me as per eldarerathis' answer but this did:
#Override
protected void onPause() {
super.onPause();
releaseMediaPlayer();
}
Dunno why though...
Edited:
According to the course I'm doing though this is the correct way to do it.
Maybe one day I'll figure out the difference. :)
Beginner here, I have a simple question.
In Android what would be the best what to check for something at regular intervals?
Please bear with me, I'll try to explain the best I can --
For example my audio app is very simple, a main activity and a service. The main activity has a UI with two buttons, start and stop audio. I press start and the audio service starts. Likewise when I click Stop the service stops and the audio ends. If isLooping() is hard-coded to true there is no issue because the audio never ends unless I hit stop button, which stops the audio service and also resets the button states.
This is an issue now because I set isLooping() to false so the audio doesn't loop. So the audio will stop playing but the service is still running.
I want to be able to detect when the audio stops so I can set the states of the UI buttons. So I need something that is always checking whether audio is playing (i.e. check player.isPlaying() so I can end the service and set the enable/disable state of the buttons.
I figured out binding to the service so I can access the MediaPlayer controls via my main activity so I know the code to check if it's playing, but WHERE do I put this code so it's checked all the time?
Am I making sense? I know this is probably very simple. Thanks for any help.
You can repeat it with the TimerTask and Timer. Code below:
public final void RepeatSoundFunction(){
t = new Timer();
tt = new TimerTask() {
#Override
public void run() {
mp.seekTo(0); //Reset sound to beginning position
mp.start(); //Start the sound
t.purge(); //Purge the sound
}
};
t.schedule(tt, 10*1000); //Schedule to run tt (TimerTask) again after 10 seconds
}
then you set a MediaPlayer onCompletionListener and in there you put this.
Inside the run-code you can check for other things than
music, I just show an example with the audio.
How to play an audio during splash screen.
Guidance needed.
My way to do this (no external sound needed, since I put my soundfile in my resources-folder):
In onCreate:
mp = MediaPlayer.create(getBaseContext(), R.raw.sound); /*Gets your
soundfile from res/raw/sound.ogg */
mp.start(); //Starts your sound
//Continue with your run/thread-code here
Remember to have the sound in .ogg-format; it's fully supported in Android.
An important thing below about handling the sound when the Splash Screen activity is stopped:
There are two general ways to manage the Splash Screen (and the sound inside it) when it's stopped:
Destroy the whole activity:
protected void onStop() {
super.onStop();
ur.removeCallbacks(myRunnable); /*If the application is stopped;
remove the callback, so the next time the
application starts it shows the Splash Screen again, and also, so the
thread-code,
don't continue after the application has stopped */
finish();
onDestroy();
}
Or you can just stop the sound in onStop:
protected void onStop() {
super.onStop();
if(mp.isPlaying()){ //Must check if it's playing, otherwise it may be a NPE
mp.pause(); //Pauses the sound
ur.removeCallbacks(myRunnable);
}
}
If you choose the second alternative you also have to start your Callback and MediaPlayer in the onStart-method.
I prefer the first alternative.
You can play audio files using the MediaPlayer class.
Example
MediaPlayer player = new MediaPlayer();
player.setDataSource("/sdcard/audiotrack.mp3");
player.prepare();
player.start();