apache / cordova-plugin-media

Apache Cordova Media Plugin
https://cordova.apache.org/
Apache License 2.0
390 stars 763 forks source link

Android 12 (api 31-32) - audio not playing error 1 #358

Closed tempo-riz closed 9 months ago

tempo-riz commented 1 year ago

Bug Report

audio record works as expected but when I try to play() the recorded audio it gives me error 1 with no other callback triggering

//works as expected
let m1;
function rec() {
    m1 = new Media(cordova.file.dataDirectory + Date.now() + ".m4a",
        () => {
            console.log('audio saved in', m1.src)
        },
        console.error,
        console.log); //status

    m1.startRecord();
    setTimeout(() => {
        m1.stopRecord()
    }, 5000);
}
//crash
let m2;
function play(src) {
    m2 = new Media(src,
        () => {
            console.log('audio play succes', m2.src)
        },
        console.error, //<- gives error code 1
        console.log); //status

    m2.play();
}

Information

cordova 11.0.0 cordova android version 11.0.0 media plugin 6.0.0 file plugin 7.0.0

file is saved in file:///data/user/0/appid/files/1662290364345.m4a I also tried playing it from localhost: https://localhost/__cdvfile_files__/1662290364345.m4a

both give error 1 but from localhost there is a delay and the status callback gives 1 before crashing with err 1

I think it was working fine before I updated the plugin...

tempo-riz commented 1 year ago

okay so after more testing I found that calling release() after stopRecord() was working... Is this normal behavior ?

ttimot24 commented 1 year ago

I can not play radio stream with this plugin, do you have any idea?

ghenry22 commented 1 year ago

@tempo-riz were you able to play .m4a files? I am seeing error 1 (media error abort) with an no supported file type error in the console whenever I try to play an M4a from a stream or the local file system.

tempo-riz commented 1 year ago

@ghenry22 Hey I finally made it work for m4a files (default for ios and android if I remember), this is the code I used hope it helps

let mediaPlayer = new Media(src,
        // success callback
        () => {
            console.log('succes callback')
        },
        // error callback
        (err) => {
            console.log(err)
        },
        (status_codes) => {
            console.log(status_codes)
            if (status_codes == Media.MEDIA_STARTING || status_codes == Media.MEDIA_RUNNING) {
                getMediaPlayerDuration().then(dur => {
                    duration = dur;
                })
            }
        }
    );

 //play trigger Media.MEDIA_STARTING and seekto trigger Media.MEDIA_RUNNING (android)
 //only play trigger Media.MEDIA_RUNNING (ios)
 setTimeout(()=>mediaPlayer.play(), 500); //timeout to avoid crash ios

I think my issue was that I tried to call play() before the media was fully loaded so I added timout and checks. Also another intresting point, for IOS it was working only with relative path for me, so this is how I saved my audio path :

if (app.device.ios) {
            path = "../Library/NoCloud/" + Date.now() + ".m4a";
        } else {
            path = cordova.file.dataDirectory" + Date.now() + ".m4a";
}
ArielAleksandrus commented 1 year ago

Any updates on this? I'm getting the same error on Android!

acosme commented 10 months ago

okay so after more testing I found that calling release() after stopRecord() was working... Is this normal behavior ?

same here after call .stop() from just playing an audio...

have to execute release() after the 'stop'.

pixellet14 commented 9 months ago

is anyone here also facing a problem of accessing storage? the app is crashing everytime a media file is being accessed

breautek commented 9 months ago

okay so after more testing I found that calling release() after stopRecord() was working... Is this normal behavior ?

Based on the docs I'd say so.

Releases the underlying operating system's audio resources. This is particularly important for Android, since there are a finite amount of OpenCore instances for media playback. Applications should call the release function for any Media resource that is no longer needed.

It sounds like not calling release may cause blocking on available OpenCore resources. I'd imagine the limit on these OpenCore instances may vary from device, but by not calling release, you'll eventually hit the instance limit.

Closing because I believe this isn't actually a bug.