1

Trying to play audio files from the external storage of phone.

Went through this plugin first finding the URI for a particular mp3 file. Then passed that to the cordova-plugin-media instance.

// find music file URI from the sdcard
new ExternalStorageSdcardAccess( fileHandler ).scanPath( "file:///storage/C67A-18F7/Music/" );
function fileHandler( fileEntry ) {
    // console.log( fileEntry.name + " | " + fileEntry.toURL() );
}    

// create a button instance
let mbutton = new Button({
  centerX: 0, top: [fbutton,100],
  text: 'play music'  
}).appendTo(ui.contentView);    

// call the play media function from the button
mbutton.on('select', () => {
    // load and play the music:
    playAudio("file:///storage/C67A-18F7/Music/demo/testSound.mp3")

});    

function playAudio(url) {
    // Play the audio file at url
    var my_media = new Media(url,
        // success callback
        function () {
            console.log("playAudio():Audio Success");
        },
        // error callback
        function (err) {
            console.log("playAudio():Audio Error: " + err.code + err.message);
        },
        // status callback
        function (status) {
            console.log("playAudio():Audio Status: " + status);
        }
    );
    // Play audio
    my_media.play();
    my_media.setVolume('1.0');
}

I checked the adb for log. This is what I got specifically hitting those buttons - http://paste.ubuntu.com/25767292/

incoming-operation: 'Call' on 'cordova.plugin' (o13) with method 'exec' with properties {action=create, arguments=["d39113d0-b5f5-bf2d-8a84-5afbbc6ae9a0","file:///storage/C67A-18F7/Music/demo/testSound.mp3"], callbackId=Media975330222}
incoming-operation: 'Call' on 'cordova.plugin' (o13) with method 'exec' with properties {action=startPlayingAudio, arguments=["d39113d0-b5f5-bf2d-8a84-5afbbc6ae9a0","file:///storage/C67A-18F7/Music/demo/testSound.mp3",null], callbackId=INVALID}
outgoing-operation: 'Notify' o13 of 'finish' with arguments {message=S01 Media975330222 s, status=1, keepCallback=false, callbackId=Media975330222}
ExtMediaPlayer-JNI: env->IsInstanceOf fails
MediaPlayer-JNI: JNIMediaPlayerFactory: bIsQCMediaPlayerPresent 0
ExtMediaPlayer-JNI: env->IsInstanceOf fails
MediaPlayer-JNI: JNIMediaPlayerFactory: bIsQCMediaPlayerPresent 0
arjun
  • 1,594
  • 16
  • 33

2 Answers2

2

I am using Capacitor with an ionic react project and was able to get cordova-plugin-media playing local files by using it in conjunction with cordova-plugin-file. I am the ionic-native wrappers, but I think its the same code here.

NOTE: iOS requires you to remove 'file://' and Android does not.

    const finishedFilePath = `${File.applicationDirectory}public/assets/audio/file.mp3`;
const hybridTransitionAudioFile = Media.create(isPlatform('ios') ? transitionFilePath.replace('file://', '') : transitionFilePath);
hybridTransitionAudioFile.play();
FinestStudios
  • 181
  • 1
  • 7
0

1- install this

https://play.google.com/store/apps/details?id=com.adobe.phonegap.app&hl=en

2- install this

http://docs.phonegap.com/getting-started/1-install-phonegap/desktop/

3- connect your laptop and your phone on same network

4- follow the getting started on this and run it on your phone , i suppose you wont get the same error , because im my case it dose not work in browser nor emulator in case of phone gap

Reference: https://phonegap.com/getstarted/

give feed back so i can update my answer here

HTML PART for audio src

        <audio id="successSound" src="/android_asset/www/audio/correct.mp3" type="audio/mpeg"></audio>
        <audio id="errorSound" src="/android_asset/www/audio/error.mp3" type="audio/mpeg" ></audio>
    </body>
</html>

Example of my version that works i hope there is help in it

playAudio("errorSound");


var my_media = null;
var mediaTimer = null;

function playAudio(id) {
    var audioElement = document.getElementById(id);
    var src = audioElement.getAttribute('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);
    }
}

function setAudioPosition(position) {
    document.getElementById('audio_position').innerHTML = position;
}


// onSuccess Callback
//
function onSuccess() {
}

// onError Callback
function onError(error) {
    switch(error.code){
        case MediaError.MEDIA_ERR_ABORTED:
        alert('MEDIA_ERR_ABORTED code: '    + error.code);
        break;
        case MediaError.MEDIA_ERR_NETWORK:
        alert('MEDIA_ERR_NETWORK code: '    + error.code);
        break;
        case MediaError.MEDIA_ERR_DECODE:
        alert('MEDIA_ERR_DECODE code: '    + error.code);
        break;
        case MediaError.MEDIA_ERR_NONE_SUPPORTED:
        alert('MEDIA_ERR_NONE_SUPPORTED code: '    + error.code);
        break;
    }
}
shareef
  • 9,255
  • 13
  • 58
  • 89
  • Do you want me to test this plugin on Phonegap? – arjun Oct 18 '17 at 19:12
  • Tabris JS. Only difference is there is no DOM. Rest everything is similar. – arjun Oct 19 '17 at 10:08
  • I think it may be because of the URL. Let me modify that, check and give you feedback. – arjun Oct 19 '17 at 10:12
  • @arjun check my edit above i added `v` – shareef Oct 19 '17 at 12:14
  • I tried your code. I am getting a `MediaError.MEDIA_ERR_ABORTED = 1` – arjun Oct 19 '17 at 14:11
  • I am giving the path for an mp3 file from the sdcard. Is it supported? – arjun Oct 19 '17 at 14:15
  • of course, but what is your permission , please update your question with more info regarding permissions you have and make sure to add `` https://stackoverflow.com/questions/2121833/permission-to-write-to-the-sd-card , and what do you mean ` I am getting a MediaError.MEDIA_ERR_ABORTED = 1` is it error ! not clear – shareef Oct 19 '17 at 16:47
  • I am using [this plugin](https://github.com/NeoLSN/cordova-plugin-android-permissions.git) to get permissions. Please check [this `adb` log](http://paste.ubuntu.com/25774011/) for the code output when I run your code. It is failing somewhere and moreover the `setAudioPosition` keeps on adding the text `-0.001sec`. You can also check `line 19` where by I had added the `status callback` of the media plugin. It gives a status of 1 - [Media.MEDIA_STARTING](https://github.com/apache/cordova-plugin-media#constants) – arjun Oct 19 '17 at 18:04
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/157093/discussion-between-shareef-and-arjun). – shareef Oct 19 '17 at 18:27