从 JavaScript 在 WebView 中播放声音

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9162022/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 05:48:04  来源:igfitidea点击:

Playing sound in WebView from JavaScript

javascriptandroidwebviewaudio

提问by Blackhex

I'm trying to play sound from JavaScript code loaded to WebView from assets:

我正在尝试从从资产加载到 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");

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:

我尝试了以下方法的所有组合如何使用 OGG、MP3 和 WAV 文件播放音频,JavaSrcipt 代码在 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.

在 audio_tag 所在的位置<audio id="audio_tag" src="audio/sound.mp3" preload="auto"></audio>,还尝试了所有路径和格式的组合。

But the sound is not playing and only clue what may be wrong is following general error:

但是声音没有播放,唯一可能是错误的线索是以下一般错误:

E/MediaPlayer(1530): error (1, -2147483648)

E/MediaPlayer(1530): 错误 (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 javascriptand the answer is not helping anyhow since shouldOverrideUrlLoading is never called, I'm trying to specify this problem more precisely here.

虽然这个问题可能被认为是Android WebView 使用 javascript 播放音频的重复, 而且答案无论如何也无济于事,因为 shouldOverrideUrlLoading 从未被调用过,但我试图在这里更准确地指定这个问题。

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:

编辑:使用 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::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

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() OsclActiveScheduler::Current 02-06 17:15:13.829: V/PlayerDriver(95): StartScheduler 02-06 17:15:13.829: V/PVPlayer(95): 发送 PLAYER_SETUP 02-06 17:129:13。 PlayerDriver(95): 发送播放器代码: 2 02-06 17:15:13.829: V/PlayerDriver(95): CommandCompleted 02-06 17:15:13.829: V/PlayerDriver(95): 命令完成 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): 数据源 = 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): HandleInformationalEvent: PVMFInfoErrorHandlingStart 02-06 17:15:13.849: V/PlayerDriver(95): HandleInformationalEvent: type=26 UNHANDLED:073-1056 W/MediaPlayer(4361): 信息/警告 (1, 26) 02-06 17:15:13.849: V/PlayerDriver(95): CommandCompleted 02-06 17:15:13.849: V/PlayerDriver(95): 完成命令PLAYER_SET_DATA_SOURCE status=PVMFErrNotSupported 02-06 17:15:13.849: E/PlayerDriver(95): 命令 PLAYER_SET_DATA_SOURCE 完成,但有错误或信息
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=-21474830402led -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): 信息 (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):handleCancelAlls 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-059:13/17 PlayerDriver(95): 发送玩家代码: 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): 完成命令 PLAYER_RESET status=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): CommandCompleted 02-06 17:15:13.939: V/PlayerDriver(95): 完成命令 PLAYER_REMOVE_DATA_SOURCE status=PVMF6:139. /PlayerDriver(95): 删除数据源完成 02-06 17:15:13.939: V/PVPlayer(95): 取消映射文件

回答by titicaca

Re: MediaPlayer( ): Error (1,-2147483648)

回复:MediaPlayer():错误 (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.

Android MediaPlayer 需要媒体文件是全局可读的,因此它们不能驻留在 Eclipse 项目内的“assets”文件夹中。将音频/视频文件推送到设备外部存储。为此,使用模拟器,在 Eclipse 中使用 DDMS Perspective(当您的模拟器运行时,转到 Window->Open Prospective->Other->DDMS)创建一个文件夹并将文件推送到 SD 卡映像或内部(非移动存储。

Reference on DDMS: http://developer.android.com/guide/developing/debugging/ddms.html

关于 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:

在 DDM 中,在左侧的设备面板中选择您的模拟器,然后选择右侧的 FileExplorer 选项卡,查找名为 /mnt/sdcard/ 的文件夹,其中包含 SDCard 内容,或者 /Android/data/package_name/ files/ 用于标准化应用程序的存储区域。因此,在上面的示例中,如果您在 SD 卡上创建一个文件夹 myaudio,文件名路径将是:

audio = new Audio("/mnt/sdcard/myaudio/sound.mp3");

audio = new Audio("/mnt/sdcard/myaudio/sound.mp3");

Reference: http://developer.android.com/guide/topics/data/data-storage.html#filesExternal

参考:http: //developer.android.com/guide/topics/data/data-storage.html#filesExternal

And don't give up on HTML5!

不要放弃 HTML5!

回答by nachete70

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

我遇到了同样的问题,最后我使用 phonegap 和cordova,您可以在其中轻松播放和录制音频:http://docs.phonegap.com/en/2.9.0/cordova_media_media.md.html

回答by yeradis

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?

这取决于您的 Android 设备和可用的编解码器...但可能路径不正确。你在用Phonegap吗?您的代码可以在桌面浏览器上播放吗?

I'm using a similar code:

我正在使用类似的代码:

var myAudio = document.getElementsByTagName('audio')[0];
myAudio.pause();
myAudio.src = file;
myAudio.play();

And my HTML audio tag is this:

我的 HTML 音频标签是这样的:

<audio id="audio" src="" type="audio/mpeg" preload="metadata" ontimeupdate="timeUpdate()"
   ondurationchange="durationChange()" onerror="musicError()" onended="musicEnded()">
<embed src="" height=50 width=100></embed>

I'm using mp3 audio file format because it works for iOS and some Android devices.

我使用 mp3 音频文件格式,因为它适用于 iOS 和一些 Android 设备。

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.

另外,请注意 Audio 标签元素并非在所有 android 版本上都可用。正如您在此处看到的,它适用于 Android 2.3 及更高版本。