Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to play song in Android - phonegap [closed]

A month ago I started working with phonegap, html5, css3 and jQtouch. I am working on an application and I need to play sound in the application. I have a serious problem with this task. First I've found out that I can play .mp3 files through phonegap using the new Media(...) function. I am not sure about .wav files, can I play those? Second I've found that files must be less than 30 sec, is this so? Third I can't find the right place for my sound files. My project structure is the following:

project
-- src
-- gen
-- assets
-- -- www
-- -- DANCE.mp3
-- -- jqtouch
-- -- -- (some folders and files)
-- -- phonegap.js
-- libs
-- res

I've tried placing the file in 'www' folder and creating a new one called 'audio'. None of this gave me what I wanted. I am using this code for executing the song:

function playStream() {
    mp3file = new Media("DANCE.mp3",
            function() {
                alert("playAudio():Audio Success");
            },
                function(err) {
                    alert(err);
            }
            );
          mp3file.play();
}

I use Android 2.1 Simulator and I've tried 2.2 as well without success. I hope I was clear enough. I am looking forward to hearing from you. Yours, Mihail Velikov

like image 969
Mihail Velikov Avatar asked Nov 11 '10 20:11

Mihail Velikov


2 Answers

PhoneGap for Android lets you store local audio files in two places: android_asset: file name must start with /android_asset/sound.mp3 sdcard: file name is just sound.mp3

If you place your DANCE.mp3 file in your project's assets directory, then you can play it using the path:

mp3file = new Media("/android_asset/DANCE.mp3",
        function() {
            alert("playAudio():Audio Success");
        },
            function(err) {
                alert(err);
        }
        );
      mp3file.play();

(You can read the source for AudioPlayer.java in the Android PhoneGap sources to see how this works.)

like image 69
Jack Palevich Avatar answered Nov 10 '22 18:11

Jack Palevich


Can I offer an advice.. If you use the PhoneGap Media player.. You need to remove the file:// from the path.. It doesn not accept a full URI for local files Regards Kim

like image 31
Kim Ras Avatar answered Nov 10 '22 16:11

Kim Ras