gpt4 book ai didi

javascript - 从 JavaScript 在 WebView 中播放声音

转载 作者:太空狗 更新时间:2023-10-29 15:48:32 26 4
gpt4 key购买 nike

我正在尝试播放从 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");

我尝试了以下方法的所有组合如何使用 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();

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

但声音没有播放,只有以下一般错误提示可能有问题:

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

从这个信息消息猜测,音频文件的路径对于第二个到第五个情况应该是正确的:

I/StagefrightPlayer(33): setDataSource('file:///android_asset/www/audio/sound.mp3');

虽然这个问题可能被认为是重复的 Android WebView playing audio with javascript并且答案无论如何都没有帮助,因为从未调用过 shouldOverrideUrlLoading,我试图在这里更准确地指定这个问题。

您是否知道可能出了什么问题或如何找出问题所在?

谢谢。

编辑:尽管在 Galaxy Gio 和 Android 2.3 中测试给我不同的错误集:

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

最佳答案

回复:媒体播放器():错误(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 中,在左侧的 Devices 面板中选择您的模拟器,然后选择右侧的 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!

关于javascript - 从 JavaScript 在 WebView 中播放声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9162022/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com