当iPhone被锁定时,AVAudioPlayer无法在后台播放

编程入门 行业动态 更新时间:2024-10-23 17:38:55
本文介绍了当iPhone被锁定时,AVAudioPlayer无法在后台播放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

实际上我从文档目录中获取歌曲。

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无法在后台播放

    本文发布于:2023-11-27 08:33:47,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1637301.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:后台   iPhone   AVAudioPlayer

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!