简体   繁体   English

Android SDK Mediaplayer.create随机返回null

[英]Android SDK Mediaplayer.create randomly returns null

I am having an issue where occasionally the MediaPlayer.create method will return null, even though the audio file is definitely there. 我有一个问题,偶尔MediaPlayer.create方法将返回null,即使音频文件肯定存在。 In fact, if I put the call to create into a while loop, the media player will eventually be created successfully. 实际上,如果我将调用create创建为while循环,最终将成功创建媒体播放器。 This only seems to happen on my phone (HTC Hero running 2.1) and never in the emulator. 这似乎只发生在我的手机上(HTC Hero运行2.1)而且从未出现在模拟器中。

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. 请注意,我想动态加载它们,这是因为我正在调用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. 谢谢。

SoundPool( Docs )可能需要考虑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM