This is a migrated thread and some comments may be shown as answers.

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

8 Answers 486 Views
Google Android
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Alfredo
Top achievements
Rank 1
Alfredo asked on 25 Nov 2013, 02:12 PM
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.

8 Answers, 1 is accepted

Sort by
0
Steve
Telerik team
answered on 27 Nov 2013, 03:25 PM
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.
0
Alfredo
Top achievements
Rank 1
answered on 27 Nov 2013, 05:57 PM
Thank“s Steve for the reply but even correcting the URL no sound is played. Nothing. Muted. Can you give it a try? Please?
0
Steve
Telerik team
answered on 28 Nov 2013, 08:00 AM
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.
0
Alfredo
Top achievements
Rank 1
answered on 29 Nov 2013, 05:35 PM
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.
0
Jack
Top achievements
Rank 1
answered on 18 Nov 2014, 02:10 PM
You are the best man, it works!
0
George
Top achievements
Rank 1
answered on 23 Apr 2015, 01:30 PM

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?

 

      

0
George
Top achievements
Rank 1
answered on 23 Apr 2015, 01:35 PM

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?

 

0
Zdravko
Telerik team
answered on 27 Apr 2015, 11:37 AM
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.

 
Tags
Google Android
Asked by
Alfredo
Top achievements
Rank 1
Answers by
Steve
Telerik team
Alfredo
Top achievements
Rank 1
Jack
Top achievements
Rank 1
George
Top achievements
Rank 1
Zdravko
Telerik team
Share this question
or