Android MediaPlayer full path to file

前端 未结 3 1695
没有蜡笔的小新
没有蜡笔的小新 2021-02-04 22:23

I need to get the full path to a file somewhere on the phone (any location) and play it with MediaPlayer.

ive heard of using a file chooser for Android (by launching an

3条回答
  •  情话喂你
    2021-02-04 22:47

    The second call to MediaPlayer.prepare() (already called once in the AudioVideoEntry ctor) in the AudioVideoEntry.prepareMedia() method was easy to spot as other people have noticed.

    The harder error to find was the first error.

    I used an Ogg file for testing.

    First clue was from davidsparks reply (last one) in android-platform - Ogg on G1

    As long as the files have a .ogg extension, they should play with the built-in music player. We rely on the file extensions because there is no centralized file recognizer for the media scanner.

    Second clue was from [android-developers] Re: File permission about MediaPlayer

    Due to the Android security model, MediaPlayer does not have root access rights. It can access the sdcard, but it can't access private app directories.

    Your app can explicitly grant MediaPlayer temporary access to secure files by opening the file and passing the file descriptor to MediaPlayer using the setDataSource(FileDescriptor fd) method.

    If you look at the absolute path of the output stream, you see it's in the /data/data directory under the app's package name's directory.

    Excuse the timestamps - I worked backwards to get data to show on a OS2.1update1 (API7) emulator.

    Your code had:

    String ave_file_name = "my_media_content";
    
    ave_fos = activity.openFileOutput(ave_file_name, Context.MODE_PRIVATE);
    

    DDMS showed:

    02-10 05:10:28.253: WARN/MediaPlayer(1992): info/warning (1, 26)
    02-10 05:10:28.253: ERROR/PlayerDriver(31): Command PLAYER_SET_DATA_SOURCE completed with an error or info PVMFErrNotSupported
    02-10 05:10:28.253: ERROR/MediaPlayer(1992): error (1, -4)
    02-10 05:10:28.274: WARN/PlayerDriver(31): PVMFInfoErrorHandlingComplete
    

    If we change JUST the file to MODE_WORLD_READABLE:

    String ave_file_name = "my_media_content";
    
    ave_fos = activity.openFileOutput(ave_file_name, Context.MODE_WORLD_READABLE);
    

    DDMS shows no improvement:

    02-10 05:08:28.543: WARN/MediaPlayer(1900): info/warning (1, 26)
    02-10 05:08:28.553: ERROR/PlayerDriver(31): Command PLAYER_SET_DATA_SOURCE completed with an error or info PVMFErrNotSupported
    02-10 05:08:28.553: ERROR/MediaPlayer(1900): error (1, -4)
    02-10 05:08:28.563: WARN/PlayerDriver(31): PVMFInfoErrorHandlingComplete
    

    If we change JUST the file extension to ogg:

    String ave_file_name = "my_media_content.ogg";
    
    ave_fos = activity.openFileOutput(ave_file_name, Context.MODE_PRIVATE);
    

    We get a change in DDMS output:

    02-10 04:59:30.153: ERROR/MediaPlayerService(31):   error: -2
    02-10 04:59:30.163: ERROR/MediaPlayer(1603): Unable to to create media player
    

    But when we combine the two:

    String ave_file_name = "my_media_content.ogg";
    
    ave_fos = activity.openFileOutput(ave_file_name, Context.MODE_WORLD_READABLE);
    

    DDMS shows no errors.

提交回复
热议问题