从资产目录中播放的音频文件 [英] Play audio file from the assets directory

查看:253
本文介绍了从资产目录中播放的音频文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的code:

  AssetFileDescriptor AFD = getAssets()openFd(AudioFile.mp3)。
球员=新的MediaPlayer();
player.setDataSource(afd.getFileDescriptor());
播放器prepare()。
player.start();
 

现在的问题是,当我运行此code,它开始播放的所有音频文件中的资产目录中,按字母顺序排列,而不是只是在玩我所要求的音频文件。我究竟做错了什么?有没有更好的办法从资产目录中播放的音频文件?

跟进的问题: 是否有保留的资产目录中的音频文件,并让他们在res / raw目录之间的差异?除了一个事实,即他们没有得到IDS,如果他们在资产目录中。如果我将音频文件的RES /原始文件夹,然后我有重复使用的问题的MediaPlayer 是因为存在对的setDataSource没有id参数() 。我无法找到一个很好的指引处理这类问题。

解决方案

<$p$p><$c$c>player.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());

  

如果您在资产只有一个文件,你的版本会工作   目录。资产目录的内容是不实际真实文件   在磁盘上。所有这些都放在一起了一个又一个。所以,如果你这样做   未指定从哪里开始,如何读取的字节数,玩家会   读至年底(也就是,将继续播放的所有文件的资产   目录)

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 MediaPlayers because there is no id parameter for setDataSource(). I can't find a good guideline for handling this kind of problem.

解决方案

player.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());

Your version would work if you had only one file in the assets directory. The asset directory contents are not actually 'real files' on disk. All of them are put together one after another. So, if you do not specify where to start and how many bytes to read, the player will read up to the end (that is, will keep playing all the files in assets directory)

这篇关于从资产目录中播放的音频文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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