Friday, May 18, 2012

[android-developers] Reading and playing audio/video files from internal storage/ external data store

Hi,
Am trying to get a media player to play a file stored in my internal / external storage (not res/raw or assets folder).
The media file is downloaded using a separate activity and stored in the local/external storage.

See below for the sample code for storing.

    FileOutputStream fos = openFileOutput("myfile.mp4" , Context.MODE_WORLD_READABLE);
    fos
.write(byteArrayBuf.toByteArray());
    fos
.close();
For playing the same file, I use.

    mMediaPlayer = new MediaPlayer();
   
ParcelFileDescriptor parcel = ParcelFileDescriptor.open(new File( URI.create("file:///data/data/com.mypackage/files/myfile.mp4")),ParcelFileDescriptor.MODE_READ_WRITE);
mMediaPlayer
.setDataSource(parcel.getFileDescriptor());
    mMediaPlayer
.setDisplay(mHolder);
    mMediaPlayer
.prepare();
    mMediaPlayer
.setOnPreparedListener(this);

   
/** On Prepared Implementation **/
   
public void onPrepared(MediaPlayer mp) {
       mMediaPlayer
.start();
   
}

However, the media player refuses to play the file and always fails with an error(1, filesize). Am kinda stuck to make the media player running as I seem to have done everything which is inline with the code samples to make it read a file from local/external storage.

Note: when i substitute a local file with a file from the res/raw folder this seems to play fine. is this specifically related to the file not being readable by the application or the media player. Help would be really appreciated to make this piece work.

Logcat output to help debug

    surfaceCreated
    I
/AwesomePlayer(   34): reset
    I
/AwesomePlayer(   34): cancel player events
    I
/AwesomePlayer(   34): cancel player events
    I
/SampleTable(   34): There are reordered frames present.
    D
/MyPlayerActivity(  460): surfaceChanged
    I
/ActivityManager(   82): Displayed com.mypackage/.MyPlayerActivity: +1s997ms
    D
/MyPlayerActivity(  460): Video Prepared
    D
/AudioSink(   34): bufferCount (4) is too small and increased to 12
    E
/MediaPlayer(  460): error (1, -2147483648)
    E
/MediaPlayer(  460): Error (1,-2147483648)
Does the media player explicitly need to use ContentResolvers to be able to play files from internal/external storage? In case it does, how does one go about implementing a ContentProvider for reading media files. Almost all code samples/documentation point towards database based ContentProviders and not file based ones. 
Help appreciated.

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate