- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
嗨,我有 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/
Closed. This question does not meet Stack Overflow guidelines。它当前不接受答案。 想改善这个问题吗?更新问题,以便将其作为on-topic
我正在使用新的 iOS10 框架进行实时语音识别。我使用 AVCaptureSession 获取音频。 我有一个“正在收听”的哔哔声,通知用户他可以开始说话了。放置声音的最佳方式是在第一次调用 cap
在我的 3D 游戏中,我目前有通过工厂类“声音”工作的声音。我正在通过我的相机类初始化 OpenAL,加载时它将存储其位置、方向和速度的全局 float 缓冲区 private static Floa
我正在尝试为我的太空射击游戏实现 3d 声音系统。我已准备好一切(在每一侧播放不同音量的声音等)但我找不到正确的公式来计算每一侧的正确音量。 一般的想法是,每次玩家(飞船)杀死一个敌人(摄像机总是在飞
我正在编写一个简单的 Android 游戏。我在播放简单的“游戏完成”音效时遇到问题,一旦游戏结束(无论是赢还是输),然后才可以选择开始新游戏。 我的源代码是: soundPool.play(comp
这是我在这里逗留了一段时间后的第一个问题。所以请饶了我吧。 我需要通过 CLI 或 C++ 将 iZotope Vinyl VST 效果应用到某些音频文件(因此语言并不重要),它必须在 Mac 或基于
我正在开发一个 Android 应用程序,允许用户将手放在设备上,我想禁用物理按钮,这样用户就不会被不必要的点击所烦扰。 我已经禁用了后退、菜单和搜索按钮: @Overridepublic void
在一个简单的 HTML5/WebGL 应用中,我想偶尔播放一个音效。 目前我有一个构造函数: this.audio = new Audio('audio/zeep.mp3'); 然后,我想播放文件:
我是一名优秀的程序员,十分优秀!