Android Assets-Folder:它始终按字母顺序获取第一个文件
我是 Java/Android 编程新手,所以请耐心等待。
我尝试播放位于 asset 文件夹中的 mp3。我知道 /res/raw/ 文件夹还有另一种方法,但使用资产文件夹,因为稍后我将尝试通过字符串访问该文件。
此代码可以播放 mp3 文件:
try
{
MediaPlayer mp = new MediaPlayer();
FileDescriptor sfd = getAssets().openFd("song.mp3").getFileDescriptor();
mp.setDataSource(sfd);
mp.prepare();
mp.start();
}
catch(Exception e) {}
现在的问题是:在同一个资产文件夹中存储了另一个 mp3 文件。虽然我指定了要使用的 mp3 的名称,但它采用字母表中第一个的名称。例如,另一个文件名为“music.mp3”,它会播放这个文件。将其重命名为“worldmusic.mp3”它将播放“song.mp3”。将“worldmusic.mp3”重命名回“music.mp3”,它将再次使用此 mp3。另一个测试:将“song.mp3”重命名为其他名称,以便应用程序可以找到上面代码指定的内容,这将导致不播放任何歌曲。所以这意味着歌曲名必须存在,尽管它在字母表中任意选取歌曲。
我正在使用eclipse的AVD模拟器进行测试。但我认为在真实设备上的行为是相同的。
有人对这个问题有想法吗?
I'm new in Java/Android programming, so please have patience with me.
I try to play a mp3 which is locate und the assets folder. I know there is another way with the /res/raw/ folder, but use the assets-folder because later I'll try to access the file by String.
This code works to play a mp3-file:
try
{
MediaPlayer mp = new MediaPlayer();
FileDescriptor sfd = getAssets().openFd("song.mp3").getFileDescriptor();
mp.setDataSource(sfd);
mp.prepare();
mp.start();
}
catch(Exception e) {}
Now the problem: In the same assets-folder is another mp3 file stored. Though I specify the name of the mp3 to use it take the one which comes first in alphabet. E.g. the other file is named "music.mp3" it plays this one. Renaming it to "worldmusic.mp3" it will play "song.mp3". Rerename "worldmusic.mp3" back to "music.mp3" it will take this mp3 again. Another test: Renaming "song.mp3" to something other so the application can find whats specify by the code above will result that no song is played. So this means the songname have to exist, although it take arbitrary the song first in alphabet.
I'm testing with the AVD emulator of eclipse. But I think the behaviour would be the same on a real device.
Have someone an idea about this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不认为使用 FileDescriptor 是执行此操作的正确方法。尝试使用
.create()
和 Uri 代替:I don't believe using FileDescriptor is the right way to do this. Try using
.create()
and a Uri instead:不知道为什么,但 URI 语法似乎不适用于资产。尝试获取 AssetFileDescriptor,如相关问题中的回答:
从资产目录
Not sure why, but the URI syntax doesn't seem to work for the assets. Try getting the AssetFileDescriptor instead, as answered in the related question:
Play audio file from the assets directory