【问题标题】:Check if Audio is playing in the App检查音频是否在 App 中播放
【发布时间】:2014-11-03 16:20:22
【问题描述】:
我已经为此工作了 3 个小时,但我找不到解决方案。
我正在使用第 3 方库,它为我播放声音,我猜他们正在使用 AVAudioPlayer playSound,我想知道是否有办法知道我的应用是否正在播放声音。
我无权访问第 3 方库,并且播放该声音的属性是私有的。
我一直在尝试 AVAudioSession,但是只有两种不同的方法可以检查是否有任何声音在播放,不幸的是它只能检查来自应用程序外部的声音。
谢谢
【问题讨论】:
标签:
ios
objective-c
iphone
avaudioplayer
avaudiosession
【解决方案1】:
//Register self for remote notifications of play/pause toggle
//(i.e. when user backgrounds app, double-taps home,
//then swipes right to reveal multimedia control buttons).
//See MyWindow.h for more info.
Add this Notification Center where to detect playback
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(toggling_Playback) name:@"TogglePlayPause" object:nil];
- (void)toggling_Playback {
(self.player.isPlaying ? [self.player pause] : [self.player play]);
}
AVAudioPlayerDelegate protocol methods:
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)p successfully:(BOOL)flag {
[self.player stop];
[self ffButtonWasPressed];
}
- (void)audioPlayerBeginInterruption:(AVAudioPlayer *)player {
//In real app, may persist this to NSUserDefaults to act on after interruption ends
//or app resumes.
NSLog(@"Interruption began");
}
//Note: this form is only for iOS >= 4.0. See docs for pre-4.0.
- (void)audioPlayerEndInterruption:(AVAudioPlayer *)player withFlags:(NSUInteger)flags {
NSLog(@"Interruption ended");
//restart playback if flag indicates we should resume
if (flags & AVAudioSessionInterruptionFlags_ShouldResume) {
[self toggling_Playback];
}
}
如果您仍有任何疑问:请参考此链接
http://www.allappsdevelopers.com/TopicDetail.aspx?TopicID=99cb8bf0-91ac-4c41-a903-2dc744444b7a