从 JavaScript 在 WebView 中播放声音
我正在尝试从资产加载到 WebView 的 JavaScript 代码播放声音:
WebView web_view = (WebView) findViewById(R.id.web_view);
web_view.getSettings().setJavaScriptEnabled(true);
web_view.setWebChromeClient(new WebChromeClient());
web_view.loadUrl("file:///assets/www/index.html");
我尝试了以下方法的所有组合如何使用 OGG、MP3 和 WAV 文件播放音频,JavaSrcipt 代码位于 asset/www/js/play.js 中:
audio = new Audio("../audio/sound.mp3");
audio.load();
audio.play();
audio = new Audio("./audio/sound.mp3");
audio.load();
audio.play();
audio = new Audio("audio/sound.mp3");
audio.load();
audio.play();
audio = new Audio("file:///android_asset/www/audio/sound.mp3");
audio.load();
audio.play();
audio = new Audio();
audio.src = document.getElementById("audio_tag").src;
audio.load();
audio.play();
其中audio_tag是,也尝试了所有路径和格式的组合。
但声音没有播放,唯一可能出现问题的线索是以下一般错误:
E/MediaPlayer(1530):错误(1,-2147483648)
从这个信息消息猜测,音频文件的路径对于第二个槽第五种情况应该是正确的:
I/StagefrightPlayer(33): setDataSource('file:///android_asset/www/audio/sound.mp3');
虽然这个问题可能被认为是重复的 Android WebView 用 javascript 播放音频 答案无论如何都没有帮助,因为 shouldOverrideUrlLoading 是从未打电话过,我试图在这里更准确地说明这个问题。
您知道可能出了什么问题或者如何找出问题所在吗?
谢谢。
编辑:在使用 Android 2.3 的 Galaxy Gio 中进行测试时,出现了不同的错误集:
02-06 17:15:13.829: V/PlayerDriver(95): AddToScheduler 02-06 17:15:13.829:V / PlayerDriver(95):PendForExec 02-06 17:15:13.829: V/PlayerDriver(95): OsclActiveScheduler::当前 02-06 17:15:13.829:V/PlayerDriver(95):启动调度程序 02-06 17:15:13.829: V/PVPlayer(95): 发送 PLAYER_SETUP 02-06 17:15:13.829: V/PlayerDriver(95): 发送播放器代码: 2 02-06 17:15:13.829: V/PlayerDriver(95):命令已完成 02-06 17:15:13.829: V/PlayerDriver(95):已完成命令 PLAYER_SETUP 状态=PVMFSuccess 02-06 17:15:13.829: V/PVPlayer(95): setDataSource(文件:///android_asset/www/audio/sound.mp3) 02-06 17:15:13.839:V/PVPlayer(95):prepareAsync 02-06 17:15:13.839: V/PVPlayer(95):数据源= 文件:///android_asset/www/audio/sound.mp3 02-06 17:15:13.849: V/PlayerDriver(95): 发送播放器代码: 3 02-06 17:15:13.849: V/PlayerDriver(95):handleSetDataSource 02-06 17:15:13.849: V/PlayerDriver(95):handleSetDataSource-扫描扩展
02-06 17:15:13.849:V / PlayerDriver(95):处理信息事件: PVMFInfoErrorHandlingStart 02-06 17:15:13.849: V/PlayerDriver(95): HandleInformationalEvent:类型 = 26 未处理 02-06 17:15:13.849: W/MediaPlayer(4361): 信息/警告 (1, 26) 02-06 17:15:13.849: V/PlayerDriver(95):命令已完成 02-06 17:15:13.849: V/PlayerDriver(95):已完成命令 PLAYER_SET_DATA_SOURCE 状态=PVMFErrNotSupported 02-06 17:15:13.849: E/PlayerDriver(95): 命令 PLAYER_SET_DATA_SOURCE 已完成,但出现错误或信息
PVMFErrNotSupported 02-06 17:15:13.849: E/MediaPlayer(4361): 错误 (1, -4) 02-06 17:15:13.849: V/PVPlayer(95): run_init s=-2147483648, 已取消=0 02-06 17:15:13.849: V/PlayerDriver(95): 处理信息事件:PVMFInfoErrorHandlingComplete 02-06 17:15:13.849:W/PlayerDriver(95):PVMFInfoErrorHandlingComplete
02-06 17:15:13.939: I/MediaPlayer(4361): 信息 (1,26) 02-06 17:15:13.939:E / MediaPlayer(4361):错误(1,-4)02-06 17:15:13.939: V/PVPlayer(95):重置 02-06 17:15:13.939:V/PlayerDriver(95):发送 玩家代码:18 02-06 17:15:13.939: V/PlayerDriver(95): handleCancelAllCommands 02-06 17:15:13.939: V/PlayerDriver(95): 命令已完成 02-06 17:15:13.939:V/PlayerDriver(95):已完成 命令 PLAYER_CANCEL_ALL_COMMANDS 状态=PVMFSuccess 02-06 17:15:13.939:V/PlayerDriver(95):发送玩家代码:11 02-06 17:15:13.939:V / PlayerDriver(95):handleReset 02-06 17:15:13.939: V/PlayerDriver(95):命令已完成 02-06 17:15:13.939: V/PlayerDriver(95):已完成命令 PLAYER_RESET 状态=PVMFSuccess 02-06 17:15:13.939: V/PlayerDriver(95): 发送玩家代码:17 02-06 17:15:13.939:V / PlayerDriver(95):handleRemoveDataSource 02-06 17:15:13.939:V/PlayerDriver(95):命令已完成 02-06 17:15:13.939:V/PlayerDriver(95):已完成命令 PLAYER_REMOVE_DATA_SOURCE 状态=PVMFSuccess 02-06 17:15:13.939: V/PlayerDriver(95):删除数据源完成 02-06 17:15:13.939: V/PVPlayer(95):取消映射文件
I'm trying to play sound from JavaScript code loaded to WebView from assets:
WebView web_view = (WebView) findViewById(R.id.web_view);
web_view.getSettings().setJavaScriptEnabled(true);
web_view.setWebChromeClient(new WebChromeClient());
web_view.loadUrl("file:///assets/www/index.html");
I tried all combinations of following ways how to play audio with OGG, MP3 and WAV files, the JavaSrcipt code is in assets/www/js/play.js:
audio = new Audio("../audio/sound.mp3");
audio.load();
audio.play();
audio = new Audio("./audio/sound.mp3");
audio.load();
audio.play();
audio = new Audio("audio/sound.mp3");
audio.load();
audio.play();
audio = new Audio("file:///android_asset/www/audio/sound.mp3");
audio.load();
audio.play();
audio = new Audio();
audio.src = document.getElementById("audio_tag").src;
audio.load();
audio.play();
Where audio_tag is <audio id="audio_tag" src="audio/sound.mp3" preload="auto"></audio>
, also tried with all combinations of paths and formats.
But the sound is not playing and only clue what may be wrong is following general error:
E/MediaPlayer(1530): error (1, -2147483648)
Guessing from this info message, the path to audio file should be correct for second trough fifth case:
I/StagefrightPlayer(33): setDataSource('file:///android_asset/www/audio/sound.mp3');
Although this question may be considered as duplicate of
Android WebView playing audio with javascript and the answer is not helping anyhow since shouldOverrideUrlLoading is never called, I'm trying to specify this problem more precisely here.
Do you have any clue what may be wrong or how to find out what is it?
Thank you.
Edit: Testing in Galaxy Gio with Android 2.3 gives me different set of errors though:
02-06 17:15:13.829: V/PlayerDriver(95): AddToScheduler 02-06
17:15:13.829: V/PlayerDriver(95): PendForExec 02-06 17:15:13.829:
V/PlayerDriver(95): OsclActiveScheduler::Current 02-06
17:15:13.829: V/PlayerDriver(95): StartScheduler 02-06
17:15:13.829: V/PVPlayer(95): send PLAYER_SETUP 02-06 17:15:13.829:
V/PlayerDriver(95): Send player code: 2 02-06 17:15:13.829:
V/PlayerDriver(95): CommandCompleted 02-06 17:15:13.829:
V/PlayerDriver(95): Completed command PLAYER_SETUP status=PVMFSuccess
02-06 17:15:13.829: V/PVPlayer(95):
setDataSource(file:///android_asset/www/audio/sound.mp3) 02-06
17:15:13.839: V/PVPlayer(95): prepareAsync 02-06 17:15:13.839:
V/PVPlayer(95): data source =
file:///android_asset/www/audio/sound.mp3 02-06 17:15:13.849:
V/PlayerDriver(95): Send player code: 3 02-06 17:15:13.849:
V/PlayerDriver(95): handleSetDataSource 02-06 17:15:13.849:
V/PlayerDriver(95): handleSetDataSource- scanning for extension
02-06 17:15:13.849: V/PlayerDriver(95): HandleInformationalEvent:
PVMFInfoErrorHandlingStart 02-06 17:15:13.849: V/PlayerDriver(95):
HandleInformationalEvent: type=26 UNHANDLED 02-06 17:15:13.849:
W/MediaPlayer(4361): info/warning (1, 26) 02-06 17:15:13.849:
V/PlayerDriver(95): CommandCompleted 02-06 17:15:13.849:
V/PlayerDriver(95): Completed command PLAYER_SET_DATA_SOURCE
status=PVMFErrNotSupported 02-06 17:15:13.849: E/PlayerDriver(95):
Command PLAYER_SET_DATA_SOURCE completed with an error or info
PVMFErrNotSupported 02-06 17:15:13.849: E/MediaPlayer(4361): error
(1, -4) 02-06 17:15:13.849: V/PVPlayer(95): run_init s=-2147483648,
cancelled=0 02-06 17:15:13.849: V/PlayerDriver(95):
HandleInformationalEvent: PVMFInfoErrorHandlingComplete 02-06
17:15:13.849: W/PlayerDriver(95): PVMFInfoErrorHandlingComplete
02-06 17:15:13.939: I/MediaPlayer(4361): Info (1,26) 02-06
17:15:13.939: E/MediaPlayer(4361): Error (1,-4) 02-06 17:15:13.939:
V/PVPlayer(95): reset 02-06 17:15:13.939: V/PlayerDriver(95): Send
player code: 18 02-06 17:15:13.939: V/PlayerDriver(95):
handleCancelAllCommands 02-06 17:15:13.939: V/PlayerDriver(95):
CommandCompleted 02-06 17:15:13.939: V/PlayerDriver(95): Completed
command PLAYER_CANCEL_ALL_COMMANDS status=PVMFSuccess 02-06
17:15:13.939: V/PlayerDriver(95): Send player code: 11 02-06
17:15:13.939: V/PlayerDriver(95): handleReset 02-06 17:15:13.939:
V/PlayerDriver(95): CommandCompleted 02-06 17:15:13.939:
V/PlayerDriver(95): Completed command PLAYER_RESET status=PVMFSuccess
02-06 17:15:13.939: V/PlayerDriver(95): Send player code: 17 02-06
17:15:13.939: V/PlayerDriver(95): handleRemoveDataSource 02-06
17:15:13.939: V/PlayerDriver(95): CommandCompleted 02-06
17:15:13.939: V/PlayerDriver(95): Completed command
PLAYER_REMOVE_DATA_SOURCE status=PVMFSuccess 02-06 17:15:13.939:
V/PlayerDriver(95): remove datasource complete 02-06 17:15:13.939:
V/PVPlayer(95): unmap file
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
回复:MediaPlayer( ):错误 (1,-2147483648)
Android MediaPlayer 需要媒体文件是世界可读的,因此它们不能驻留在 Eclipse 项目内的“assets”文件夹中。将音频/视频文件推送到设备外部存储上。为此,使用模拟器,使用 Eclipse 中的 DDMS Perspective(当模拟器运行时,转到 Window->Open Prospective->Other->DDMS)创建一个文件夹并将文件推送到 SD 卡映像或内部(不可移动)存储。
DDMS参考:
http://developer.android.com/guide/developing/debugging/ddms.html
在 DDM 中,在左侧的“设备”面板中选择您的模拟器,然后选择右侧的“FileExplorer”选项卡,查找名为 /mnt/sdcard/ 的文件夹,其中包含 SDCard 内容,或者, /Android/data/package_name/files/ 用于标准化应用程序的存储区域。因此,在上面的示例中,如果您在 SD 卡上创建文件夹 myaudio,则文件名路径将为:
audio = new Audio("/mnt/sdcard/myaudio/sound.mp3");
参考:
http://developer.android.com/guide/topics/data /data-storage.html#filesExternal
并且不要放弃 HTML5!
Re: MediaPlayer( ): Error (1,-2147483648)
The Android MediaPlayer needs the media files to be world-readable so they can’t reside in the “assets” folder inside the Eclipse project. Push the audio/video files onto the device external storage. To do this, with an emulator, use the DDMS Perspective in Eclipse (while your emulator is running go to Window->Open Prospective->Other->DDMS) to create a folder and push files onto SD card image or internal (non-removable) storage.
Reference on DDMS:
http://developer.android.com/guide/developing/debugging/ddms.html
In DDMs, select your emulator in the Devices panel on the left, and then choose the FileExplorer tab on the right, look for a folder named /mnt/sdcard/ which contains the SDCard contents, or, alternatively /Android/data/package_name/files/ for the standardized application’s storage area. So, in the above example, if you create a folder myaudio on the SDcard the filename path would be:
audio = new Audio("/mnt/sdcard/myaudio/sound.mp3");
Reference:
http://developer.android.com/guide/topics/data/data-storage.html#filesExternal
And don't give up on HTML5!
我遇到了同样的问题,最后我使用phonegap和cordova,您可以轻松地播放和录制音频:
http://docs.phonegap.com/en/2.9.0/cordova_media_media .md.html
I had the same problem and finally i'm using phonegap and cordova where you can play and record audios easily:
http://docs.phonegap.com/en/2.9.0/cordova_media_media.md.html
这取决于您的 Android 设备和可用的编解码器...但路径可能不正确。您使用 Phonegap 吗?您的代码可以在桌面浏览器上运行吗?
我正在使用类似的代码:
我的 HTML 音频标签是这样的:
我正在使用 mp3 音频文件格式,因为它适用于 iOS 和某些 Android 设备。
另请注意,音频标签元素并非在所有 Android 版本上都可用。正如您在此处看到的,它适用于 Android 2.3 及更高版本。
It depends on your Android device and the available codecs... But probably the path is not correct. Are you using Phonegap? Does your code play on desktop browsers?
I'm using a similar code:
And my HTML audio tag is this:
I'm using mp3 audio file format because it works for iOS and some Android devices.
Also, notice that Audio tag element is not available on all android versions. As you can see here, it works for Android 2.3 and up.