有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我有一个程序,每次用户做动作时都会播放声音。但是过了一段时间,所有的音频都不起作用了

示例代码:

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

共 (1) 个答案

  1. # 1 楼答案

    使用MediaPlayer后,需要使用mp.release();

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