java - 为什么应用程序中的音频停止播放?

标签 java android android-studio audio

我有一个程序,每次用户执行某个操作时都会播放声音。但一段时间后,所有音频都不起作用。

示例代码:

public void bPressed(View view) {
    MediaPlayer mp = MediaPlayer.create(activity.this, R.raw.audio);
    mp.start();
}

最佳答案

您需要使用mp.release();当您完成MediaPlayer时:

As an example, consider the problems that could happen if you forgot to release the MediaPlayer when your activity is stopped, but create a new one when the activity starts again. As you may know, when the user changes the screen orientation (or changes the device configuration in another way), the system handles that by restarting the activity (by default), so you might quickly consume all of the system resources as the user rotates the device back and forth between portrait and landscape, because at each orientation change, you create a new MediaPlayer that you never release. (For more information about runtime restarts, see Handling Runtime Changes.)

http://developer.android.com/intl/es/guide/topics/media/mediaplayer.html

添加 mp.release(); 例如在 onStop()

关于java - 为什么应用程序中的音频停止播放?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36742621/

相关文章:

java - JTable 为图形返回 null?

java - 从 JPanel 中使用 javascript 调用 Java 方法

java - 我不能正确划分两个数字

java - 在 Android 上重复注释

xml - 安卓工作室 : Preview rendering not appearing for all my XML files

java - 无法加载要通过键盘移动的图像

android - 未定义的函数引用 Android NDK

git - 为什么在Android Studio 3.4.2 中从Git clone 项目时只创建本地master 分支?

java - 如何清除android中的后台进程?

当值不为空时java分配属性