Android mediaPlayer asynchronously repeat - android

I am a little confused on how to manage the mediaplayer functions.
I want a sound to repeat for a designated number of seconds. It is intended to be very attention getting. The problem is that I don't know how to make the sound stop. I see that there are Async functions in the MediaPlayer but I don't get it. Should I just do my own Async or CountdownTimer that stops the sounds at the end of the timer?
MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.telephone_ring_3);
mediaPlayer.setLooping(true);
mediaPlayer.setVolume(1.0f, 1.0f);
mediaPlayer.start();
Insight or best practices appreciated

If there is not some other user initiated event that stops the sound, then yes, I would probably go with a CountdownTimer.

Related

Why mediaPlayer plays 15 minutes and then stop?

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();
}

Why i can not create media player in the android alarm manager onReceive method?

I want want application to start every 10 second play a sound alarm,i'm use the android alarm manager,every thing is good,but in the this line:
player = MediaPlayer.create(this, R.raw.music);
i want create media player,up line get error this parameter i show error this picture:
How can i solve that?thanks.
Replace this with context. Although, creating MediaPlayer in a broadcast receiver is not a good way. Because onReceive should return ASAP.

How to start a media player object in a activity, and then use that same object and another activity?

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.

Detecting stop event of android media player

I want to detect the stop event of android music player.
I am actually calling a method when the file stops playing.
There is an isPlaying method, but in order to get the stop event, I need to put it inside a while loop which might induce instability?
Is there an elegant way to do this?
This is the code I have:
mp.start(); // mp is the media player object
while (mp.isPlaying() == true) continue;
handler.sendEmptyMessage(0);
Thank you.
MediaPlayer has an OnCompletionListener callback you can register to get notified when playback stops.

How to check something at regular intervals?

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.

Categories

Resources