从 JavaScript 在 WebView 中播放声音 [英] Playing sound in WebView from JavaScript

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

问题描述

我正在尝试播放从资产加载到 WebView 的 JavaScript 代码的声音:

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");

我尝试了以下方法的所有组合如何使用 OGG、MP3 和 WAV 文件播放音频,JavaSrcipt 代码在 assets/www/js/play.js 中:

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();

其中audio_tag是<audio id="audio_tag" src="audio/sound.mp3" preload="auto"></audio>,还尝试了所有路径组合和格式.

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): 错误 (1, -2147483648)

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');

虽然这个问题可能被认为是重复的Android WebView 使用 javascript 播放音频 无论如何,答案无济于事,因为 shouldOverrideUrlLoading 是从来没有打过电话,我想在这里更准确地说明这个问题.

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?

谢谢.

虽然在带有 Android 2.3 的 Galaxy Gio 中进行测试给了我不同的错误集:

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-0617:15:13.829: V/PlayerDriver(95): PendForExec 02-06 17:15:13.829:V/PlayerDriver(95):OsclActiveScheduler::Current 02-0617:15:13.829:V/PlayerDriver(95):StartScheduler 02-0617: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 status=PVMFSuccess02-06 17:15:13.829: V/PVPlayer(95):setDataSource(file:///android_asset/www/audio/sound.mp3) 02-0617:15:13.839: V/PVPlayer(95): prepareAsync 02-06 17:15:13.839:V/PVPlayer(95): 数据源 =file:///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):处理信息事件:类型=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-0617:15:13.849:W/PlayerDriver(95):PVMFInfoErrorHandlingComplete
02-06 17:15:13.939: I/MediaPlayer(4361): 信息 (1,26) 02-0617: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 status=PVMFSuccess 02-0617:15:13.939:V/PlayerDriver(95):发送玩家代码:11 02-0617: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 status=PVMFSuccess02-06 17:15:13.939:V/PlayerDriver(95):发送玩家代码:17 02-0617:15:13.939:V/PlayerDriver(95):handleRemoveDataSource 02-0617:15:13.939:V/PlayerDriver(95):命令完成 02-0617: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): 取消映射文件

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

推荐答案

我遇到了同样的问题,最后我使用 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

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

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