No sound played by device using Cordova's Media object.

9 posts, 0 answers
  1. Alfredo
    Alfredo avatar
    6 posts
    Member since:
    Nov 2013

    Posted 25 Nov 2013 Link to this post

    Hello,

    my_media.play() isn't playing any sounds. App is a small game for toddlers and some sound effects must be reproduced at certain points during program execution. Made sure no my_media.play() is called before Cordova is fully loaded (first call to my_media.play() is made inside the OnDeviceReady callback function by means of the playAudio() function), however no sound is ever played.

    Checked the provided example  in the "samples" section but frankly speaking that example ends up being more an OOP tutorial than a real world example on how to use the Cordova's Media object. For example, contrary to the sample, many times in games sounds come packaged with the app, so in that case how are you supposed to reference the location of such file? If you check Cordova's docs, it specifies something like "/android_asset/www/soundsfolder/sound.mp3", but that's when you create a stand-alone Cordova/PhoneGap project.

    So the question is how do you reference the location of sound files in an Icenium project? The file format must be .wav or .mp3?

    Your help will be appreciated.

    Alfredo.
  2. Steve
    Admin
    Steve avatar
    10941 posts

    Posted 27 Nov 2013 Link to this post

    Hi Alfredo,

    Using the /android_asset/www/ path for Android is correct. You can find more information and code snippet about our inquiry in the following forum thread.

    Regards,
    Steve
    Telerik
    You've missed the Icenium Visual Studio Integration keynote? It has been recorded and posted here.
    Looking for tips & tricks directly from the Icenium team? Check out our blog!
    Share feedback and vote for features on our Feedback Portal.
  3. Alfredo
    Alfredo avatar
    6 posts
    Member since:
    Nov 2013

    Posted 27 Nov 2013 Link to this post

    Thank´s Steve for the reply but even correcting the URL no sound is played. Nothing. Muted. Can you give it a try? Please?
  4. Steve
    Admin
    Steve avatar
    10941 posts

    Posted 28 Nov 2013 Link to this post

    Hi Alfredo,

    The code snippet from my post is used in a test, so the code works. How many devices did you test your project on? Are you by any chance using an HTC with Android 2.x (really problematic devices). If you're still having problems, please share your device model and Android version and we would test against it (if available locally).

    Regards,
    Steve
    Telerik
    You've missed the Icenium Visual Studio Integration keynote? It has been recorded and posted here.
    Looking for tips & tricks directly from the Icenium team? Check out our blog!
    Share feedback and vote for features on our Feedback Portal.
  5. Alfredo
    Alfredo avatar
    6 posts
    Member since:
    Nov 2013

    Posted 29 Nov 2013 Link to this post

    Steve,

    No, my device is a Motorola Defy 526 running 2.3.5 . After several hours of head-banging and hair-pulling this is the code that actually worked:

    function playAudio(src) {

        // HTML5 Audio
        if (typeof Audio != "undefined") { 
            new Audio(src).play() ;

        // Phonegap media
        } else if (typeof device != "undefined") {

            // Android needs the search path explicitly specified
            if (device.platform == 'Android') {
                src = '/android_asset/www/' + src;
            }

            var mediaRes = new Media(src,
                function onSuccess() {
                    // release the media resource once finished playing
                    mediaRes.release();
                },
                function onError(e){
                    console.log("error playing sound: " + JSON.stringify(e));
                });
            mediaRes.play();

        } else {
            console.log("no sound API to play: " + src);
        }
     }

    That is, plain javascript is what's actually working (new Audio(src).play() ;) Works in the Mist simulator and works on the Android device. I'm about to test it in Android 4.1 and above 'cause some say this is not working on these devices.
  6. Jack
    Jack avatar
    1 posts
    Member since:
    Jul 2014

    Posted 18 Nov 2014 in reply to Alfredo Link to this post

    You are the best man, it works!
  7. George
    George avatar
    2 posts
    Member since:
    Apr 2015

    Posted 23 Apr 2015 Link to this post

    Hi, Alfredo

         You are a cordova pioneer! thank you for help me to use the cordova audio plugin.

    But giving you an easier alternating way to play audio: that is using HTML's audio playing method.

    And could you tell me whether your this method is really sometimes using cordova audio plugin?

     

         Because my last time debug shows that the in the  "var mediaRes = new Media(src,  ..." , Media is

    unrecognized, I wonder whether the Media function is claimed in cordova.js or plugin.js? 

    Would you like to tell me what is the Javasript's method to claim using an outside var? is it just include

    by <script src=> or other method like import in Java?

     

          

  8. George
    George avatar
    2 posts
    Member since:
    Apr 2015

    Posted 23 Apr 2015 in reply to George Link to this post

    by the way, what is the best mobile-debug tool for cordova and phonegap?

    can the "phonegap developer app" be debuggable? can it just use "console.log" method?

     

  9. Zdravko
    Admin
    Zdravko avatar
    657 posts

    Posted 27 Apr 2015 Link to this post

    Hello George,

    The Media API is provided by a Cordova plugin which we have integrated in AppBuilder clients as a Core plugin and you can enable it from Properties > Plugins page. This is all you have to do. No references are required in order to use it further. Just note that when using any plugin's API you will have to call it in deviceready callback function.

    As for your subsequent question, AppBuilder provides two options that let you debug your app - in our Simulator and on device.

    Regards,
    Zdravko
    Telerik
     

    Visit the Telerik Verified Plugins Marketplace and get the custom Cordova plugin you need, already tweaked to work seamlessly with AppBuilder.

     
Back to Top