简体   繁体   中英

How to stop sound effect to get playing after i hit back button from activity

Hi I have 2 classes Player and Activity1 as following. When I play a sound effect and hit back button sound file keep playing. Can anyone help me what i need to add in code to stop this. Also please suggest if there is more efficient way of implementing audio player for sound effects. Please give some example as fairly new to android development. Thanks

Activity1

   public class Activity1
        extends Activity
{


    Sound Sounds[] = {
            new Sound(this,R.raw.sound1 ,R.id.id1,"")   
    };


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.an);


        Player p = new Player();
        Sound.setPlayer(p);

        for (int i = 0; i < Sounds.length; i++){
            Sounds[i].Initialize();
        }


    }




} 

Player

public class Player {
    private MediaPlayer mp;
    public Player(){
        mp = new MediaPlayer();
    }
    public void play(Context c, int r) {
        if (mp==null)
            return;
        if (mp.isPlaying()){
            mp.stop();
            mp.reset();
            mp.release();
        }else{
            mp = MediaPlayer.create(c, r);
            mp.start();
        }
    }




}

Logs:

11-03 02:28:27.300 1653-2196/system_process W/InputMethodManagerService: Got RemoteException sending setActive(false) notification to pid 3045 uid 10060
11-03 02:28:27.459 1653-1676/system_process I/ActivityManager: Displayed com.example.keshav.giantsoundboard/.MainActivity: +837ms
11-03 02:28:42.709 1653-2152/system_process I/ActivityManager: START u0 {cmp=com.example.keshav.giantsoundboard/.Activity8} from uid 10060 on display 0
11-03 02:28:43.221 1653-1676/system_process I/ActivityManager: Displayed com.example.keshav.giantsoundboard/.Activity8: +462ms
11-03 02:28:44.105 1653-2152/system_process I/AccountManagerService: getTypesVisibleToCaller: isPermitted? true
11-03 02:28:44.440 1653-2197/system_process I/AccountManagerService: getTypesVisibleToCaller: isPermitted? true
11-03 02:28:44.981 1653-2197/system_process I/AccountManagerService: getTypesVisibleToCaller: isPermitted? true
11-03 02:28:45.750 1653-2152/system_process I/AccountManagerService: getTypesVisibleToCaller: isPermitted? true
11-03 02:28:46.549 1653-1981/system_process I/AccountManagerService: getTypesVisibleToCaller: isPermitted? true
11-03 02:28:47.286 1653-1981/system_process I/AccountManagerService: getTypesVisibleToCaller: isPermitted? true
11-03 02:28:47.315 1653-2196/system_process I/AccountManagerService: getTypesVisibleToCaller: isPermitted? true
11-03 02:28:47.337 1653-1666/system_process I/AccountManagerService: getTypesVisibleToCaller: isPermitted? true
11-03 02:28:47.357 1653-3044/system_process I/AccountManagerService: getTypesVisibleToCaller: isPermitted? true
11-03 02:28:47.379 1653-1805/system_process I/AccountManagerService: getTypesVisibleToCaller: isPermitted? true
11-03 02:28:47.398 1653-1981/system_process I/AccountManagerService: getTypesVisibleToCaller: isPermitted? true
11-03 02:28:50.068 1653-1666/system_process W/ActivityManager:   Force finishing activity com.example.keshav.giantsoundboard/.Activity8
11-03 02:28:50.169 1653-3041/system_process I/OpenGLRenderer: Initialized EGL, version 1.4
11-03 02:28:50.203 1653-3041/system_process D/EGL_emulation: eglCreateContext: 0xaeafc780: maj 2 min 0 rcv 2
11-03 02:28:50.234 1653-3041/system_process D/EGL_emulation: eglMakeCurrent: 0xaeafc780: ver 2 0 (tinfo 0x9f7addd0)
11-03 02:28:50.275 1653-1667/system_process I/AccountManagerService: getTypesVisibleToCaller: isPermitted? true
11-03 02:28:50.306 1653-3041/system_process D/EGL_emulation: eglMakeCurrent: 0xaeafc780: ver 2 0 (tinfo 0x9f7addd0)
11-03 02:28:50.585 1653-1671/system_process W/ActivityManager: Activity pause timeout for ActivityRecord{79a57b u0 com.example.keshav.giantsoundboard/.Activity8 t78 f}
11-03 02:28:50.782 1653-3044/system_process I/AccountManagerService: getTypesVisibleToCaller: isPermitted? true
11-03 02:28:52.278 1653-3041/system_process D/EGL_emulation: eglMakeCurrent: 0xaeafc780: ver 2 0 (tinfo 0x9f7addd0)
11-03 02:28:52.289 1653-3041/system_process E/Surface: getSlotFromBufferLocked: unknown buffer: 0xb40fdde0
11-03 02:28:52.306 1653-3041/system_process D/OpenGLRenderer: endAllStagingAnimators on 0x9eb33980 (RippleDrawable) with handle 0x9f7adbc0
11-03 02:28:52.312 1653-1711/system_process W/InputDispatcher: channel '4430afe com.example.keshav.giantsoundboard/com.example.keshav.giantsoundboard.MainActivity (server)' ~ Consumer closed input channel or an error occurred.  events=0x9
11-03 02:28:52.312 1653-1711/system_process E/InputDispatcher: channel '4430afe com.example.keshav.giantsoundboard/com.example.keshav.giantsoundboard.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
11-03 02:28:52.313 1653-1711/system_process W/InputDispatcher: channel '2731e57 com.example.keshav.giantsoundboard/com.example.keshav.giantsoundboard.Activity8 (server)' ~ Consumer closed input channel or an error occurred.  events=0x9
11-03 02:28:52.313 1653-1711/system_process E/InputDispatcher: channel '2731e57 com.example.keshav.giantsoundboard/com.example.keshav.giantsoundboard.Activity8 (server)' ~ Channel is unrecoverably broken and will be disposed!
11-03 02:28:52.315 1653-1806/system_process I/WindowState: WIN DEATH: Window{2731e57 u0 com.example.keshav.giantsoundboard/com.example.keshav.giantsoundboard.Activity8}
11-03 02:28:52.316 1653-1806/system_process W/InputDispatcher: Attempted to unregister already unregistered input channel '2731e57 com.example.keshav.giantsoundboard/com.example.keshav.giantsoundboard.Activity8 (server)'
11-03 02:28:52.317 1653-2152/system_process I/ActivityManager: Process com.example.keshav.giantsoundboard (pid 3089) has died
11-03 02:28:52.320 1653-2211/system_process D/GraphicsStats: Buffer count: 3
11-03 02:28:52.344 1653-2197/system_process I/WindowState: WIN DEATH: Window{4430afe u0 com.example.keshav.giantsoundboard/com.example.keshav.giantsoundboard.MainActivity}
11-03 02:28:52.344 1653-2197/system_process W/InputDispatcher: Attempted to unregister already unregistered input channel '4430afe com.example.keshav.giantsoundboard/com.example.keshav.giantsoundboard.MainActivity (server)'
11-03 02:28:52.368 1653-2152/system_process I/ActivityManager: Start proc 3432:com.example.keshav.giantsoundboard/u0a60 for activity com.example.keshav.giantsoundboard/.MainActivity
11-03 02:28:52.639 1653-1676/system_process W/AppOps: Finishing op nesting under-run: uid 1000 pkg android code 24 time=0 duration=0 nesting=0
11-03 02:28:52.867 1653-1676/system_process W/WindowAnimator: Failed to dispatch window animation state change.
                                                              android.os.DeadObjectException
                                                                  at android.os.BinderProxy.transactNative(Native Method)
                                                                  at android.os.BinderProxy.transact(Binder.java:503)
                                                                  at android.view.IWindow$Stub$Proxy.onAnimationStopped(IWindow.java:534)
                                                                  at com.android.server.wm.WindowAnimator.updateWindowsLocked(WindowAnimator.java:286)
                                                                  at com.android.server.wm.WindowAnimator.animateLocked(WindowAnimator.java:678)
                                                                  at com.android.server.wm.WindowAnimator.-wrap0(WindowAnimator.java)
                                                                  at com.android.server.wm.WindowAnimator$1.doFrame(WindowAnimator.java:123)
                                                                  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:856)
                                                                  at android.view.Choreographer.doCallbacks(Choreographer.java:670)
                                                                  at android.view.Choreographer.doFrame(Choreographer.java:603)
                                                                  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
                                                                  at android.os.Handler.handleCallback(Handler.java:739)
                                                                  at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                  at android.os.Looper.loop(Looper.java:148)
                                                                  at android.os.HandlerThread.run(HandlerThread.java:61)
                                                                  at com.android.server.ServiceThread.run(ServiceThread.java:46)
11-03 02:28:52.910 1653-1805/system_process W/InputMethodManagerService: Got RemoteException sending setActive(false) notification to pid 3089 uid 10060
11-03 02:28:53.089 1653-1676/system_process I/ActivityManager: Displayed com.example.keshav.giantsoundboard/.MainActivity: +740ms

The first thing you will have to do in your player class add a method to stop the MediaPlayer mp. Add this method in the mediaPlayer:

public void stop() {
    if (mp==null)
        return;
    if (mp.isPlaying()){
        mp.stop();
        mp.reset();
        mp.release();
    }
}

The in your Activity1 declare the Player object inside the class but not in any method like what you have done with the Sounds Add the following line:

private Player p;

After that go to the line this line and change:

Player p = new Player();

Change it to be:

p = new Player();

Up to there you can access your Player object throught your Activity class, so override the method onBackPressed and call the method in the Activity1 it:

@Override
public void onBackPressed() {
    super.onBackPressed();
    p.stop();
}

Try this and if you want to pause the Music each time the user leave your app call method in onPause method. Sometimes users may leave your app by tapping a notification or home Button (not only by pressing back!).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM