4

我有一个 ios 应用程序,当它进入后台时会继续播放音乐。现在,如果有电话,无论是否接听,应用程序都不会继续播放音乐。两天来,我一直在这里阅读有关此问题的帖子。他们都没有解决我的问题。

我正在使用 AVQueuePlayer 对象,因为我也在需要时流式传输我的音乐。现在委托方法自 ios6 以来已被弃用。所以我正在使用通知。

令人惊奇的是,中断结束(电话结束)被通知,播放音乐的代码也被编写,但应用程序直到前台才播放音乐(有另一个通知)

这是我的代码

 -(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 和苹果官方音乐应用程序在通话后恢复。所以这是可能的。我似乎错过了什么。如果我使用核心电话。我将不得不添加一个完整的框架,这将增加应用程序的大小。如果这是唯一的方法。请告诉如何。

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

4

2 回答 2

3

所以我现在过了一会儿又回来了,我看到我的问题仍然没有答案。好吧,我已经解决了我的问题。事实证明,这只是缺少一个简单的陈述。

[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

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

于 2014-09-26T12:35:43.710 回答
0

我已经解决了——App被电话打断,音乐消失了怎么办?

  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'(没有挂断),然后回到挂断,再回到app,音乐就不能播放了,你应该这样做:
    当app从后台退出时到前台,设置AVAudioSessionand context。(不只是在中断结束时设置它们)

于 2015-05-13T12:34:55.267 回答