问题描述
实际上我从文档目录中获取歌曲.
我提到了这个链接: - http://www.raywenderlich.com/29948/Brucationing-for-ios ,但它只将播放来自捆绑的歌曲.但我想从文件目录中播放歌曲.
我试过
-
在plist中的背景模式
-
应用程序不在背景中在Plist中没有否
-
在appdelegate didfinishlaunchingwithoptions: -
NSError *setCategoryErr = nil; NSError *activationErr = nil; [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:&setCategoryErr]; [[AVAudioSession sharedInstance] setActive:YES error:&activationErr];
-
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; } }
-
数据保护:
Under the Xcode -> capabilities .
我尝试但不工作!任何人都可以帮助我.
推荐答案
我使用了数据保护机制,特别是在我将NSDATA写入文档目录之前.我需要将PROTEGRE NONE设置为特定的文件路径.所以我使用 nsprotectionnone 属性为文件路径. 如果我们不会设置ProtientionNone属性,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!"); }
和我曾经在app包中播放无限循环中的虚拟音频文件.因此,虚拟音频文件使用Avfoundation框架持续播放.因此,我可以连续访问文档目录音频文件.一个接一个.
其他推荐答案
当您的设备解锁时,它是否在后台工作?
如果是这样,它听起来像一个权限问题.如果您在Developer.apple.com上为应用程序启用了数据保护.虽然设备被锁定有密码,但您将无法读取文档目录,因为文件是加密的.
我们试图写入数据库,而设备被锁定.浪费了一个好两天来弄清楚.
w
- 编辑 - 也可以在功能下找到Xcode中的设置
其他推荐答案
如果将app的Info.plist中的UIBackgroundModes键设置为audio,则音频将继续在背景时播放.
音频:该应用程序在后台播放可听内容.
更多关于UIBackgroundModes键检查这里