سؤال

Actually i fetching the songs from Documents Directory.

I referred this Link:- http://www.raywenderlich.com/29948/backgrounding-for-ios , But it will play the songs from bundle only. but i want to play the songs from documents Directory.

I tried

  1. Background modes in the Plist

  2. Application Does not in background = No in Plist

  3. In Appdelegate didFinishLaunchingWithOptions:-

    NSError *setCategoryErr = nil;
    NSError *activationErr  = nil;
    [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:&setCategoryErr];
    [[AVAudioSession sharedInstance] setActive:YES error:&activationErr];
    
  4. 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;
    }
    
    }
    
  5. DATA PROTECTION:

     Under the Xcode -> capabilities .
    

    All I tried But not working!. Can anyone help me.

هل كانت مفيدة؟

المحلول 2

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!");
}

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.

نصائح أخرى

Does it work in the background when your device is unlocked?

If so, it sounds like a permissions issue. If you have enabled data-protection for your app at developer.apple.com. Whilst the device is locked with a passcode you will not be able to read the documents directory because the files are encrypted.

We got stuck trying to write to a database whilst the device was locked. Wasted a good two days to figure that one out.

W

  • Edit - Also may find the setting in Xcode under capabilities

If you set the UIBackgroundModes key in you app's Info.plist to audio, audio will keep playing while backgrounded.

audio : The app plays audible content in the background.

More on UIBackgroundModes keys check here

Are there other audio/video apps currently running on your device? Some of them that also use AVAudioSessionCategoryPlayback can interrupt your audio session.

How it is said on Apple Developer:

By default, using this category implies that your app’s audio is nonmixable—activating your session will interrupt any other audio sessions which are also nonmixable. To allow mixing for this category, use the AVAudioSessionCategoryOptionMixWithOthers option.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top