在eclipse android中播放音频不起作用

时间:2014-03-11 10:53:02

标签: android eclipse core-audio

我想播放本地存储音频,但在编辑器中它显示为变量,我收到以下错误。

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

我将empreitions放在mainfest文件中,pkugin放在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>

2 个答案:

答案 0 :(得分:1)

我有相关的代码播放音频并且运行良好。希望这会对你有所帮助。 您可以将音频文件添加到文件夹中,并在res文件夹下将其命名为raw

通过编码: 在您要播放音频的班级中添加此代码:

//*************** 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();
            }
        });
    }

答案 1 :(得分:0)

试试这个简单的代码:

          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();
            }
        });
    }