MediaPlayer cant play audio files from program data folder?

后端 未结 5 1724
春和景丽
春和景丽 2021-02-15 18:11

When i record my audio from MIC and store file in /data/data/..... why MediaPlayer can\'t play this file ? If i change destination to / sdcard/..... - all works great. I do so

5条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-15 19:00

    Better practice is to close the stream, like this:

    FileInputStream stream = new FileInputStream(path);
    mediaPlayer.setDataSource(stream.getFD());
    stream.close();
    

    It is noted in the documentation of the method:

    android.media.MediaPlayer.setDataSource(FileDescriptor fd)

    Sets the data source (FileDescriptor) to use. It is the caller's responsibility to close the file descriptor. It is safe to do so as soon as this call returns.

提交回复
热议问题