简体   繁体   English

Android:按文件名播放/res/raw 中的音频文件

[英]Android: playing audio files in /res/raw by file name

In my application's /res/raw/ folder, I have: file1.ogg file2.ogg ... fileN.ogg在我的应用程序的 /res/raw/ 文件夹中,我有: file1.ogg file2.ogg ... fileN.ogg

In a local database, I have the references to the file names, ie file1 file2 ... fileN .在本地数据库中,我引用了文件名,即file1 file2 ... fileN

Given one of those strings, say fileM , how can I play the audio contained in fileM.ogg ?给定这些字符串之一,比如fileM ,我如何播放fileM.ogg包含的音频? Looking for the relevant Java code here.在此处查找相关的 Java 代码。

Here's what I looked at so far:这是我到目前为止所看到的:

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(); 
}

I get the error message in the toast.我在吐司中收到错误消息。 Also I've tried the link string without "/res/raw" in it.我也试过没有“/res/raw”的link字符串。

Thank you.谢谢你。

MediaPlayer player = new MediaPlayer();
String path = "android.resource://" + getPackageName() + "/raw" + filename;
try {
     player.setDataSource(context, Uri.parse(path));
     player.prepare();
     player.start();
} catch (IOException e) {
     e.printStackTrace();
}

try it with or without the extention ".mp3"尝试使用或不使用扩展名“.mp3”

hope i could help you希望我能帮助你

i think that should be like that as sample at http://en.kuma-de.com/blog/2011-11-14/341我认为这应该是http://en.kuma-de.com/blog/2011-11-14/341 上的示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM