我是phoneGap的新手,目前我有媒体库播放音频的问题
我已按照步骤启用媒体插件但不起作用。
cordova插件rm cordova-plugin-media cordova插件rm cordova-plugin-media-capture
cordova插件rm cordova-plugin-extension cordova插件rm cordova-plugin-file
phonegap平台添加android
cordova平台更新android@5.0
cordova插件添加cordova-plugin-media cordova插件添加cordova-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="dev@cordova.apache.org" 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="dev@cordova.apache.org" 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 :(得分:1)
用以下代码替换playAudio函数。
function 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();
}