2014-02-25 91 views
4

我有一个ios应用程序,它在移动到后台时继续播放音乐。现在,如果有电话回答与否,应用程序不会继续播放音乐。 两天来,我一直在这里阅读有关这个​​问题的帖子。没有一个解决了我的问题。恢复通话后在后台运行的应用程序ios

我在使用AVQueuePlayer对象时,也会在需要时流式播放我的音乐。 现在委托方法已被弃用,因为ios6。所以我使用Notications。

令人惊奇的是,中断结束(电话呼叫结束)的通知,播放音乐也是写代码,但应用程序强制犯规播放音乐,直到它涉及到前台(与另一个通知)

这里是我的代码

-(void)viewWillAppear 
{..... 
    ........ 
    ..... 
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(audioInterruptionNotification:) name:AVAudioSessionInterruptionNotification object:nil]; 
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(audioInterruptionNotification:) name:UIApplicationDidBecomeActiveNotification object:nil] 
} 

-(void)audioInterruptionNotification:(NSNotification *) aNotification { 

NSLog(@"Interrupt %@", aNotification); 
NSDictionary *dict = [aNotification userInfo]; 
NSUInteger typeKey = [[dict objectForKey:@"AVAudioSessionInterruptionTypeKey"] unsignedIntegerValue]; NSLog(@"%d", typeKey); 
if (typeKey == 0) 
{ 
     [avPlayer play]; 
     NSLog(@"1......."); 
    } 
else if (typeKey == 1) 
{ 
    [avPlayer play]; 
    NSLog(@"3..............."); 

    ; 
} 

}

而且,我已经试图通过调度队列引起的延迟。 委托方法似乎不起作用。 但是gaana,saavn和苹果的官方音乐应用程序在通话后恢复。所以这是可能的。 我似乎错过了某些东西。 如果我使用核心电话。我将不得不添加整个框架,这将增加应用程序的大小。 如果这是唯一的方式,请告诉我们如何。

谢谢,我真的很感激你的时间。

+0

你使用过'UIApplicationWillEnterForegroundNotification'吗?请参阅https://developer.apple.com/library/ios/documentation/uikit/reference/UIApplication_Class/Reference/Reference.html#//apple_ref/c/data/UIApplicationWillEnterForegroundNotification –

+1

为什么前景通知会被解雇?我的应用程序从非活动状态转换为背景状不过谢谢你。 –

回答

3

所以我回了一段时间后,我看到我的问题仍然没有答案。那么我已经解决了我的问题。原来,这只是一个简单的陈述,失踪了。

[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

以及我写在我的问题中的代码。

+0

对我来说不是必需的。只需在dispatch_after中包装unpause就行了。虽然这是在iOS 8.3上。可能是版本之间的微小差异。 – user3099609

+1

关于多媒体事件的最重要的方法。 – OhadM

0

我已经解决了这个问题 - 应用程序被电话中断,音乐消失如何?

  1. 当应用程序启动,请致电:
    OSStatus result1 = AudioSessionInitialize(NULL, NULL, interruptionListener, NULL);
  2. 实现interruptionListener

    void interruptionListener(void * inClientData, UInt32 inInterruptionState){ 
        if (inInterruptionState == kAudioSessionBeginInterruption) 
        { 
         NSLog(@"Begin kAudioSessionBeginInterruption");   
         alcMakeContextCurrent(NULL); // important 
        } 
        else if (inInterruptionState == kAudioSessionEndInterruption) 
        { 
         AVAudioSession * audioSession = [AVAudioSession sharedInstance]; 
         NSError * err = nil; 
         [audioSession setCategory :AVAudioSessionCategoryPlayback error:&err];  // important 
         if(err){ 
          NSLog(@"audioSession: %@ %ld %@", [err domain], (long)[err code], [[err userInfo] description]); 
          return; 
         } 
         err = nil; 
         [audioSession setActive:YES error:&err]; 
         if(err){ 
          NSLog(@"audioSession: %@ %ld %@", [err domain], (long)[err code], [[err userInfo] description]); 
          return; 
         } 
    
         //AudioSessionSetActive(true); //sometimes have no effect 
    
         // use alcMakeContextCurrent to set the context——with the context you stored before // important 
         NSLog(@"End kAudioSessionEndInterruption"); 
        } 
    } 
    

    现在它的确定,当钟或电话中断,音乐仍然可以播放。

  3. 但是,如果您接听电话并触摸'HOME'(尚未挂断),并回到挂断状态,并返回到应用程序,音乐无法播放,您应该这样做:
    当应用程序从后台转到前台时,请设置AVAudioSessioncontext。 (不只是在中断结束时设置它们)