0

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.

Andi Pavllo
  • 2,506
  • 4
  • 18
  • 30
benjipelletier
  • 653
  • 5
  • 11
  • 29

2 Answers2

1

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.

MBielski
  • 6,628
  • 3
  • 32
  • 43
1

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();
}
Tamura
  • 1,788
  • 1
  • 14
  • 10
  • Hi, I have implemented this, but it still only seems to work for iPhone. In Android calling the function gives an error in the Monaca debugger which is normal since the getPhoneGapPath() is undefined I assume, but when the app is built no sound comes out. Can you please share with me the implementation you have succeeded with? Thanks, Ben – benjipelletier May 09 '14 at 12:47
  • [Here](http://pastebin.com/W9ymw2UE) is what one function looks like. I don't believe that there is a problem with this function because it works fine on an iPhone – benjipelletier May 10 '14 at 13:50
  • Hi, again. I think that you might have forgotten to add the function getPhoneGapPath() in you answer because it does not seem to be built in function. is this the correct function: [stated in the answer to this question](http://stackoverflow.com/questions/11175489/ios-phonegap-media-object-how-do-i-access-a-resource-in-the-www-folder) – benjipelletier May 11 '14 at 14:37
  • After playing around I get it to work. Thanks for this great learning process though. – benjipelletier May 11 '14 at 15:04