Android SDK Mediaplayer.create randomly returns null

蹲街弑〆低调 提交于 2019-12-22 03:17:02

问题


I am having an issue where occasionally the MediaPlayer.create method will return null, even though the audio file is definitely there. In fact, if I put the call to create into a while loop, the media player will eventually be created successfully. This only seems to happen on my phone (HTC Hero running 2.1) and never in the emulator.

Is this just an issue with the phone? Or is there a different way I should be playing these audio files?

Note that I want to be loading them dynamically which is get I am making the call to getIndentifer. If there is a better way of playing these dynamically please let me know.

public void playAudio(String audioFile){

    //instance variable of type MediaPlayer
    _player = null;
    while(_player == null){
         _player = MediaPlayer.create(_context, getResources().getIdentifier(audioFile, "raw", _context.getPackageName()));
    }
    _player.start();

}


Thanks.


回答1:


A SoundPool (Docs) might be something to consider.



来源:https://stackoverflow.com/questions/3589310/android-sdk-mediaplayer-create-randomly-returns-null

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!