检查应用程序中是否正在播放音频

时间:2014-11-03 16:20:23

标签: ios objective-c iphone avaudioplayer avaudiosession

我已经在那里工作了3个小时,我无法找到解决方案。 我正在使用第三方库,它为我播放声音,我猜他们正在使用AVAudioPlayer playSound,我想知道,如果有办法知道我的应用程序是否正在播放声音。

我无法访问第三方库,播放该声音的属性是私有的。 我一直在尝试AVAudioSession,但只有两种不同的方法可以检查是否有声音播放,不幸的是它只能检查来自应用程序外部的声音。

由于

1 个答案:

答案 0 :(得分:0)

//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