MediaPlayer error -2147483648 when playing file on internal storage

随声附和 提交于 2019-12-02 22:11:45
Thunder Rabbit

Thanks to gtkandroid:

Instead of mPlayer.setDataSource(mFile); I did this:

FileInputStream fileInputStream = new FileInputStream(mFile);
mPlayer.setDataSource(fileInputStream.getFD());         
fileInputStream.close();
mPlayer.prepare();
KitKat

Tim Crowley is right. 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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!