2014-01-13 93 views
0

我工作的應用程序與phonegap構建和phonegap 3構建。我想讓用戶播放一些外部音頻文件與應用程序。但我應該錯過一些東西,因爲媒體無法找到併發揮。Phonegap與phonegap 3和媒體插件構建

這裏是我的config.xml:

<?xml version="1.0" encoding="UTF-8" ?> 
    <widget xmlns = "http://www.w3.org/ns/widgets" 
     xmlns:gap = "http://phonegap.com/ns/1.0" 
     id   = "com.phonegap.example" 
     versionCode = "10" 
     version  = "1.0.0" > 

    <preference name="phonegap-version" value="3.0.0" /> 
    <!-- versionCode is optional and Android only --> 

    <name>My App</name> 

    <description> 
     Application mobile 
    </description> 

    <author href="https://www.website.fr" email="[email protected]"> 
     Henri Labarre 
    </author> 

    <gap:platform name="ios" /> 
    <gap:platform name="android" /> 

    <gap:plugin name="org.apache.cordova.core.media" /> 
    <access origin="http://ibeat.org" subdomains="true" /> 

</widget> 

我的index.html:

<!DOCTYPE html> 
<html> 
    <head> 
    <title>My Audio</title> 
    <meta name="viewport" content="width=device-width, 
    height=device-height initial-scale=1.0, 
    maximum-scale=1.0, user-scalable=no;" /> 
    <meta http-equiv="Content-type" content="text/html; charset=utf-8"> 
    <link rel="stylesheet" href="jquery.mobile1.0b3.min.css" /> 
    <script type="text/javascript" charset="utf-8" src="jquery1.6.4.min.js"></script> 
    <script type="text/javascript" charset="utf-8" src="jquery.mobile1.0b3.min.js"></script> 
    <script type="text/javascript" charset="utf-8" src="main.js"></script> 
    <script type="text/javascript"> 
     function onBodyLoad() { 
     //During testing, Let me know we got this far 
     alert("onBodyLoad"); 
     //Add the PhoneGap deviceready event listener 
     document.addEventListener("deviceready", onDeviceReady(), false); 
     } 

     function onDeviceReady() { 
     //During testing, Let me know PhoneGap actually 
     // initialized 
     //Get our media file and stuff 
     init(); 
     } 
    </script> 
    </head> 
    <body onload="onBodyLoad()"> 
    <section id="main" data-role="page" > 
     <header data-role="header"> 
     <h1>My audio</h1> 
     </header> 
     <div data-role="content"> 
     <p id="track"></p> 
     <p id="pos"></p> 
     <div data-role="controlgroup"> 
      <a onclick="doPlay();" id="btnPlay" data-role="button" data-icon="arrow-r">Play</a> 
      <a onclick="doPause();" id="btnPause" data-role="button" data-icon="grid">Pause</a> 
      <a onclick="doStop();" id="btnStop" data-role="button" data-icon="delete">Stop</a> 
     </div> 
     </div> 
    </section> 
    </body> 
</html> 

而且我的腳本main.js

var fileDur, theMedia, theTimer; 

function init() { 
    //alert("Init"); 
    var fileName = "http://audio.ibeat.org/content/p1rj1s/p1rj1s_-_rockGuitar.mp3"; 
    console.log(fileName); 
    //Create the media object we need to do everything we need here 
    theMedia = new Media(fileName, onMediaSuccess, onMediaError, onMediaStatus); 
    console.log("Got this far!"); 
    console.log(theMedia); 
    //Update the UI with the track name 
    $('#track').html("<b>File:</b> " + fileName); 
    $('#pos').html('Duration: ' + Math.round(theMedia.getDuration()) + ' seconds'); 
} 

function onMediaSuccess() { 
    console.log("onMediaSuccess"); 
    window.clearInterval(theTimer); 
    theTimer = null; 
} 

function onMediaError(e) { 
    var msgText = "Media error: " + e.message + "(" + e.code + ")"; 
    console.log(msgText); 
    navigator.notification.alert(msgText, null, "Media Error"); 
} 

function onMediaStatus(statusCode) { 
    console.log("Status: " + statusCode); 
} 

function doPlay() { 
    if(theMedia) { 
    console.log("doPlay"); 
    //Start the media file playing 
    theMedia.play(); 
    //fire off a timer to update the UI every second as it plays 
    theTimer = setInterval(updateUI, 1000); 
    } else { 
    alert("No media file to play"); 
    } 
} 

function doPause() { 
    if(theMedia) { 
    console.log("doPause"); 
    //Pause media play 
    theMedia.pause(); 
    window.clearInterval(theTimer); 
    } 
} 

function doStop() { 
    if(theMedia) { 
    console.log("doStop"); 
    //Kill the timer we have running 
    theTimer = null; 
    //Then stop playing the audio clip 
    theMedia.stop(); 
    } 
} 

function updateUI() { 
    console.log("updateUI"); 
    theMedia.getCurrentPosition(onGetPosition, onMediaError); 
} 

function onGetPosition(filePos) { 
    console.log("onGetPosition"); 
    //We won't have any information about the file until it's 
    // actually played. Update the counter on the page 
    $('#pos').html('Time: ' + Math.floor(filePos) + ' of ' + theMedia.getDuration() + ' seconds'); 
} 

非常感謝你的幫助!

編輯:添加(),以監聽 EDIT2:添加訪問起源=

+0

也許,如果你精確在哪個平臺/設備測試你的,如果你得到的console.log消息的任何錯誤的日誌,它會幫助 – QuickFix

+0

在android設備上測試,通過phonegap構建的應用程序。看起來init函數沒有加載。 –

+0

並且您是否嘗試將設備連接到計算機並使用adb監視日誌? – QuickFix

回答

0

最後我用這個代碼,並將其與PhoneGap的構建工作3

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" 
        "http://www.w3.org/TR/html4/strict.dtd"> 
    <html> 
     <head> 
     <title>Media Example</title> 

     <link rel="stylesheet" href="jquery.mobile1.0b3.min.css" /> 
     <script type="text/javascript" charset="utf-8" src="jquery1.6.4.min.js"></script> 
     <script type="text/javascript" charset="utf-8" src="jquery.mobile1.0b3.min.js"></script> 
     <script type="text/javascript" charset="utf-8" src="cordova.js"></script> 
     <script type="text/javascript" charset="utf-8"> 

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

     // device APIs are available 
     // 
     //function onDeviceReady() { 
     // playAudio("http://www.mywebsite.fr/addons/Rome.m4a"); 
     //} 

     // Audio player 
     // 
     var my_media = null; 
     var mediaTimer = null; 

     // Play audio 
     // 
     function playAudio(src) { 
      // Create Media object from src 
      my_media = new Media(src, onSuccess, onError); 

      // Play audio 
      my_media.play(); 

      // Update my_media position every second 
      if (mediaTimer == null) { 
       mediaTimer = setInterval(function() { 
        // get my_media position 
        my_media.getCurrentPosition(
         // success callback 
         function(position) { 
          if (position > -1) { 
           setAudioPosition((position) + " sec"); 
          } 
         }, 
         // error callback 
         function(e) { 
          console.log("Error getting pos=" + e); 
          setAudioPosition("Error: " + e); 
         } 
        ); 
       }, 1000); 
      } 
     } 

     // Pause audio 
     // 
     function pauseAudio() { 
      if (my_media) { 
       my_media.pause(); 
      } 
     } 

     // Stop audio 
     // 
     function stopAudio() { 
      if (my_media) { 
       my_media.stop(); 
      } 
      clearInterval(mediaTimer); 
      mediaTimer = null; 
     } 

     // onSuccess Callback 
     // 
     function onSuccess() { 
      console.log("playAudio():Audio Success"); 
     } 

     // onError Callback 
     // 
     function onError(error) { 
      alert('code: ' + error.code + '\n' + 
        'message: ' + error.message + '\n'); 
     } 

     // Set audio position 
     // 
     function setAudioPosition(position) { 
      document.getElementById('audio_position').innerHTML = position; 
     } 
     </script> 
     </head> 
     <body> 
     <section id="main" data-role="page" > 
      <header data-role="header"> 
      <h1>mywebsite</h1> 
      </header> 
      <div data-role="content"> 
      <p id="track"></p> 
      <div data-role="controlgroup"> 
       <a onclick="playAudio('http://www.mywebsite.fr/addons/Rome.m4a');" id="btnPlay" data-role="button" data-icon="arrow-r">Lecture</a> 
       <a onclick="pauseAudio();" id="btnPause" data-role="button" data-icon="grid">Pause</a> 
       <a onclick="stopAudio();" id="btnStop" data-role="button" data-icon="delete">Stop</a> 
      </div> 
      </div> 

      <p id="audio_position"></p> 
     </section> 
     </body> 
    </html> 
0

我想你應該走線document.addEventListener("deviceready", onDeviceReady, false);出你onbodyload功能,而是在主初始化函數的定義之後添加document.addEventListener("deviceready", init, false); .js文件。

由於您在onbodyload中有警報,因此在您開始偵聽之前觸發deviceready事件。

+0

我做你的建議,但仍然沒有媒體。我不知道聽者設備是否已經工作 –

+0

Humm使用()更好,addEventListener需要這樣一個函數:document.addEventListener(「deviceready」,onDeviceReady(),false); –

+0

是的,我可以向你保證,deviceready工作,但我的壞,我告訴你把行放在main.js的開頭,實際上它應該放在init函數定義後。 – QuickFix