Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Media Plugin Error Code: 1

I haven't been able to get a local sound playing using the Cordova Media plugin.

I have the right options in my platforms\android\res\xml\config.xml here...

<content src="index.html" />
<access origin="*" />
<preference name="loglevel" value="DEBUG" />
<feature name="App">
    <param name="android-package" value="org.apache.cordova.App" />
</feature>
<feature name="Device">
    <param name="android-package" value="org.apache.cordova.device.Device" />
</feature>
<feature name="File">
    <param name="android-package" value="org.apache.cordova.file.FileUtils" />
</feature>
<feature name="Media">
    <param name="android-package" value="org.apache.cordova.media.AudioHandler" />
</feature>
<feature name="SplashScreen">
    <param name="android-package" value="org.apache.cordova.splashscreen.SplashScreen" />
</feature>

...and the permission option in my AndroidManifest.xml...

<uses-sdk android:minSdkVersion="10" android:targetSdkVersion="19" />
<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" />

However when I try to play a sound I get nothing:

enter image description here

Note: I'm using Apache Cordova 3.3.0

like image 790
Joshua Barnett Avatar asked Jan 20 '14 21:01

Joshua Barnett


2 Answers

After doing a bit of digging as to how the Media plugin goes about playing audio naively via Android's MediaPlayer, I see that for some unknown reason the following permissions are required in the \platforms\androidAndroidManifest.xml.

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />

Internet Permission - If you are using MediaPlayer to stream network-based content, your application must request network access.

I can only guess that because you're trying to stream local media via a WebView it requires this permission to function correctly.

However it would have been nice to get a better error message than undefined maybe something like.

code: 1
message: Missing Android permission

Apache Cordova should have added this permission automatically when installing the plugin.

EDIT: Actually this is only a partial fix for accessing remote audio files I still have yet to get local files working.

like image 113
Joshua Barnett Avatar answered Nov 11 '22 11:11

Joshua Barnett


Here is my code and it works. make sure to install the device plugin

if(selectedSoundOption === "Coins"){
        var coin180Url = getMediaURL("sounds/coin180.mp3");
        var coin180 = new Media(coin180Url, null, mediaError);
        var coin360Url = getMediaURL("sounds/coin360.wav");
        var coin360 = new Media(coin360Url, null, mediaError);
        if(rotation.indexOf("360") > -1){
            coin180.play();
        }else{
            coin360.play();
        }
    }
}

function mediaError(e) {
    alert('Media error '+e);
}

function getMediaURL(s) {
    if(device.platform.toLowerCase() === "android") return "/android_asset/www/" + s;
    return s;
}
like image 43
owen gerig Avatar answered Nov 11 '22 11:11

owen gerig