gpt4 book ai didi

java - 当我从 Activity 中按后退按钮后如何停止播放音效

转载 作者:行者123 更新时间:2023-12-02 12:03:17 25 4
gpt4 key购买 nike

嗨,我有 2 个类 Player 和 Activity1,如下所示。当我播放音效并按后退按钮时,声音文件继续播放。任何人都可以帮助我需要在代码中添加什么来阻止这种情况。另请建议是否有更有效的方法来实现音频播放器的音效。请举一些对于 android 开发相当新的例子。谢谢

Activity 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

最佳答案

您要做的第一件事是在播放器类中添加一个方法来停止 MediaPlayer mp。在mediaPlayer中添加此方法:

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

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

private Player p;

之后转到这一行并更改:

Player p = new Player();

将其更改为:

p = new Player();

到这里,您可以通过 Activity 类访问 Player 对象,因此重写方法 onBackPressed 并在 Activity1 中调用该方法:

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

如果您想在每次用户在 onPause 方法中离开您的应用调用方法时暂停音乐,请尝试此操作。有时,用户可能会通过点击通知或主页按钮(不仅仅是按后退!)来离开您的应用。

关于java - 当我从 Activity 中按后退按钮后如何停止播放音效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47085783/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com