2014-02-19 70 views
2

其实我从获取歌曲文档目录当iPhone被锁定时,AVAudioPlayer不在背景中播放

我提到这个链接: - http://www.raywenderlich.com/29948/backgrounding-for-ios,但它只会播放来自捆绑的歌曲。但我想播放文件目录中的歌曲。

我试图在plist中

  • 应用

    1. 背景模式不会在后台=没有在plist中

    2. 在AppDelegate中didFinishLaunchingWithOptions: -

      NSError *setCategoryErr = nil; 
      NSError *activationErr = nil; 
      [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:&setCategoryErr]; 
      [[AVAudioSession sharedInstance] setActive:YES error:&activationErr]; 
      
    3. PlayMethod()

      { 
      NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
      
      NSString *documentsDirectory = [paths objectAtIndex:0]; 
      
      NSString *path = [documentsDirectory stringByAppendingPathComponent:saveFileName]; 
      
      NSURL *url1 = [[NSURL alloc] initFileURLWithPath: path]; 
      
      self.audioPlayer=[[AVAudioPlayer alloc] initWithContentsOfURL:url1 error:NULL]; 
      
      [self.audioPlayer play]; 
      self.seekBarSlider.minimumValue = 0.0f; 
      self.seekBarSlider.maximumValue = self.audioPlayer.duration; 
      //[self updateTime]; 
      
      self.isPlaying=YES; 
      
      [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil]; 
      [[AVAudioSession sharedInstance] setActive: YES error: nil]; 
      
      _audioPlayer.delegate=self; 
      
      NSError *error; 
      
      UIBackgroundTaskIdentifier bgTaskId = 0; 
      
      if (_audioPlayer == nil) 
          NSLog([error description]); 
      else{ 
      
      
          UIBackgroundTaskIdentifier newTaskId = UIBackgroundTaskInvalid; 
      
          [_audioPlayer prepareToPlay]; 
      
          if([_audioPlayer play]){ 
      
           newTaskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL]; 
      
          } 
      
          if (newTaskId != UIBackgroundTaskInvalid && bgTaskId != UIBackgroundTaskInvalid) 
           [[UIApplication sharedApplication] endBackgroundTask: bgTaskId]; 
      
          bgTaskId = newTaskId; 
      } 
      
      } 
      
    4. 数据保护:

      Under the Xcode -> capabilities . 
      

      所有我试过,但没有工作!谁能帮我。

  • 回答

    1

    我使用了数据保护机制,特别是在我要将NSDATA写入Documents目录之前。我需要将Protection None设置为特定的文件路径。所以我用NSProtectionNone文件路径的属性。 如果我们不设置ProtentionNone属性,Apple将不允许访问锁定状态上的文件。

    NSDictionary *protection = [NSDictionary dictionaryWithObject:NSFileProtectionNone forKey:NSFileProtectionKey]; 
    [[NSFileManager defaultManager] setAttributes:protection ofItemAtPath:_path error:nil]; 
    
    if([_rawData writeToFile:_path atomically:YES]) 
    { 
        NSLog(@"HOpefully written into Documentz directory.."); 
    
    } 
    else 
    { 
        NSLog(@"Writting file mechanism - Failed!"); 
    } 
    

    而我曾经从应用程序包中无限循环播放虚拟音频文件。所以虚拟音频文件正在使用AVFoundation Framework持续播放。所以我可以连续访问Documents目录音频文件。逐个。

    1

    当您的设备解锁后,它在后台工作吗?

    如果是这样,它听起来像一个权限问题。如果您已在developer.apple.com上为您的应用程序启用了数据保护。虽然设备被密码锁定,但您将无法读取文档目录,因为这些文件已被加密。

    我们在设备锁定时试图写入数据库时​​出现卡住现象。浪费了两天的时间才弄清楚了这一点。

    W¯¯

    • 编辑 - 如果设置在UIBackgroundModes键还可以发现在功能
    0

    在Xcode中设置你的应用程序的Info.plistaudio,音频将继续播放时背景执行。

    音频:该应用在后台播放可听内容。

    更多关于UIBackgroundModes键检查here

    +0

    我也试过这个。但不工作@deimus – iTag

    0

    是否有当前在设备上运行的其他音频/视频应用程序吗?其中一些也使用AVAudioSessionCategoryPlayback可以中断您的音频会话。

    How it is said on Apple Developer

    默认情况下,使用这一类意味着你的应用程序的音频nonmixable激活您的会话将中断它也nonmixable任何其他音频会议。要允许为此类别混音,请使用AVAudioSessionCategoryOptionMixWithOthers选项。

    相关问题