检查应用程序中是否正在播放音频 [英] Check if Audio is playing in the App

查看:144
本文介绍了检查应用程序中是否正在播放音频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为此工作了3个小时,但我找不到解决方案. 我正在使用一个3rd Party库,该库可以为我播放声音,我想他们正在使用AVAudioPlayer playSound,并且我想知道是否有办法知道我的应用程序是否在播放声音.

I've been working on that for 3 hours already, and i can't find a solution to it. I am using a 3rd Party library, that plays a sound for me, I am guessing they are using AVAudioPlayer playSound, and i want to know, if there is a way to know if my app is playing a sound.

我无权访问3rd Party库,并且播放该声音的属性是私有的. 我一直在尝试AVAudioSession,但是只有两种不同的方法来检查是否正在播放声音,不幸的是,它只能检查来自应用程序外部的声音.

I don't have access to the 3rd Party library, and the property to play that sound is private. I've been trying AVAudioSession, but there's only two different ways to check if there is any sound playing, and unfortunately it can only check for sound coming from outside the app.

谢谢

推荐答案

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

If you still have any doubt: kindly refer this link http://www.allappsdevelopers.com/TopicDetail.aspx?TopicID=99cb8bf0-91ac-4c41-a903-2dc744444b7a

这篇关于检查应用程序中是否正在播放音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆