简体   繁体   English

Android-从资产文件夹播放声音时出现问题

[英]Android - Problem playing sounds from assets folder

i have 5 mp3 files stored on the assets folder. 我在资产文件夹中存储了5个mp3文件。 The files are all 25 KB. 文件均为25 KB。
I load the files using: 我使用以下方式加载文件:

manager = context.getAssets();
this.inputStream = manager.openFd(fileName).createInputStream();

Whenever i try to play the files, the sounds are all messed up like they were mixed or something. 每当我尝试播放文件时,声音都会像被混在一起一样被弄乱。 I've zipaligned the app already but with no results. 我已经对该应用程序进行了zipalign,但是没有结果。
anny help about this issue? 安妮对此问题有帮助吗? Thanks in advance 提前致谢

After some research i've found the awnser myself. 经过一番研究,我自己找到了遮阳篷。 the problem was i was using the following method to set the MediaPlayer's datasource: 问题是我正在使用以下方法来设置MediaPlayer的数据源:

inputStream = manager.openFd(fileName).createInputStream();    
player.setDataSource(inputStream.getFD());

Wich is just a call to setDataSource(fd, 0, 0x7ffffffffffffffL); Wich只是对setDataSource(fd, 0, 0x7ffffffffffffffL);的调用setDataSource(fd, 0, 0x7ffffffffffffffL); , passing the min offset and this arbitrary length, causing the sounds to be played all mixed. ,传递最小偏移量和此任意长度,导致声音混合播放。
When using the following code everything worked fine: 使用以下代码时,一切工作正常:

AssetFileDescriptor descriptor = manager.openFd(fileName);
long start = descriptor.getStartOffset();
long end = descriptor.getLength();
player.setDataSource(descriptor.getFileDescriptor(), start,end);

You can also try playing them from the res/raw folder: 您也可以尝试从res / raw文件夹播放它们:

MediaPlayer p=MediaPlayer.create(this, R.raw.soundid);
p.start();
  1. For start try to eliminate one potential problem: compare inputStream with the original file. 首先,尝试消除一个潜在的问题:将inputStream与原始文件进行比较。

  2. Try opening and playing files directly. 尝试直接打开和播放文件。

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

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