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);
}
Related
public class momtahina5 extends AppCompatActivity {
private static final String TAG = "momtahina5";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_momtahina5);
Log.d(TAG,"onCreate: Starting.");
Button gotobakara201 = (Button) findViewById(R.id.gotobakara201);
Button backtobakara286 = (Button) findViewById(R.id.backtobakara286);
gotobakara201.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d(TAG, "onClick: clicked btngotobakara201");
Intent intent1 = new Intent(momtahina5.this,bakara201.class);
startActivity(intent1);
MediaPlayer mp = MediaPlayer.create(momtahina5.this,R.raw.mumfive);
mp.start();
}
});
}
}
What you can do is Release media player when activity is finished or destroyed.
below code will demonstrate you.
#Override
protected void onDestroy() {
super.onDestroy();
if (mp != null) {
mp.stop();
mp.reset();
mp.release();
}
}
override the 'onPause()' method of the current Activity and in that do this:
#Override
public void onPause() {
super.onPause();
if(mp != null) {
mp.release();
mp = null;
}
}
And also you want to make the MediaPlayer object as a class variable.
*Edit: It would be better to use the onPause() method instead of the onDestroy() method. When you move to another activity, the previous activity is not immediately destroyed. Refer to Android activity lifecycle for more info.
How to stop play sound by press button back or button home
Please Help me :
public class MainActivity extends AppCompatActivity {
Button play;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
play = (Button) findViewById(R.id.button_play);
final MediaPlayer mP = MediaPlayer.create(MainActivity.this,R.raw.music);
play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mP.isPlaying()) {
mP.pause();
play.setBackgroundResource(R.drawable.play);
} else {
mP.start();
play.setBackgroundResource(R.drawable.pause);
}
}
}
);
}}
Pause media player in onPause(); Method
#Override
protected void onPause() {
if (mMediaPlayer != null && mMediaPlayer.isPlaying()){
mMediaPlayer.pause();
}
super.onPause();
}
Hope It will work for you.
For more information Refer MediaPlayer LifeCycle
to stop music player
private void stopPlaying() {
if (mp != null) {
mp.stop();
mp.release();
mp = null;
}
}
to stop play sound by press button back or button home
in Activity you need to stop music on onDestroy and onBackPressed
#Override
protected void onDestroy() {
super.onDestroy();
stopPlaying();
}
#Override
public void onBackPressed() {
super.onBackPressed();
stopPlaying();
}
I am creating a game, I want to play a background music for one activity only(For main menu of game), my code shown below, The problem is that the music plays more than one time, I want to play the same music also when activity Resumes.
public class Menu extends Activity {
MediaPlayer mp
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
mp = MediaPlayer.create(Menu.this, R.raw.adalante);
if(!mp.isPlaying()) {
mp.start();
}
public void play(View ButtonClicked) {
mp.stop();
mp.release();
//mp = MediaPlayer.create(Menu.this, R.raw.l);
//mp.start();
goToActivity(Game.class);
}
#Override
public void onResume() {
super.onResume(); // Always call the superclass method first
//coins
coin.setText(data.getString("coin"));
mp = MediaPlayer.create(Menu.this, R.raw.adalante);
if(!mp.isPlaying()) {
mp.start();
}
//mps.release();
}
In your onResume don't initialise MediaPlayer again and again. It creates new instance every time when you come to onResume. So add a check in onResume like this :
#Override
protected void onResume() {
super.onResume();
if (mp==null)
mp=MediaPlayer.create(MainActivity.this,R.raw.adalante);
if (!mp.isPlaying())
mp.start();
}
and additionally add this for prevention to play when activity goes to onPause
#Override
protected void onPause() {
super.onPause();
mp.pause();
}
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();
}
I'm having a bit of a trouble,, here's the gen. idea,, I have created an app a simple game-like application.. however the problem is when I try to click the button in the title screen it doesn't play the click sound.. can anyone please help me.. here's the code for my sound
public class Effects extends Activity implements MediaPlayer.OnCompletionListener {
Button mPlay;
MediaPlayer mPlayer;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPlay = (Button)findViewById(R.id.btnStart);
mPlay.setOnClickListener(playListener);
setContentView(R.layout.activity_main);
}
#Override
public void onDestroy() {
super.onDestroy();
if(mPlayer != null) {
mPlayer.release();
}
}
private View.OnClickListener playListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mPlayer == null) {
try {
mPlayer = MediaPlayer.create(Effects.this, R.raw.button11);
mPlayer.start();
} catch (Exception e) {
e.printStackTrace();
}
} else {
mPlayer.stop();
mPlayer.release();
mPlayer = null;
}
}
};
#Override
public void onCompletion(MediaPlayer arg0) {
// TODO Auto-generated method stub
}
}
and here's my code for the main java that I want the sound to be played when I clicked this button
public class Main extends Activity implements OnClickListener
{
Button about;
Button Quit;
Button start;
protected void onCreate(Bundle onSavedInstanceState) {
super.onCreate(onSavedInstanceState);
setContentView(R.layout.activity_main);
about = (Button)findViewById(R.id.btnAbout);
about.setOnClickListener(this);
Quit = (Button)findViewById(R.id.btnQuit);
Quit.setOnClickListener(this);
start = (Button)findViewById(R.id.btnStart);
start.setOnClickListener(this);
}
public void onClick(View argo)
{
if(argo.getId()==R.id.btnAbout)
{
Intent i = new Intent(this,About.class);
this.startActivity(i);
}
if(argo.getId()==R.id.btnQuit)
{
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
if(argo.getId()==R.id.btnStart)
{
Intent start = new Intent(this,Howto.class);
this.startActivity(start);
Intent svc=new Intent(this,Effects.class);
startService(svc);
}
}
}
Use SoundPool to play your sounds: http://developer.android.com/reference/android/media/SoundPool.html you should init sounds in your onCreate() and then play them back in onClick()
And instead of doing thousands of pointless if(argo.getId()==R.id.btnQuit) (you should at least use else) get familiar with switch/case syntax)