press home or back music stops - android

Hi In My Application I am playing one audio if i click back or home button that window should stop i am new to android please help me
final MediaPlayer mp = new MediaPlayer();
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent nextScreen = new Intent(getApplicationContext(), Demovote2.class);
startActivity(nextScreen);
mp.start();
finish();
}
});
}

you should stop it on onpause method and replay it on onresume,
#Override
protected void onPause() {
mp.stop();
super.onPause();
}
#Override
public void onResume(){
// put your code here...
mp.start();
super.onPause();
}

Related

how to make mediaplayer is playing even after exit application

whenever i click back or click home the sound is stop.. how to make the sound is still playing even when i click home or back button.. sorry for my english.. thank you
here is my main_activity code
public class Child extends Activity{
private static final String isPlaying = "Media is Playing";
private MediaPlayer player;
private Button jakartaButton;
private Button acehButton;
private Button jogjaButton;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.child);
// Get the button from the view
jakartaButton = (Button) this.findViewById(R.id.jakarta);
jakartaButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
playSound(1);
}
});
acehButton = (Button) this.findViewById(R.id.aceh);
acehButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
playSound(2);
}
});
jogjaButton = (Button) this.findViewById(R.id.jogja);
jogjaButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
playSound(3);
}
});
}
#Override
public void onPause() {
try{
super.onPause();
player.pause();
}catch (Exception e){
}
}
private void playSound(int arg){
try{
if (player.isPlaying()) {
player.stop();
player.release();
}
}catch(Exception e){
Toast.makeText(this, " Masuk Exception", Toast.LENGTH_LONG).show();
}
if (arg == 1){
Toast.makeText(this, isPlaying+" jakarta", Toast.LENGTH_LONG).show();
player = MediaPlayer.create(this, R.raw.jakarta);
}else if (arg == 2){
Toast.makeText(this, isPlaying+" aceh", Toast.LENGTH_LONG).show();
player = MediaPlayer.create(this, R.raw.aceh);
}else if (arg == 3){
Toast.makeText(this, isPlaying+" jogja", Toast.LENGTH_LONG).show();
player = MediaPlayer.create(this, R.raw.yogya);
}
player.setLooping(true); // Set looping
player.start();
}
whenever i click back or click home the sound is stop.. how to make the sound is still playing even when i click home or back button.. sorry for my english..
There is a Google sample project about playing music across different Android platforms, like Phone, Pad, Watch, Auto... It can keep music playing even app is exited. Take a deep look at here , and I think you will be inspired a lot.

How to play music again when returing to main activity

What I would like to achieve is to stop the music when I go to my 2nd activity from my main activity (this works and the music does stop) But when I press the back button to go back to the main activity the music doesn't seem to start again. How can I make it to start again or if possible pause to music when going to the 2nd activity and resume when going back to the main activity.
Here is my code where the music stops when going to the next activity but doesn't play when going back to the main activity:
public class MainActivity extends Activity implements OnClickListener{
public static MediaPlayer player;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
player = new MediaPlayer();
player = MediaPlayer.create(this, R.raw.music_a);
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setLooping(true);
player.start();
Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
player.stop();
}
});
}
protected void onPause() {
if (this.isFinishing()){
player.stop();
Toast.makeText(MainActivity.this, "BYE", Toast.LENGTH_LONG).show();
}
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())) {
player.stop();
}
else {
}
}
super.onPause();
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
And here is my 2nd activity (do I need to put something here as well?)
public class SecondActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.secondscreen);
}
}
Thanks
You have to call onResume on your First Activity to continue playing music
#Override
protected void onPause() {
super.onPause();
// stop the music
}
#Override
protected void onResume() {
super.onResume();
// play the music here
}
just remove your code to play from oncreate and start playing song in onResume
try {
if (player != null) {
player.start();
} else {
player = new MediaPlayer();
player = MediaPlayer.create(this, R.raw.meintenu);
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setLooping(true);
player.start();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
edit:
write this in your button click
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
startActivity(intent);
player.stop();
player.release();
player=null;
pause Your music when you go to next activity and get the current position of the music
homeMusic.pause();
position=homeMusic.getCurrentPosition();
then add onRestart method,
#Override
protected void onRestart() {
super.onRestart();
homeMusic.seekTo(position);
homeMusic.start();
homeMusic.setLooping(true);
}

how to pause media player sound in android

I have media player in main activity. And sound start with this activity. I want when new activity start audio stop and when I press back from next activity and back to main activity audio starts. Please help me.
Code-
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mp = MediaPlayer.create(this, R.raw.test);
mp.start();
mp.setLooping(true);
addListenerOnButton();
}
public void addListenerOnButton()
{
boy1 = (Button) findViewById(R.id.boy1);
boy1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
mp.stop();
startActivity(new Intent(Main.this, Boy1.class));
}
});
}
#Override
protected void onPause() {
super.onPause();
mp.stop();
}
#Override
public void onBackPressed() {
super.onBackPressed();
mp.stop();
finish();
}
#Override
public void onResume()
{
super.onResume();
}
#Override
protected void onStop() {
super.onStop();
mp.stop();
}
}
try this way:onPause()
#Override
protected void onPause() {
super.onPause();
try{
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
}
}catch(Exception we){
we.printStackTrace();
}
}
And onResume()
#Override
protected void onResume() {
super.onResume();
try{
mediaPlayer.start();
}catch(Exception we){
we.printStackTrace();
}
}
You could use the pause() method available in MediaPlayer class. This would be useful:
http://developer.android.com/reference/android/media/MediaPlayer.html
You can do this using mediaPlayer.pause();

how do i stop background music when back button is pressed?

i have 2 activities. and 2 different background music plays on both. when i press a button on the first activity it will go to this activity and the changing of music goes smoothly but i want to stop background music when back button is pressed. i tried this but it force closes. is there any other way to do this?
public class Categories extends Activity{
MediaPlayer mp;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.categories);
mp = MediaPlayer.create(Categories.this, R.raw.looney);
mp.setLooping(true);
mp.start();
Button cartoonButton = (Button) findViewById(R.id.cartoon);
cartoonButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent launchCartoon = new Intent(Categories.this, Cartoon.class);
startActivity(launchCartoon);
}
});
Button superButton = (Button) findViewById(R.id.hero);
superButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
Intent launchSuperheroes = new Intent(Categories.this, SuperHeroes.class);
startActivity(launchSuperheroes);
}
});
Button singerButton = (Button) findViewById(R.id.singer);
singerButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent launchSinger = new Intent(Categories.this, Singer.class);
startActivity(launchSinger);
}
});
Button famousButton = (Button) findViewById(R.id.famous);
famousButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent launchFamous = new Intent(Categories.this, Famous.class);
startActivity(launchFamous);
}
});
}
#Override
protected void onResume() {
super.onResume();
mp.start();
}
#Override
protected void onPause() {
super.onPause();
if (mp != null) {
mp.stop();
mp.release();
mp = null;
}
}
}
You can try this code:
#Override
public void onBackPressed() {
if (player.isPlaying()) {
player.stop();
}
player.release();
super.onBackPressed();
}
Hope this help you :)

onDestroy mediaplayer release

I want when I click on back button on the phone to close current activity and get back to menu and stop Media Player
But I get error: Unfortunately Weapons has stopped! upon clicking on back button
So how to fix that?
public class pushke extends Activity {
private SeekBar volumeSeekbar = null;
private AudioManager audioManager = null;
MediaPlayer mp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
setContentView(R.layout.guns);
initControls();
final MediaPlayer mp=MediaPlayer.create(this, R.raw.uzi);
ImageButton btn1 = (ImageButton) findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (mp.isPlaying()){
mp.pause();
mp.seekTo(0);
}
else{
mp.start();
}
}
});
}
private void initControls()
{
try
{
volumeSeekbar = (SeekBar)findViewById(R.id.seekBar1);
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
volumeSeekbar.setMax(audioManager
.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
volumeSeekbar.setProgress(audioManager
.getStreamVolume(AudioManager.STREAM_MUSIC));
volumeSeekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
#Override
public void onStopTrackingTouch(SeekBar arg0)
{ }
#Override
public void onStartTrackingTouch(SeekBar arg0)
{ }
#Override
public void onProgressChanged(SeekBar arg0, int progress, boolean arg2)
{
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
progress, 0);
}
});
}
catch (Exception e)
{
e.printStackTrace();
}
}
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
mp.release();
}
}
Logcat:
1) I want when I click on back button on the phone to close current
activity and get back to menu and stop Media Player
1) What do you mean by menu? I mean application menu or device menu or notification bar etc.
2) But I get error: Unfortunately Weapons has stopped! upon clicking on
back button So how to fix that?
2)
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
if(mp!=null){
mp.stop();
mp.release();
mp = null;
}
}
EDIT:
private MediaPlayer mp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
setContentView(R.layout.guns);
initControls();
mp=MediaPlayer.create(this, R.raw.uzi);
ImageButton btn1 = (ImageButton) findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (mp.isPlaying()){
mp.pause();
mp.seekTo(0);
}
else{
mp.start();
}
}
});
}
In On destroy before releasing media player please stop the media player

Categories

Resources