user3405375
user3405375

Reputation: 11

Play audio in eclipse android doesn’t work

I want to play an local storage audio, but in the editor it is displayed as a variable, and I get the following error.

 Refranceerror:can’t find variable:Media
 Refranceerror: can’t find Media at file://android_assets/www/ Over_the_horizon.mp3

I put the empreitions in mainfest file and pkugin in config

  <!DOCTYPE html>                                        
   <html>
    <head>
    <script type="text/javascript" charset="utf-8" src="cordova-2.6.0.js"></script>
    <link rel="stylesheet" href="jqueryCSS.css" />
    <script src="jquery.js"></script>

    <script src="jqueryMobile.js"></script>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"     type="text/javascript"></script>
          <meta name="viewport" content="width=device-width, initial-scale=1"> 
        <script type="text/javascript" charset="utf-8">

                               // Wait for Cordova to load
                  //
               document.addEventListener("deviceready", onDeviceReady, false);


             var myMedia;
                // Cordova is ready
                 //
             function onDeviceReady() {
               // Throw an error if no update is received every 30 seconds
             }



             function voicea(a){
                 alert(a);
                 myMedia = new Media("/android_asset/www/Over_the_horizon.mp3", 
                 function(){
                    if (myMedia) {
                       myMedia.play();

                       //alert();
                    }
                  }, 
                  function(error){
                      console.log(error.message);
                      alert(error);
                  }
                  );
                  alert();
                  // myMedia.play();

               }
                </script>

              </head>
              <body>

                <a onclick="voicea(0);"><img src="1.jpg" id="img1"width="100%" hieght="80%"/></a>

             </body> 
            </html>

Upvotes: 1

Views: 234

Answers (2)

Gaganpreet Singh
Gaganpreet Singh

Reputation: 886

Try this simple code:

          private void sound(){
final MediaPlayer mediaplayer=MediaPlayer.create(MainActivity.this,R.raw.audio);  
        mediaplayer.start();
        mediaplayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            public void onCompletion(MediaPlayer mp) {
                mp.release();
            }
        });
    }

Upvotes: 0

Pihu
Pihu

Reputation: 1039

I have related code which plays audio and is working well.Hope this would help you. you can add your audio file in a folder and name it raw under the res folder.

Through coding : Add this code in your class where you want to play audio:

//*************** Will play sound *******************************
          private void play_sound(){
        //set media player
final MediaPlayer mediaplayer=MediaPlayer.create(MainActivity.this,R.raw.youraudio);  
        mediaplayer.start();
        mediaplayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            public void onCompletion(MediaPlayer mp) {
                mp.release();
            }
        });
    }

Upvotes: 1

Related Questions