2016-02-05 40 views
0

我在PhoneGap的是新的目前我有問題通過媒體庫中播放音頻的PhoneGap媒體對象不definied錯誤

我必須遵循的步驟,以使媒體插件,但它並沒有奏效。

科爾多瓦插件RM科爾多瓦 - 插件 - 媒體 科爾多瓦插件RM科爾多瓦 - 插件媒體捕捉

科爾多瓦插件RM科爾多瓦 - 插件擴展 科爾多瓦插件RM科爾多瓦 - 插件文件

的PhoneGap平臺加上Android的

科爾多瓦平臺更新[email protected]

科爾多瓦插件添加科爾多瓦 - 插件 - 媒體 科爾多瓦插件添加合作rdova-plugin-media-capture

這裏是我的代碼audio.js。

// Audio player 
// 
var my_media = null; 

// Play audio 
// 
function playAudio(src,callbackFinishCallBack) { 
    // Create Media object from src 
    if (typeof Media !== 'undefined') { 
     // the variable is defined 
     alert("Error No Media Object defined..!"); 
     return; 
    } 
    var my_media = null; 
    my_media = new Media(src, // success callback 
      function() { 
       my_media.release(); 
       if(typeof callbackFinishCallBack == 'function') 
       { 
        callbackFinishCallBack.call(this); 
       } 
      }, 
      // error callback 
      function (err) { 
       alert('code: ' + err.code + '\n' + 
        'message: ' + err.message + '\n'); 
       my_media.release(); 
       if(typeof callbackError == 'function') 
       { 
        callbackError.call(this); 
       } 
      }); 

    // Play audio 
    my_media.play(); 


} 

/*function playAudio(src,callbackFinishCallBack) { 
// HTML5 Audio 
    if (typeof Audio != "undefined") { 
     var audioRes = new Audio(src).play() ; 
     alert("Called audio"); 
     audioRes.addEventListener('onended', function(){ 
      alert(); 
      callbackFinishCallBack.call(this); 
     }); 

    // Phonegap media 
    } else if (typeof device != "undefined") { 
     alert("Called Media"); 

     if (typeof Media !== 'undefined') { 
      // the variable is defined 
      alert("Error No Media Object Defnied"); 
      return; 
     } 

     // Android needs the search path explicitly specified 
     if (device.platform == 'Android') { 
      src = '/android_asset/www/' + src; 
     } 

     var mediaRes = new Media(src, 
      function onSuccess() { 
       my_media.release(); 
       if(typeof callbackFinishCallBack == 'function') 
       { 
        callbackFinishCallBack.call(this); 
       } 
      }, 
      function onError(e){ 
       console.log("error playing sound: " + JSON.stringify(e)); 
      }); 
     mediaRes.play(); 

    } else { 
     console.log("no sound API to play: " + src); 
     alert(src); 
    } 
}*/ 

只要我以前在設備上的準備代碼在這裏.. 的index.html

function onDeviceReady() 
    { 
$.ajax({ 
      type: "POST", 
      url: "http://erronak.com/dev/child_app/index.php", 
      dataType:'json' 
     }).done(function(row) { 

      $("#main_image").attr("src",row.image); 
      $("#popup_name").html(row.name); 
      $("#audio_hidden").val(row.audio); 


      //Start replacing operation.. 
      //stopAudio(); 
      //alert(row.audio); 
      playAudio(row.audio,function(){ 
      //Let Display text over here.. 
      $("#popup_name").popup("open"); 

      }); 

這裏是我的主配置文件config.xml中

<?xml version='1.0' encoding='utf-8'?> 
<widget id="io.cordova.hellocordova" version="0.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0"> 
    <name>HelloCordova</name> 
    <description> 
     A sample Apache Cordova application that responds to the deviceready event. 
    </description> 
    <author email="[email protected]" href="http://cordova.io"> 
     Apache Cordova Team 
    </author> 
    <content src="index.html" /> 
    <preference name="fullscreen" value="true" /> 
    <preference name="SplashScreen" value="splash" /> 
    <preference name="SplashScreenDelay" value="3000" /> 
    <preference name="SplashMaintainAspectRatio" value="true" /> 
    <preference name="SplashShowOnlyFirstTime" value="true" /> 
    <access origin="*" /> 
    <allow-intent href="http://*/*" /> 
    <allow-intent href="https://*/*" /> 
    <allow-intent href="tel:*" /> 
    <allow-intent href="sms:*" /> 
    <allow-intent href="mailto:*" /> 
    <allow-intent href="geo:*" /> 
    <platform name="android"> 
     <allow-intent href="market:*" /> 
    </platform> 
    <platform name="ios"> 
     <allow-intent href="itms:*" /> 
     <allow-intent href="itms-apps:*" /> 
    </platform> 
    <engine name="android" spec="~4.1.1" /> 
    <plugin name="cordova-plugin-splashscreen" spec="~3.1.0" /> 
    <plugin name="cordova-plugin-dialogs" spec="~1.2.0" /> 
    <plugin name="cordova-plugin-whitelist" spec="1" /> 
    <plugin name="cordova-admob" spec="~4.1.6" /> 
    <plugin name="cordova-libgoogleadmobads" spec="C:\Users\ADMIN\Downloads\google-iosadmobads-master" /> 

</widget> 

這裏是我的android /res/xml/config.xml

<?xml version='1.0' encoding='utf-8'?> 
<widget id="io.cordova.hellocordova" version="0.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0"> 
    <preference name="loglevel" value="DEBUG" /> 
    <feature name="connectivity"> 
     <param name="android-package" value="com.appfeel.cordova.connectivity.Connectivity" /> 
    </feature> 
    <feature name="AdMobAds"> 
     <param name="android-package" value="com.appfeel.cordova.admob.AdMobAds" /> 
    </feature> 
    <feature name="Notification"> 
     <param name="android-package" value="org.apache.cordova.dialogs.Notification" /> 
    </feature> 
    <feature name="File"> 
     <param name="android-package" value="org.apache.cordova.file.FileUtils" /> 
     <param name="onload" value="true" /> 
    </feature> 
    <feature name="SplashScreen"> 
     <param name="android-package" value="org.apache.cordova.splashscreen.SplashScreen" /> 
     <param name="onload" value="true" /> 
    </feature> 
    <feature name="Whitelist"> 
     <param name="android-package" value="org.apache.cordova.whitelist.WhitelistPlugin" /> 
     <param name="onload" value="true" /> 
    </feature> 
    <feature name="Media"> 
     <param name="android-package" value="org.apache.cordova.media.AudioHandler" /> 
    </feature> 
    <feature name="Capture"> 
     <param name="android-package" value="org.apache.cordova.mediacapture.Capture" /> 
    </feature> 
    <allow-intent href="market:*" /> 
    <name>HelloCordova</name> 
    <description> 
     A sample Apache Cordova application that responds to the deviceready event. 
    </description> 
    <author email="[email protected]" href="http://cordova.io"> 
     Apache Cordova Team 
    </author> 
    <content src="index.html" /> 
    <preference name="fullscreen" value="true" /> 
    <preference name="SplashScreen" value="splash" /> 
    <preference name="SplashScreenDelay" value="3000" /> 
    <preference name="SplashMaintainAspectRatio" value="true" /> 
    <preference name="SplashShowOnlyFirstTime" value="true" /> 
    <access origin="*" /> 
    <allow-intent href="http://*/*" /> 
    <allow-intent href="https://*/*" /> 
    <allow-intent href="tel:*" /> 
    <allow-intent href="sms:*" /> 
    <allow-intent href="mailto:*" /> 
    <allow-intent href="geo:*" /> 
</widget> 

androidMenifest.xml

<?xml version='1.0' encoding='utf-8'?> 
<manifest android:hardwareAccelerated="true" android:versionCode="1" android:versionName="0.0.1" package="io.cordova.hellocordova" xmlns:android="http://schemas.android.com/apk/res/android"> 
    <supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:resizeable="true" android:smallScreens="true" android:xlargeScreens="true" /> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 
    <application android:hardwareAccelerated="true" android:icon="@drawable/icon" android:label="@string/app_name" android:supportsRtl="true"> 
     <activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="@string/activity_name" android:launchMode="singleTop" android:name="MainActivity" android:theme="@android:style/Theme.Black.NoTitleBar" android:windowSoftInputMode="adjustResize"> 
      <intent-filter android:label="@string/launcher_name"> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> 
     <activity android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" android:name="com.google.android.gms.ads.AdActivity" android:theme="@android:style/Theme.Translucent" /> 
    </application> 
    <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="22" /> 
    <uses-permission android:name="android.permission.RECORD_AUDIO" /> 
    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" /> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    <uses-permission android:name="android.permission.READ_PHONE_STATE" /> 
    <uses-permission android:name="android.permission.RECORD_VIDEO" /> 
</manifest> 

當上面的代碼運行它給我 錯誤沒有媒體對象定義..!

我已經嘗試了這麼多,我非常接近的解決方案,請大家幫我出來..

+0

我曾嘗試了這麼多的可能性,但我覺得我幾乎近於解決方案.. – Roni

回答

1

更換playAudio功能與下面的代碼。

功能playAudio(SRC,callbackFinishCallBack){

var my_media = null; 
my_media = new Media(src, // success callback 
     function() { 
      my_media.release(); 
      if(typeof callbackFinishCallBack == 'function') 
      { 
       callbackFinishCallBack.call(this); 
      } 
     }, 
     // error callback 
     function (err) { 
      alert('code: ' + err.code + '\n' + 
       'message: ' + err.message + '\n'); 
      my_media.release(); 
      if(typeof callbackError == 'function') 
      { 
       callbackError.call(this); 
      } 
     }); 

// Create Media object from src 
if (typeof my_media !== 'undefined') { 
    // the variable is defined 
    alert("Error No Media Object defined..!"); 
    return; 
} 

// Play audio 
my_media.play(); 

}

+0

沒有紈絝子弟,但它仍然同樣進入/ /該變量被定義並警告(「錯誤沒有媒體對象定義..! – Roni

+0

感謝您的努力.. – Roni