在我的应用程序的 /res/raw/ 文件夹中,我有:
假设给定其中一个字符串,比如
这是我到目前为止看过的内容:
file1.ogg file2.ogg ... fileN.ogg
在本地数据库中,我有文件名的引用,例如:file1 file2 ... fileN
。假设给定其中一个字符串,比如
fileM
,如何播放包含在 fileM.ogg
中的音频?希望提供相关的Java代码。这是我到目前为止看过的内容:
String name = "plumber";
String link = "/res/raw/" + name + ".ogg";
try {
mp.setDataSource(link); //earlier: mp = new MediaPlayer();
mp.prepare();
mp.start();
} catch (Exception e) {
Toast.makeText(PlayScreen.this, "ERROR: audio player not working.", Toast.LENGTH_LONG).show();
}
我在 toast 中收到错误信息。此外,我已经尝试过不在字符串 link
中加入 "/res/raw"。
谢谢。
MediaPlayer.create(context, Uri.parse(path))
,这比实例化MediaPlayer
、设置数据源,然后调用prepare
更加简洁。 - ahmadPH