播放本地声音文件

播放本地声音文件

本文介绍了Phonegap - 无法播放本地声音文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我尝试使用Phonegap播放本地声音文件。这工作正常超过http,但不适用于本地文件。我可以看到设备上的文件,并手动播放它们,但不能这样做与Phonegap的API。我尝试过:I'm trying to play a local sound file with Phonegap. This works fine over http, but doesn't for local files. I can see the files on the device and play them manually but cannot do so with Phonegap's API. I've tried: .mp3和.wav文件格式。 将文件放在www /(使用路径 /android_asset/www/sound.mp3 )和/ sdcard / 更改协议(带或不带文件://)。 $.mp3 and .wav file formats.Putting the files in www/ (with path/android_asset/www/sound.mp3) and /sdcard/ (with path sound.mp3).Varying the protocol (with or without file://).但似乎没有什么工作,成功和错误回调都不会被调用。我也确保文件实际存在,然后尝试播放它。这是我的代码:But nothing seems to work, neither the success nor the error callback gets called. I'm also making sure the file actually exists before trying to play it. Here's my code:window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fs) { var directoryReader = fs.root.createReader(); directoryReader.readEntries(function(entries) { for (var i=0; i<entries.length; i++) { if (entries[i].isFile && entries[i].name.match(/.*\.wav$/)){ var audio = new Media(entries[i].toURL().substr(19), function(){console.log('success');}, function(){console.log('error');}); console.log("!!!!! " + audio.getDuration()); audio.play(); audio.release(); break; } } }, function (error) { alert(error.code); }) }, function (error) { alert(error.code); }); audio.getDuration 始终返回-1。我在运行Android 2.3.3的Android Nexus S上使用Phonegap 1.9,该应用的目标是API级别2.1。audio.getDuration always returns -1. I'm using Phonegap 1.9 on an Android Nexus S running Android 2.3.3 and the app is targeted at API level 2.1.任何想法?推荐答案 getDuration 当同步调用时返回-1。您需要在setInterval循环中调用它,如此处所示 http ://docs.phonegap.com/en/1.9.0/cordova_media_media.md.html#media.getDuration 以获取正确的时间。getDuration returns -1 when you call it synchronously. You need to call it in a setInterval loop like shown here http://docs.phonegap.com/en/1.9.0/cordova_media_media.md.html#media.getDuration to get the correct duration.我不要以为你可以从资产/ www播放文件,但sdcard上的文件工作完全正常。您需要确保不传递FileEntry对象的完整路径。您需要修剪起始路径(file:/// mnt / sdcard /),因为媒体播放器插件本身添加了它。我发现,一个小时左右后抓头!I don't think you can play files from assets/www, but files on the sdcard work perfectly fine. You need to make sure that you don't pass the full path from the FileEntry object. You need to trim off the starting path (file:///mnt/sdcard/) since the media player plugin adds that by itself. I found that out after an hour or so of head scratching! 这篇关于Phonegap - 无法播放本地声音文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-22 13:42