eshaz / icecast-metadata-js

Browser and NodeJS packages for playing and reading Icecast compatible streaming audio with realtime metadata updates.
164 stars 20 forks source link

NotAllowedError on iOS #169

Closed EvgeniyDoctor closed 1 year ago

EvgeniyDoctor commented 1 year ago

I cannot get this player to work on iOS (15.7.1). I have tried Safari, Chrome and Brave - all the same.

If i press Play then Stop and then press Play again - there is no audio; if i try to play another station - the same result. Sometimes i see this error in onError():

NotAllowedError: The request is not allowed by the user agent or the platform in the current context, possibly because the user denied permission.

Here is my code for Play/Stop button:

$('.playerPlay').on('click', function(){
    if (player != undefined) {
        switch (player.state) { // "loading", "playing", "stopping", "stopped", "retrying"
            case 'playing':
                playerStop()
                break
            case 'stopped':
                playerPlay()
                break
        }
    }
})
//----

function playerStop(){
    if (player != undefined) {
        player.stop()
        // player.detachAudioElement() // did not help
    }
}
//----
function playerPlay(){
    player.play()
}
//----

Example of an instance:

const player1 = new IcecastMetadataPlayer(`${RADIO}/music1.ogg`, {
    onMetadata: metadataUpdate,
    metadataTypes: ["ogg"]
})
...
player = player1

So, is there a way to make it work?

eshaz commented 1 year ago

Can you try out the demo page and see if that works for you? I haven't seen this error before while testing myself. You might also try searching StackOverflow or similar for this error, it sounds like the user denied audio playback.

EvgeniyDoctor commented 1 year ago

Demo: most stations works well, some work partially and some of them do not work at all. For example:

I tested it on iOS 15.7.1 in the last version of Brave Browser in incognito mode.

eshaz commented 1 year ago

It looks like you're using JQuery (based on the $ variable). This might be preventing iOS from detecting that the click event is coming from a user interaction. iOS and other browsers won't allow the AudioContext to start if it's not triggered directly by a user interaction.

You might try removing JQuery and just attaching the event directly to the element that is receiving the click.

Based on the results of the demo, it looks like the issue isn't with your version of iOS or your browser environment.

EvgeniyDoctor commented 1 year ago

Use JS only: did not help.