MediaPlayer.setDataSource(String) not working with local files

前端 未结 6 1041
一个人的身影
一个人的身影 2020-12-02 20:31

I am able to play a local mp3 if I use the static method MediaPlayer.create(context, id) but it\'s not working if I use the non-static method MediaPlayer.setDataSource(Strin

6条回答
  •  死守一世寂寞
    2020-12-02 20:57

    Like the android documentation said

    Arbitrary files to save in their raw form. To open these resources with a raw InputStream, call Resources.openRawResource() with the resource ID, which is R.raw.filename.

    However, if you need access to original file names and file hierarchy, you might consider saving some resources in the assets/ directory (instead of res/raw/). Files in assets/ are not given a resource ID, so you can read them only using AssetManager.

    So you need to use a InputStream to read the audio file before set it to the media player.

    I suggest you to put the audio file in the assets folder like you said you played

    :)

提交回复
热议问题