Giter Club home page Giter Club logo

Comments (23)

frontyard avatar frontyard commented on September 15, 2024

@netmikey It sounds like something that wouldn't be in the scope of the player.

What you are looking for is use/create a plugin that monitors telephony events and notifies your app of any so you can pause/resume the playback. If you don't find anything suitable and you're willing to sponsor the development of that let me know.

from cordova-plugin-exoplayer.

netmikey avatar netmikey commented on September 15, 2024

Thanks for your feedback on this! 👍
After I wrote these lines, I got an even more general feedback: the player also doesn't stop playing when another app starts playing music. I'm not an Android guy, but I'm told that apps that play back sound usually stop when something else starts playing a sound too?
It seems to me like that would be the OS's job, wouldn't it? Or is there also a native API that notifies me when another app starts playing back audio?

from cordova-plugin-exoplayer.

frontyard avatar frontyard commented on September 15, 2024

I'm not sure, I would have to do a bit of research. I would expect that there is an API that let's you know if another app is using audio subsystem. Or maybe there is a setting we can use to trigger the behavior we want?

from cordova-plugin-exoplayer.

netmikey avatar netmikey commented on September 15, 2024

I just had a look around and found this SO question:
https://stackoverflow.com/questions/24474929/stop-mediaplayer-when-an-other-app-play-music

Which links to this piece of android documentation about handling audio focus: https://developer.android.com/guide/topics/media-apps/volume-and-earphones.html#audio-focus

I'd be interested in your thoughts: is this an exoplayer topic?

from cordova-plugin-exoplayer.

frontyard avatar frontyard commented on September 15, 2024

I think we can make the plugin ask for audio focus if it helps.

from cordova-plugin-exoplayer.

netmikey avatar netmikey commented on September 15, 2024

I think that would be great 👍

Let's go over what's in the docs:

  • Before starting to play, request audio focus and verify that it has been granted

that one shouldn't be too much of an issue - the only decision required: what should we do if we don't get focus granted?

  • When playback stops, abandon audio focus

this one's straight forward

  • When another app gains audio focus, stop playing or duck the volume down

I don't know about this one. Maybe add events that applications can subscribe to in order to decide what to do? Or add a config setting like: onAudioFocusLost: [IGNORE|STOP|DUCK_VOLUME]. But then again: what should the player do when it regains audio focus? Resume playback? Nothing?... Thinking about it, I kinda like the idea of handing that decision off to the application.

  • For apps that target Android 5.0 (API level 21) and later, use AudioAttributes to describe the type of audio your app is playing. Apps that play speech should specify CONTENT_TYPE_SPEECH.

That could definitely be a config setting, since the application should know what it's about to play. Don't you think?

from cordova-plugin-exoplayer.

netmikey avatar netmikey commented on September 15, 2024

Hi frontyard,

Thanks for releasing support for audio focus into 2.4.0!

I have a problem with the way it currently behaves though: our app monitors the player state via the onSuccess callback and its event.playbackState state to update its GUI elements. When the player loses audio focus, it stops playing as expected, but it looks like the onSuccess callback isn't fired with an event.playbackState of STATE_IDLE as I'd expected it.

So ultimately, when losing audio focus, exoplayer stops playing fine, but our app's GUI still shows a "playing" state.

Is there any other event fired on audio focus loss / regain that I can listen for and react on in the application?

from cordova-plugin-exoplayer.

frontyard avatar frontyard commented on September 15, 2024

@netmikey Check out version 2.4.1. There is an event now being fired on audio focus change.

from cordova-plugin-exoplayer.

netmikey avatar netmikey commented on September 15, 2024

Sounds great! I'll check this out when I come home 👍

Could you elaborate on what the different AUDIOFOCUS_LOSS* events stand for and how the plugin reacts to each one?

from cordova-plugin-exoplayer.

frontyard avatar frontyard commented on September 15, 2024

The events are comging from android AudioManager

On AUDIOFOCUS_LOSS_TRANSIENT and AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK we pause.
On AUDIOFOCUS_GAIN we play.
On AUDIOFOCUS_LOSS we close.

from cordova-plugin-exoplayer.

netmikey avatar netmikey commented on September 15, 2024

@frontyard are you sure you released 2.4.1? npm shows 2.4.0 as the latest version...

from cordova-plugin-exoplayer.

netmikey avatar netmikey commented on September 15, 2024

Okay, so with 2.4.1 it works much better now. When we get any of the AUDIOFOCUS_LOSS events, we explicitly close the player so that our app ends up in the right state. It looks like we never get the AUDIOFOCUS_GAIN event (maybe because we close the player on loss?), but requiring people to hit play manually isn't too much of an issue I think.

from cordova-plugin-exoplayer.

frontyard avatar frontyard commented on September 15, 2024

@netmikey Yes, you won't get events back once you close the player. Have you considered not closing the player on AUDIOFOCUS_LOSS_TRANSIENT* and have it just resume automatically?

from cordova-plugin-exoplayer.

netmikey avatar netmikey commented on September 15, 2024

We can't do that: if I got it right, you said the player pauses playback. We're streaming live content however. If we pause the live stream and resume it some time in the future, it won't be live anymore and the delay would make for an awkward experience. So we really need the player to stop and restart playback, not just pause.

from cordova-plugin-exoplayer.

frontyard avatar frontyard commented on September 15, 2024

That makes sense. I'm actually not comfortable with plugin performing actions based on audio focus events, so I'm thinking about removing the play/pause and just letting the app decide what to do on these events. Would that work better?

from cordova-plugin-exoplayer.

netmikey avatar netmikey commented on September 15, 2024

I agree that this would be a cleaner behavior. In this case, we would still need to get the focus gain events, even if we stop (close) the player though. Would that be possible?

We're about to release into production with the current version btw. So it would be awesome if the api could be kept backwards compatible ;-)

from cordova-plugin-exoplayer.

netmikey avatar netmikey commented on September 15, 2024

@frontyard We rolled out. Immediate feedback we get: it looks like Android OS withdraws Audio Focus even on notification sounds like "new message", so our player keeps stopping all the time when the sound stage is busy.

Which now puts pressure on me to fix the current behaviour. Have you been able to modify the player so that it lets the application chose what action to take on audio focus loss?

from cordova-plugin-exoplayer.

frontyard avatar frontyard commented on September 15, 2024

@netmikey version 2.4.2 no longer acts on audio focus events. Make sure you differentiate between AUDIOFOCUS_LOSS_TRANSIENT, AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK and AUDIOFOCUS_LOSS.

from cordova-plugin-exoplayer.

netmikey avatar netmikey commented on September 15, 2024

That sounds great! Does 2.4.2 deliver the AUDIOFOCUS_GAIN event even if I closed the player on a previously received AUDIOFOCUS_LOSS event?

from cordova-plugin-exoplayer.

frontyard avatar frontyard commented on September 15, 2024

No, close is suppose to be used to dispose of the player. Any event listeners are disposed of as well. When you initialize the player again you can check the status of audio focus.

from cordova-plugin-exoplayer.

netmikey avatar netmikey commented on September 15, 2024

Hmm... could you add a "real" stop function then? I'm trying to figure out how to stop and restart live streaming while getting all audio focus events...

from cordova-plugin-exoplayer.

frontyard avatar frontyard commented on September 15, 2024

@netmikey I've added .stop function but haven't had time to test it. Please let me know if this works for you. Once you stop you can call setStream(url) to restart the playback.

from cordova-plugin-exoplayer.

frontyard avatar frontyard commented on September 15, 2024

I'm assuming this is no longer an issue so closing this ticket.

from cordova-plugin-exoplayer.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.