实际上我从文档目录中获取歌曲。
Actually i fetching the songs from Documents Directory.
我推荐此链接: - www.raywenderlich/29948/backgrounding-for-ios ,但它只播放来自捆绑的歌曲。但我想播放文件目录中的歌曲。
I referred this Link:- www.raywenderlich/29948/backgrounding-for-ios , But it will play the songs from bundle only. but i want to play the songs from documents Directory.
我试过
Plist中的背景模式
Background modes in the Plist
应用程序不在背景中=在Plist中没有
Application Does not in background = No in Plist
在Appdelegate中didFinishLaunchingWithOptions: -
In Appdelegate didFinishLaunchingWithOptions:-
NSError *setCategoryErr = nil; NSError *activationErr = nil; [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:&setCategoryErr]; [[AVAudioSession sharedInstance] setActive:YES error:&activationErr];
PlayMethod()
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; } }
数据保护:
DATA PROTECTION:
Under the Xcode -> capabilities .所有我尝试但不工作!任何人都可以帮助我。
All I tried But not working!. Can anyone help me.
推荐答案
我使用了数据保护机制,特别是之前我打算将NSDATA写入Documents目录。我需要将Protection None设置为特定的文件路径。所以我使用 NSProtectionNone 属性作为文件路径。 如果我们不设置ProtentionNone属性,Apple将不允许访问锁定状态下的文件。
I used Data Protection mechanism and particularly before I am going to write the NSDATA into Documents directory. I need to set the Protection None to particular file path. So I used NSProtectionNone Property for the file path. If we won't set the ProtentionNone property the Apple won't allow to access the file on Locked State.
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 Bundle以无限循环播放Dummy音频文件。因此使用AVFoundation Framework连续播放虚拟音频文件。所以我可以连续访问Documents目录音频文件。一个接一个。
And I used to play the Dummy audio file in infinite loop from App Bundle. so the dummy audio file is playing continuously using AVFoundation Framework. So I can able to access the Documents directory audio files continuously. one by one.
更多推荐
当iPhone被锁定时,AVAudioPlayer无法在后台播放
发布评论