How to detect the playing state of the audio in UI

2020-08-01 02:41发布

问题:

I have an UIWebView to load a html web link:http://m.y.qq.com in my iOS app. I don't know how to detect the playing state of the audio in UIWebView.

I just need to know whether the audio in UIWebView is playing or not in my iOS app, so that I can do something.

I have searched a lot from Stackoverflow and Google, but I can't find a satisfied answer. Can anyone give me some help?

回答1:

You can check if audio is playing with isOtherAudioPlaying

So in your UIWebView delegate methods you can use:

BOOL isOtherAudioPlaying = [[AVAudioSession sharedInstance] isOtherAudioPlaying];


回答2:

AVAudioSession has a sharedInstance property which you can utilize in this instance. The property is a BOOL and can be updated upon multiple threads.

@tnylee's code is perfect.