我試圖從資產加載到WebView中的JavaScript代碼播放聲音:從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代碼是在資產/網絡/ 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是從來沒有所謂的幫助,我想更精確地在這裏指定了這個問題。
你有什麼線索可能是錯誤的或如何找出它是什麼?
謝謝。
編輯:測試在銀河吉奧了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
想我發現了一個奇妙的辦法解決這個。請檢查我的答案在這裏** http://stackoverflow.com/a/40634355/3866399** – 2016-11-16 14:16:01