songsList.add("raw/yequ.mp3");找不到音乐
时间: 2024-02-16 08:02:30
浏览: 72
在 Android 中,使用 `[raw](https://geek.csdn.net/educolumn/61e31fceefc94edcbbed7f53fd269b6c?spm=1055.2569.3001.10083)/` 目录下的[音频](https://geek.csdn.net/educolumn/137d6288ee64fa29a4a76a9adf01580c?spm=1055.2569.3001.10083)文件时,需要使用 `R.raw` 来访问资源。因此,你需要将[代码](https://geek.csdn.net/educolumn/1572ef9b473b4e00f6b2bf6d428b7c27?spm=1055.2569.3001.10083)中的 `songsList.add("raw/yequ.mp3");` 修改为 `songsList.add("android.resource://" + [get](https://geek.csdn.net/educolumn/31aebce56f4ecc5451aac3a194ff6259?spm=1055.2569.3001.10083)PackageName() + "/" + R.raw.yequ);`,其中 `[get](https://geek.csdn.net/educolumn/31aebce56f4ecc5451aac3a194ff6259?spm=1055.2569.3001.10083)PackageName()` 为获取应用包名的方法,`R.raw.yequ` 表示 `res/raw` 目录下 `yequ.mp3` 文件的资源 ID。这样修改后,应该能够正确地访问 `yequ.mp3` 文件了。
相关问题
我已经按照上面的步骤修改了,为什么还是报错Failed to open file 'android.resource://com.example.alarmclock/yequ'. (No such file or directory)
这个错误通常是由于文件名拼写错误或者文件不存在导致的。你需要检查代码中使用的文件名是否正确,并确保文件存在于正确的位置。
在你的代码中,使用了以下代码来访问音频资源:
```java
int resId = getResources().getIdentifier("yequ", "raw", getPackageName());
String fileName = getResources().getResourceEntryName(resId);
songsList.add("android.resource://" + getPackageName() + "/" + fileName);
这里,`getResources().getIdentifier("yequ", "raw", getPackageName())` 方法用于获取 `yequ.mp3` 文件在 `res/raw`
mediaPlayer.setDataSource(R.raw.yequ);
`mediaPlayer.setDataSource()` 方法用于设置 MediaPlayer 播放的数据源。该方法的参数可以是文件路径、URI 或 FileDescriptor 等。
如果你将音频文件放在 `res/raw` 目录下,可以按照以下方式设置数据源:
```java
mediaPlayer.setDataSource(context, R.raw.yequ);
```
相关推荐
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)