Android Media Player Plays In The Background, But Doesn't Stop When App Killed

后端 未结 10 1731
被撕碎了的回忆
被撕碎了的回忆 2021-01-17 08:15

I\'m new to Android, so I have a problem. In Android I want to play background music as soon as my music player starts and have it continue even if the activity changes from

相关标签:
10条回答
  • 2021-01-17 08:49

    You have to simply call the music.stop() whenever you leave the application. You can do this in onStop() method of your launcher activity if your app consists only one activity.

    I would suggest you to use SoundPool if you want to efficiently play the sounds. Using it you can play lots of concurrent sounds. I also made a demo project of SoundPool. You can find it here

    When you use MediaPlayer object you'll have to manage all the things by own your own. I also used the same in my first game. But now for my new game I'm using SoundPool. It was added in API 1 n still rocks.

    0 讨论(0)
  • 2021-01-17 08:50

    To start playing music you will need to figure out when our application starts. We can do that by extending Application class. Application object is instantiated before any activity and is preserved until our app is terminated by the Android OS. That makes it ideal to control playback only within this object. When we create new class that extends Application we must register it into our manifest.

    We have determined when our application is created but what about stopping the music when our application goes from foreground to background? Android offers no out-of-the-box method for determining that. To solve that problem I followed this very good article and part of the solution code was from there.

    Code for Application:

    package com.example.stackmusic; 
    
    import android.app.Activity;
    import android.app.Application;
    import android.media.MediaPlayer;
    import android.os.Bundle;
    
    
    public class PlayMusicApp extends Application {
    
    
    private MediaPlayer music;
    @Override
    public void onCreate() {
        super.onCreate();
        registerActivityLifecycleCallbacks(new MyLifecycleHandler());
    
        music = MediaPlayer.create(this, R.raw.some_music);
    }
    
    /**
     * All activities created in our application will call these methods on their respective
     * lifecycle methods.
     */
    class MyLifecycleHandler implements ActivityLifecycleCallbacks {
    
        private int resumed;
        private int stopped;
    
        public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
        }
    
        public void onActivityDestroyed(Activity activity) {
        }
    
        public void onActivityResumed(Activity activity) {
            // this is the first activity created
            if(resumed == 0) {
                music.start();
            }
            ++resumed;
        }
    
        public void onActivityPaused(Activity activity) {
        }
    
        public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
        }
    
        public void onActivityStarted(Activity activity) {
        }
    
        public void onActivityStopped(Activity activity) {
            ++stopped;
            android.util.Log.w("test", "application is being backgrounded: " + (resumed == stopped));
            // we are stopping the last visible activity
            if(resumed == stopped) {
                music.stop();
            }
        }
      }
    }
    

    Registration in manifest:

    <application
        android:name=".PlayMusicApp"
    

    You don't want to play music in the UI thread. Ideally create a service for playing music in background thread instead of managing media player in Application object.

    0 讨论(0)
  • 2021-01-17 08:51

    If you have not very much activities, You can manage this task by adding boolean flags in activities, where you want music to be played.
    When you stop one Activity these flags will show you, whether other Activities need your MediaPlayer

    So in your main Activity:

    public class MainActivity extends Activity {
    
        static MediaPlayer introPlayer;
        static boolean sActive;
    
        @Override
        protected void onResume() {
    
        // starting the player if it is not playing
            if (!introPlayer.isPlaying()) {
                introPlayer.start();
                introPlayer.setLooping(true);
            }
    
            // true when activity is active
            sActive = true;
            super.onResume();
        }
    
        @Override
        protected void onPause() {
    
            sActive = false;
            super.onPause();
        }
    
        @Override
        protected void onStop() {
    
            // before stoping mediaplayer checking whether it is not used by other          activities
    
            if (introPlayer.isPlaying()
                    && !(Activity2.sActive || Activity3.sActive)) {
                introPlayer.pause();
            }
    
            super.onStop();
    
        }
    }
    

    Other activities:

    public class Activity2 extends Activity {
    
        static boolean sActive;
    
        @Override
        protected void onPause() {
            sActive = false;
    
            super.onPause();
        }
    
        @Override
        protected void onStop() {
    
            // pausing the player in case of exiting from the app
            if (MainActivity.introPlayer.isPlaying() && !(MainActivity.sActive || Activity3.sActive)) {
                MainActivity.introPlayer.pause();
            }
    
            super.onStop();
        }
    
        @Override
        protected void onResume() {
            sActive = true;
    
            if (!MainActivity.introPlayer.isPlaying()) {
                MainActivity.introPlayer.start();
                MainActivity.introPlayer.setLooping(true);
            }
    
            super.onResume();
            }
    }
    

    And

    public class Activity3 extends Activity {
    
        static boolean sActive;
    
        @Override
        protected void onPause() {
            sActive = false;
    
            super.onPause();
        }
    
        @Override
        protected void onStop() {
    
            // pausing the player in case of exiting from the app
            if (MainActivity.introPlayer.isPlaying() && !(MainActivity.sActive || Activity2.sActive)) {
                MainActivity.introPlayer.pause();
            }
    
            super.onStop();
        }
    
        @Override
        protected void onResume() {
            sActive = true;
    
            if (!MainActivity.introPlayer.isPlaying()) {
                MainActivity.introPlayer.start();
                MainActivity.introPlayer.setLooping(true);
            }
    
            super.onResume();
            }
    }
    
    0 讨论(0)
  • 2021-01-17 08:51

    i did this by following way

    initialize

        MediaPlayer player;
        Music music;
    

    in OnCreate

        music = new Music();
        music.execute("abc");
    

    when you want to stop music, usually in onDestroy method

            music.cancel(true);
            if(player.isPlaying()){
            player.stop();
            player.release();
            }
    

    create music class

      public class Music extends AsyncTask<String, Integer, Void>
    {
    
        @Override
        protected Void doInBackground(String... params) {
    
            if(running)
            {
            player = MediaPlayer.create(getApplicationContext(), R.raw.bg_music1); 
            player.setVolume(100,100); 
            player.setLooping(true); 
            player.start(); 
            }
            else
            {
                player.stop();
                player.release();
            }
            return null;
        }
    
        @Override
        protected void onCancelled() {
            // TODO Auto-generated method stub
            super.onCancelled();
            running = false;
        }
    }
    

    another way is to use service

    0 讨论(0)
  • 2021-01-17 08:55

    Take a look @ ActivityLifecycleCallbacks and track the starting and stopping of activities to know when your app is "stopped". This would be when a call comes in to onActivityStopped and not followed by a call to onActivityStarted within a small timeframe.

    0 讨论(0)
  • 2021-01-17 08:58

    try this

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)  {
        if (keyCode == KeyEvent.KEYCODE_BACK ) {
            music.stop();
            return true;
        }
    
        return super.onKeyDown(keyCode, event);
    }
    

    else if you have any separate button action for closing the app, there to u can call as music.stop();

    0 讨论(0)
提交回复
热议问题