机器人:按文件名播放音频文件在/ RES /生 [英] Android: playing audio files in /res/raw by file name

查看:192
本文介绍了机器人:按文件名播放音频文件在/ RES /生的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序的/ RES /生/文件夹,我有: file1.ogg file2.ogg ... fileN.ogg

在本地数据库中,我必须将文件名的引用,即文件1文件2 ... fileN

鉴于这些字符串中的一个,说 fileM ,我怎么能起到包含在 fileM.ogg 音频?寻找相关的Java code在这里。

下面是我看着迄今:

 字符串名称=管道工;
字符串link =/ RES /生/+姓名+.OGG尝试{
    mp.setDataSource(链接); //前面熔点为新的MediaPlayer();
    MP prepare()。
    mp.start();
}赶上(例外五){
    Toast.makeText(PlayScreen.this,ERROR:音频播放器无法正常工作。Toast.LENGTH_LONG).show();
}

我得到了敬酒的错误消息。此外,我已经试过了链接字符串而不为/ RES /生。

感谢您。


解决方案

 的MediaPlayer播放器=新的MediaPlayer();
字符串路径=android.resource://+软件包名+/原始+文件名;
尝试{
     player.setDataSource(上下文,Uri.parse(路径));
     播放器prepare()。
     player.start();
}赶上(IOException异常五){
     e.printStackTrace();
}

带或不带延伸.MP3

试试吧

希望我可以帮你。

我觉得应该是这样,截至 HTTP示例:// en.kuma-de.com/blog/2011-11-14/341

In my application's /res/raw/ folder, I have: file1.ogg file2.ogg ... fileN.ogg

In a local database, I have the references to the file names, i.e. file1 file2 ... fileN.

Given one of those strings, say fileM, how can I play the audio contained in fileM.ogg? Looking for the relevant Java code here.

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.

Thank you.

解决方案

MediaPlayer player = new MediaPlayer();
String path = "android.resource://"+packagename+"/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"

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

这篇关于机器人:按文件名播放音频文件在/ RES /生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆