Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MediaPlayer cant play audio files from program data folder?

Tags:

android

When i record my audio from MIC and store file in /data/data/..... why MediaPlayer can't play this file ? If i change destination to / sdcard/..... - all works great. I do something wrong ? I not found limitation for MediaPlayer. Device - Samsung T959 (Galaxy S) Thanks, i hope anybody know solution....

like image 472
dmtrlbdv Avatar asked Sep 22 '10 20:09

dmtrlbdv


2 Answers

i had the same issues... whenever i used setDataSource with filepaths, it would not work; kept getting IOException.

changing my code to use setDataSource(FileDescriptor) does work... and with this i don't have to copy the files to SDCard or anything like that.

So, with a simple File object, create a new FileInputStream and pass the actual file descriptor as data source as in:

setDataSource((new FileInputStream(myFileInstance)).getFD());

creating an input stream pulls the file data into memory and thereby addresses /data/data access violations.

like image 182
Sheshadri Mantha Avatar answered Nov 16 '22 02:11

Sheshadri Mantha


Better practice is to close the stream, like this:

FileInputStream stream = new FileInputStream(path);
mediaPlayer.setDataSource(stream.getFD());
stream.close();

It is noted in the documentation of the method:

android.media.MediaPlayer.setDataSource(FileDescriptor fd)

Sets the data source (FileDescriptor) to use. It is the caller's responsibility to close the file descriptor. It is safe to do so as soon as this call returns.

like image 29
KitKat Avatar answered Nov 16 '22 01:11

KitKat