Play sound on Phonegap app for Android

后端 未结 6 1172
时光说笑
时光说笑 2020-11-29 05:47

I try to play an mp3 file. This works if I change the path to the file on my local webserver, but if I run this on an Android device the sound is not played and no error is

6条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-29 06:04

    I had the same problem and finally got it working, with me the problem lied with the scope of my media variable. I got it working as follows:

    (function () {
    
        "use strict";
         var media;
         document.addEventListener('deviceready', onDeviceReady.bind(this), false);
    
    function onDeviceReady() {
        media = new Media("/android_asset/www/sounds/wat is je wens.mp3", null, function (e) {
        alert('Media Error');
        alert(JSON.stringify(e));
        });
    
        alert('deviceready')
    };
    function onStart() {
        media.play();
        alert('yay')
    
    }
    };
    

    Essentially I made sure that the scope of my media variable was sort of global by declaring it outside of onDeviceReady, but that Media wasn't called before the deviceready event.

提交回复
热议问题