播放assets目录下的音频文件
我有以下代码:
AssetFileDescriptor afd = getAssets().openFd("AudioFile.mp3");
player = new MediaPlayer();
player.setDataSource(afd.getFileDescriptor());
player.prepare();
player.start();
问题是,当我运行此代码时,它开始按字母顺序播放资产目录中的所有音频文件,而不是仅播放我请求的音频文件。我做错了什么?有没有更好的方法来播放资产目录中的音频文件?
后续问题: 将音频文件保存在 asset 目录中和将它们保存在 res/raw 目录中有区别吗?除此之外,如果它们位于资产目录中,则它们不会获得 id。如果我将音频文件移动到 res/raw 文件夹,那么我在重用 MediaPlayer
时会遇到问题,因为 setDataSource()
没有 id 参数。我找不到处理此类问题的良好指南。
I have the following code:
AssetFileDescriptor afd = getAssets().openFd("AudioFile.mp3");
player = new MediaPlayer();
player.setDataSource(afd.getFileDescriptor());
player.prepare();
player.start();
The problem is that, when I run this code, it starts playing all the audio files in the assets directory, in alphabetical order instead of just playing the audio file I requested. What am I doing wrong? Is there a better way to play audio files from the assets directory?
Follow-up question:
Is there a difference between keeping audio files in the assets directory and keeping them in the res/raw directory? Besides the fact that they don't get ids if they are in the assets directory. If I move the audio files to the res/raw folder then I have a problem with reusing MediaPlayer
s because there is no id parameter for setDataSource()
. I can't find a good guideline for handling this kind of problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
该功能将正常工作:)
This function will work properly :)
这是我的静态版本:
Here my static version:
启动声音
方法
start sound
method
修复上述播放和暂停功能
Fix of above function for play and pause
这对我有用:
在主要活动中,创建时:
如何在代码中使用(单击按钮时):
this works for me:
In main activity, on create:
how to use in code (on button click):