1

I am trying to make a media player play a media file stored in the local/external storage.

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_WRITEABLE);
    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, ). I am really at my wits end to make this 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.

Update 1

@Chris : I have temporarily made the file WORLD_WRITEABLE and once I get the media player to work would in fact write files as WORLD_READABLE. Hard-coding the paths is deliberate as of now as I just want to initially ensure that the file gets read without any ambiguity. In fact I have checked that the file does exist by going through adb shell and DDMS.Once i get the file read, in fact I will probably use something more sensible like getFilesDir(). Logcat output as below.

    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)
4

1 回答 1

0

正如 Chris Stratton 评论的那样,不要对内部或外部存储的路径进行硬编码。它们可能适用于某些系统,但不能保证适用于每个系统。

用于getFilesDir()查找目录的路径。从文档...

公共文件 getFilesDir ()

返回文件系统上存储使用 openFileOutput(String, int) 创建的文件的目录的绝对路径。

返回包含应用程序文件的目录的路径。

您可以调用getAbsolutePath()返回的File并附/myfile.mp4加到它,然后setDataSource(...)Mediaplayer.

于 2012-05-17T17:27:06.787 回答