i create app including sound player but when i minimize the app or close it the song still working so how to exterminate sound after minimize or exiting app
here is the code
player = MediaPlayer.create(this, R.raw.emotional);
end = (Button) findViewById(R.id.button12);
end.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
player.stop();
}
});
I think you have to stop playing sound when the activity stop.
Try adding
#Override
protected void onStop()
{
if (player != null)
player.stop();
super.onStop();
}
Brute-force method is to kill the process (your application):
int yourId = android.os.Process.myPid();
android.os.Process.killProcess(pid);
A bit more subtle solution is to start the music on a separate thread and use this code in your onPause() method:
Intent musicIntent = new Intent(getApplicationContext(), MusicClass.class);
getApplicationContext().stopService(musicIntent);
musicThread.stop();
Replace MusicClass.class with the name of the class in which you play your music, similarily, replace musicThread with the name of the thread you started to play the music.
Related
// This is my app exit button it destroys the activity and exits the user from an app. I want to //play a sound when I click this button but unfortunately, the sound is not playing and the activity //destroys. Please help me in solving this issue. Except for this button, all my button sound is working.
// This is my second activity, in this activity, I have used a button for quitting the app but I also //want the button to play a sound when the button is clicked by user. But it is not playing the sound and //quits the app which destroys activity. please tell me how to play the sound on my exit button during //the click
mExitApp = (Button)findViewById(R.id.exit_app);
final MediaPlayer mp = MediaPlayer.create(this, R.raw.sample2);
mExitApp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mp.start();
ScoreActivity.this.finish();
System.exit(0);
}
});
A quick and dirty way would be to wait for the sound to end playing before finishing the activity/process.
public void onClick(View v) {
mp.start();
final Handler handler = new Handler(Looper.getMainLooper());
handler.postDelayed(new Runnable() {
#Override
public void run() {
ScoreActivity.this.finish();
System.exit(0);
}
}, 300);
}
A more cleaner solution would probably involve a MediaPlayer.OnCompletionListener.
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
finish(); // finish current activity
}
});
Or you could play the sound in a foreground/background service. But then you shouldn't kill the entire process (System.exit(0)) like you do now ofc.
In my project I am trying to make a media player which plays a shoutcast stream. Everything seemed to be working well until I pressed the back button on my device, which I think stops the activity and causes the device to recreate the activity when launched again. The problem is , when the activity is recreated , I lose the control of the mediaplayer and a new mediaplayer is created.
I need to be able to have the mediaplayer's control back at that point. How is it possible?
This part of code belongs to onCreate
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button);
if (mediaPlayer == null){
mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource(getString(R.string.yayin));
} catch (Exception e) {
e.printStackTrace();
}
mediaPlayer.prepareAsync();
}
if(!isPlaying){
btn.setBackgroundResource(R.drawable.oynat);
}
else{
btn.setBackgroundResource(R.drawable.durdur);
}
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(!isPlaying){
playOnReady();
isPlaying = true;
btn.setBackgroundResource(R.drawable.durdur);
}
else{
mediaPlayer.reset();
isPlaying = false;
btn.setBackgroundResource(R.drawable.oynat);
}
}
});
This part of code belongs to the function playOnReady()
private void playOnReady(){
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
}
Take a look at the Android Activity lifecycle flowchart: https://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle
You need to account for the path where onPause is called when you leave the Activity and then onResume is called when you enter it again. The solution for you could be as simple as moving some/all of your code from onCreate into onResume.
Using this tutorial I have managed to build a service which handles the mediaplayer that I can have the control of anytime I need.
I've a strange problem with my app.
Every time a button is clicked, I call a method to execute sound.
The code:
public static void executeSound(Context context) {
if (isSoundEnabled(context)) {
if (mediaPlayer == null) {
mediaPlayer = MediaPlayer.create(context, R.raw.button_click);
}
mediaPlayer.start();
}
}
Where isSoundEnabled function verified if user has sound enabled inside app.
Sound is not executed each time I tap button, but after I tap other buttons, and sounds are executed all together.
So happen that I press three different buttons, and three sounds are executed only when I push a button for third time. How can I execute immediately the sound?
I have this problem on lollipop devices (the same code run perfectly on the same device but with kitkat)
Change your execute method to this
public static void executeSound(Context context) {
if (isSoundEnabled(context)) {
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(mContext, audioUriPath);
mediaPlayer.prepare();
mediaPlayer.start();
mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.release();
}
});
}
}
I am writing an app in which i am allowing user to view images and select one of them to set an WALLPAPER, and in this i also want to play an mp3 when user starts an App and stop that mp3 when user close application
I have stored an MP3 Music file in res/raw folder namely : mymusic.mp3
I know how to play and stop MP3 music by using click on button, but don't know how to play mp3 in background continuosly, play when user start an app and stop when user close an app.
Please someone help me, its much needed any suggestion, sample code would be helpful...
MediaPlayer mPlayer;
MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.music_file);
Button buttonPlay;
Button buttonStop;
buttonPlay = (Button) findViewById(R.id.play);
buttonPlay.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
mPlayer = MediaPlayer.create(getApplicationContext(),R.raw.mymusic.mp3);
mPlayer.start();//Start playing the music
}
});
buttonStop = (Button) findViewById(R.id.stop);
buttonStop.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(mPlayer!=null && mPlayer.isPlaying()){//If music is playing already
mPlayer.stop();//Stop playing the music
}
}
});
This part has to be in EVERY activity's onPause:
Stop music automatically when user exit from app
public void onPause(){
super.onPause();
Context context = getApplicationContext();
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> taskInfo = am.getRunningTasks(1);
if (!taskInfo.isEmpty()) {
ComponentName topActivity = taskInfo.get(0).topActivity;
if (!topActivity.getPackageName().equals(context.getPackageName())) {
StopPlayer();
Toast.makeText(xYourClassNamex.this, "YOU LEFT YOUR APP. MUSIC STOP", Toast.LENGTH_SHORT).show();
}
}
}
This part has to be in EVERY activity's onResume:
Play music automatically when user resume the app
Public void onResume()
{
super.onResume();
StartPlayer();
}
You can put your Player functionalty in global class. where every class can call it's player. so your plyer will be remain same in whole application. & you can start or stop it. on Pause method it will detect wether user left this App or not. if user left from the app so u can stop it.
GlobalPlayer.class
public MediaPlayer mPlayer;
public void StartPlayer(){
MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.music_file);
// TODO Auto-generated method stub
mPlayer = MediaPlayer.create(getApplicationContext(),R.raw.mymusic.mp3);
mPlayer.start();//Start playing the music
}
public void StopPlayer(){
if(mPlayer!=null && mPlayer.isPlaying()){//If music is playing already
mPlayer.stop();//Stop playing the music
}
}
simply put your player.start() method in onResume() method and call player.stop ()
in onPause() method.Take a look at this
Difference between onStart() and onResume()
create service and put Mp3 start code in OnstartCommand(its a service class method you can override it) and whenever your you want to start mp3 just create Intent object and call StartService method and call StopSerice where you want to stop mp3 in application.
I made a basic app with 2 buttons, start and stop. When I start the app and hit the start button the sound starts, and when I the end button it stops, BUT if I try to start up again with the start button it doesn't start again.
Code:
buttonStart = (Button)findViewById(R.id.ButtonStart);
buttonStart.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
onClickButton(buttonStart);
}
});
buttonEnd = (Button)findViewById(R.id.ButtonEnd);
buttonEnd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
onClickButton(buttonEnd);
}
});
beat = MediaPlayer.create(this, R.raw.beat);
public void onClickButton(Button button){
if(button == buttonStart){
beat.start();
beat.setLooping(true);
}
else if(button == buttonEnd){
beat.stop();
//beat.setLooping(false);
}
}
if you stop the media player then the instance of the media player is destroyed so if you want to play again. Then you have to create the instance of media player again. Put this code in your buttonStart
beat = MediaPlayer.create(this, R.raw.beat);
beat.start();
beat.setLooping(true);
Best of luck and don't forget to tick that this answer is useful to you.