文件未从 Android 中的内部存储播放

file not playing from Internal storage in Android

这里是设置数据源的字符串

/data/data/com.player/app_player/file.mp3

正在获取 E/Exception:setDataSource 失败。

代码如下:

mediaPlayer.setDataSource(context, Uri.parse("/data/data/com.player/app_player/file.mp3"));

我使用此代码存储了该文件

getContext().getDir("player", Context.MODE_PRIVATE)

与/data/data/com.player/app_player

相同

使用内容://data/data/com.player/app_player/file.mp3 无效。

我是这样解决的

 String musicUrl = "";
 if (songSavedInDB()) {
            musicUrl = "here is any file path(Internal or external)"
            FileInputStream fileInputStream = null;
            try {
                fileInputStream = new FileInputStream(musicUrl);
                mPlayer.setDataSource(fileInputStream.getFD());
                fileInputStream.close();
                mPlayer.prepare();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

        } else {
            musicUrl = "here is url";
            try {
                mPlayer.setDataSource(getContext(), Uri.parse(musicUrl));
                mPlayer.prepareAsync();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }