when i run my app开发者_运维技巧lication it shows "Exceeds maximum number of OpenCore instances" error.How to resolve this error. Thank you
The above happens if your instantianting multiple instances of the MediaPlayer.
You should read about the lifecycle of the media player and its different states
- Define one media player, at class variable scope for example
- Instantiate it in the Oncreate method and in the OnResume methods as required
- in your function to play your audio use the .reset method to get the media player to go into and idle state
- use the .setDataSource or equivalent to get it into an initialised state
- then .prepare for the prepared state
- then .start to play the audio
- .stop() etc to stop it
- when your finished use the .release function to release the audio resource.
Media Player is defined as a class scope variable and instantiated in the OnCreate() Method
private void startPlayingAudio()
{
try {
Uri path1 = Uri.parse("android.resource://com.yourpackagehere./" + R.raw.beep);
mp.reset(); // reset to idle state
mp.setDataSource(this, path1); // from idle to initialised state
mp.prepare();
mp.start();
} catch (Exception ioe) {
Log.e(LOG_TAG, "Error playing the beep sound");
}
}
精彩评论