【问题标题】:Resuming an app running in background after a phone call ios打电话后恢复在后台运行的应用程序 ios
【发布时间】:2014-03-27 04:53:48
【问题描述】:

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

谢谢。非常感谢您的宝贵时间。

【问题讨论】:

标签: ios background multitasking interruption


【解决方案1】:

所以我过了一会儿回来了,我发现我的问题仍未得到解答。好吧,我已经解决了我的问题。 事实证明,它只是缺少一个简单的语句。

[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

连同我在我的问题中编写的代码。

【讨论】:

  • 我不需要。只需将 unpause 包装在 dispatch_after 中就可以了。虽然这是在 iOS 8.3 上。版本之间可能存在微小差异。
  • 多媒体事件最重要的方法。
【解决方案2】:

我已经解决了-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,音乐就不能播放了,你应该这样做:
    当应用程序从后台转到前台时,设置AVAudioSessioncontext。 (不只是在中断结束时设置它们)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-24
    • 1970-01-01
    • 2012-05-27
    • 2015-04-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多