在用模擬器播放音樂時,總是報一個錯誤:PVMFErrNotSupported Prepare failed.: status=0x1
錯誤在這段代碼裏:
try{
mp = new MediaPlayer();
mp.setDataSource(somePathToAudioFile);
mp.prepare();
mp.start();
}catch(Exception e){
}
裏面mp.setDataSource(somePathToAudioFile);這個方法中調用的是setDataSource(String);在Java中有一個FileDescriptor;我們可以通過getFD()方法得到一個FileDescriptor;以避免這些錯誤;
代碼修改後如下:
String audioFilePath = getFilesDir().getAbsolutePath() + File.separator + "aa.mp4";
try {
File file = new File(audioFilePath);
FileInputStream fis = new FileInputStream(file);
mediaPlayer.setDataSource(fis.getFD());
mediaPlayer.prepare();
mediaPlayer.start();
} catch(FileNotFoundException e){
} catch (IllegalArgumentException e) {
} catch (IllegalStateException e) {
} catch (IOException e) {
}