AVPlayer, MPVolumeView and Volume HUD
-
07-07-2021 - |
Pregunta
I'm using an AVPlayer to play various medias. I redesigned the whole player interface and I added a MPVolumeView to control output level. It works just fine, either with the slider or with the volume buttons (yje output volume is changed), but the user gets no visual indication that the volume is changing. When using the Apple's built in Video App, when you change the output volume using buttons, there is a nice hud indicating the current volume : how can I make it appear? Thanks.
Solución
As a matter of fact, it seems that the VolumeView blocks the delivery of notifications to the system. I had to use AudioSessionAddPropertyListener to make it work like this
AudioSessionAddPropertyListener (kAudioSessionProperty_AudioRouteChange,
audioRouteChangeListenerCallback,
self);
and the callback
// detecting outplugg of the iPhone
void audioRouteChangeListenerCallback (void *inClientData,
AudioSessionPropertyID inID,
UInt32 inDataSize,
const void *inData)
{
CFDictionaryRef routeChangeDictionary = inData;
CFNumberRef routeChangeReasonRef =
CFDictionaryGetValue (routeChangeDictionary,
CFSTR (kAudioSession_AudioRouteChangeKey_Reason));
SInt32 routeChangeReason;
CFNumberGetValue (routeChangeReasonRef, kCFNumberSInt32Type, &routeChangeReason);
if (routeChangeReason == kAudioSessionRouteChangeReason_OldDeviceUnavailable)
{
// Headset is unplugged..
[(VideoPlayerViewController*)inClientData pause];
}
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow