我从 activity 按下后退按钮后如何停止播放音效

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

您好,我有 2 个 类 Player 和 Activity1,如下所示。当我播放音效并按下后退按钮时,声音文件会继续播放。谁能帮我在代码中添加什么来阻止它。另外请建议是否有更有效的方法来实现音频播放器的音效。请举一些 android 开发的新例子。谢谢

活动 1

   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();
        }


    }




} 

玩家

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();
        }
    }




}

日志:

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.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

您必须在播放器中做的第一件事 class 向 stop MediaPlayer mp 添加一个方法。在mediaPlayer中添加这个方法:

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

在您的 Activity1 中声明 class 中的 Player 对象,但不是像您对 Sounds 所做的那样的任何方法 添加以下行:

private Player p;

之后转到这一行并更改:

Player p = new Player();

将其更改为:

p = new Player();

至此,您可以通过 Activity class 访问 Player 对象,因此覆盖方法 onBackPressed 并调用 [=35= 中的方法]1它:

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

试试这个,如果你想在每次用户离开你的应用程序调用方法时暂停音乐 onPause 方法。有时用户可能会通过点击通知或主页按钮离开您的应用程序(不仅仅是通过按返回!)。