嗨,我有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$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

最佳答案

您必须在播放器类中做的第一件事是向stop mp添加方法。在mediaPlayer中添加以下方法:

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


在您的MediaPlayer中,在类内声明Activity1对象,但不能使用与Player相同的任何方法添加以下行:

private Player p;


之后,转到该行并更改:

Player p = new Player();


更改为:

p = new Player();


到目前为止,您可以通过Activity类访问Sounds对象,因此可以覆盖方法Player并在Activity1中调用该方法:

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


尝试执行此操作,如果您希望用户每次在onBackPressed方法中离开您的应用程序调用方法时暂停音乐。有时,用户可以通过点击通知或主页按钮(不仅可以通过按回!)来离开您的应用。

10-07 19:49
查看更多