Tengo un archivo .wav
en mi carpeta www
. Estoy usando jQuery con el siguiente código. Las alertas se apagan pero el sonido no se reproduce. ¿Estoy haciendo algo mal?
$(document).ready(function () { window.alert("READY!"); document.addEventListener("deviceready", onDeviceReady, true); function onDeviceReady(){ window.alert("OK@!"); var snd = new Media("test.wav"); snd.play(); } });
El sonido simplemente no funciona.
Intenta dar una ruta local absoluta. P.ej
new Media (“/ android_asset / www / test.wav”);
Eso debería funcionar en Android. Esperemos que arreglen esto en PhoneGap, ya que es un error en el soporte de dispositivos cruzados.
Puede usar window.location.pathname
para obtener la ruta de su aplicación en cualquier aplicación PhoneGap. De esta forma, no es necesario codificarlo para Android. Se verá algo así en el iPhone:
/var/mobile/Applications/{GUID}/{appname}.app/www/index.html
Y esto en Android:
/android_asset/www/index.html
Quite el /index.html
, /index.html
file://
y anexe su archivo test.wav
.
Demostración: http://jsfiddle.net/ThinkingStiff/r7eay/
Código:
function getPhoneGapPath() { var path = window.location.pathname; path = path.substr( path, path.length - 10 ); return 'file://' + path; }; var snd = new Media( getPhoneGapPath() + 'test.wav' );
Tengo otra versión de getPhonegapPath, detecta cuando usas Android:
function getPhoneGapPath() { var path = window.location.pathname; path = path.substr( path, path.length - 10 ); var devicePlatform = device.platform; if(devicePlatform=="Android"){ path = 'file://'+path; } return path; };