Reputation: 3255
When playing audio from sdcard, MediaPlayer
plays the file. But when playing from getCacheDir()
it is an error. Is my conclusion right that MediaPlayer
can't play files from getCacheDir()
?
Here's a code snippet:
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());
}
}
}
Upvotes: 3
Views: 3060
Reputation: 3255
UPDATE: I just read from this Blog and solved my problem, inorder for me to play audio inside a cache, I added this line and that's it. Hope this helps anyone in the future. Here is the method.
File file = new File(filepath)
file.setReadable(true, false);
Upvotes: 7
Reputation: 3088
Here's a function that also works on API levels < 9:
Runtime.getRuntime().exec("chmod 644 "+ outputFile.getAbsolutePath());
Upvotes: 0