0

I have really hard time with background music in my app. I just want to play music in all activities - when I press home button I want to stop music. I want "stop or play" music button in all activities, but couldn´t make it work.

So I decided to make embarassing choice - play it only in 1 activitiy by

onCreate

    backgroundmusic = MediaPlayer.create(StoryActivity.this, R.raw.creepy_music);
    backgroundmusic.start();

onPause

    @Override
    protected void onPause() {
    super.onPause();
    backgroundmusic.release();
    finish();
}

Can you please help me with easy activity lifecycle? So when a user presses home button - music will stop. When he will come back to app - music will be restored and this activity too (it is not MainActivity)

Thank you, guys

Community
  • 1
  • 1
mousik
  • 101
  • 1
  • 11
  • 2
    Possible duplicate of [Simplest Android Activity Lifecycle](http://stackoverflow.com/questions/5538312/simplest-android-activity-lifecycle) – wake-0 Dec 23 '16 at 17:59
  • You can play music from Service and manage stream from Activity. Here's a good tutorial about that: http://sapandiwakar.in/building-a-music-player-app-for-android-2/ – Nikita Khlebushkin Dec 23 '16 at 18:08
  • In that case music won't be attached to one activity only and you can manage from anywhere – Nikita Khlebushkin Dec 23 '16 at 18:09
  • @НикитаХлебушкин I tried a lot of types of services for this music manager, but I will give this one a try :) Thank you – mousik Dec 23 '16 at 18:10
  • @НикитаХлебушкин It is exactly what I am trying few hours to do... I hope this will help – mousik Dec 23 '16 at 18:15

2 Answers2

0

<code>enter image description here</code>

Here are the different LifeCycle states. Now to your answer,

@Override
    protected void onStop() {
        super.onStop();
        backgroundmusic.pause();
        length = backgroundmusic.getCurrentPosition();  
    }

@Override
    protected void onResume() {
        super.onResume();
        backgroundmusic.seekTo(length);
        backgroundmusic.start();
    }

In public class MainActivity extends AppCompatActivity, It's the AppCompatActivity that is the main source of an Activity's functionality, hence in the above methods like super.onResume(); and super.onStop(); super refers to the AppCompatActivity class

devDeejay
  • 5,494
  • 2
  • 27
  • 38
0

assume that you can get the music service in the Application ,you may be looking for this :

 public class MyApp extends Application{

MusicService musicService;

@Override
public void onCreate() {
    super.onCreate();

    registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
        @Override
        public void onActivityCreated(Activity activity, Bundle savedInstanceState) {

        }

        @Override
        public void onActivityStarted(Activity activity) {

        }

        @Override
        public void onActivityResumed(Activity activity) {
            if(musicService==null) return;
            if(!musicService.isPlaying()){
                musicService.play();
            }
        }

        @Override
        public void onActivityPaused(Activity activity) {
             if(musicService==null) return;
             if(musicService.isPlaying()){
                musicService.pause();
            }
        }

        @Override
        public void onActivityStopped(Activity activity) {

        }

        @Override
        public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

        }

        @Override
        public void onActivityDestroyed(Activity activity) {

        }
    });
}

}

Hope this helps

Du.Fantasy
  • 475
  • 2
  • 7