Question

I am using Monaca.mobi to build a hybrid app. When I build the app for IOS everything is fine; however, when I build it for an android device (Nexus 7) audio does come through. In the Monaca debugger; however, the audio works fine. Is there something about android devices that I am not aware about, maybe some permissions of the app?

Sound is played through an angularJS function called on certain button clicks. I know that this code is correct, just thought I might share it:

function DontAsk($scope){
$scope.play = function(){
    var audio = new Audio();
        audio.src = 'sounds/DontEventAsk.mp3';
    audio.play();
}}

Thanks for any insight.

Was it helpful?

Solution

your above code is only working with iOS. For Android, the path to your local audio file is not recognized. The following code will work for both OSes. I've already tested with the built app too.

$scope.play= function(){
    var os = navigator.platform;
    if (os=='iPhone'){
        var url = "sounds/DontEventAsk.mp3";
    }
    else{
        var url = getPhoneGapPath() + "sounds/DontEventAsk.mp3";
    }
    var my_media = new Media(url,
        // success callback
        function() {
            console.log("playAudio():Audio Success");
        },
        // error callback
        function(err) {
            console.log("playAudio():Audio Error: "+JSON.stringify(err));
    });

    // Play audio
    my_media.play();
}

OTHER TIPS

The big question here is what browser does the Monaca.mobi app use internally? The default Android browser is notorious for not supporting newer codecs like Audio that require HTML5. You might be better off setting some kind of flag that the app can watch and then use the app to play the sound instead of relying on the browser.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top