MediaPlayer.setDataSource(String) not working with local files

前端 未结 6 1042
一个人的身影
一个人的身影 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 21:06

    When dealing with a raw resource, you should rely on the following constructor:

    public static MediaPlayer create (Context context, int resid)

    Convenience method to create a MediaPlayer for a given resource id. On success, prepare() will already have been called and must not be called again.

    The code looks like

    mediaPlayer = MediaPlayer.create(this, R.raw.test0);
    mediaPlayer.start();
    

    Don't forget to call mediaPlayer.release() when you're done with it.

    (source)

提交回复
热议问题