從SD卡播放音頻時,MediaPlayer
播放該文件。但從getCacheDir()
播放時,這是一個錯誤。我的結論是MediaPlayer
無法播放getCacheDir()
的文件嗎?從緩存目錄中播放音頻
這裏有一個代碼片段:
private void startPlaying(String asAudioFileName) {
File fFile = new File(VocaPreview.this.getCacheDir() + "/" + asAudioFileName.toLowerCase() + ".mp3");
if(!fFile.isFile()) {
Toast.makeText(getApplicationContext(), "File not found", Toast.LENGTH_SHORT).show();
} else {
try {
FileInputStream fisAudio = new FileInputStream(fFile);
//fmpAudio.setDataSource(VocaPreview.this.getCacheDir() + "/" + asAudioFileName.toLowerCase() + ".mp3");
//fmpAudio.setDataSource("/sdcard/2.mp3");
fmpAudio.setDataSource(fisAudio.getFD());
fmpAudio.prepare();
fmpAudio.start();
} catch(IOException ioe) {
Log.e("START PLAYING", ioe.getMessage());
}
}
}