1

如果设备未锁定,则音乐正在后台播放。我已将代码粘贴到 .Plist 文件中,这是我使用的代码

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:saveFileName];
    NSURL *url1 = [[NSURL alloc] initFileURLWithPath: path];
    appdelegate.sharedplayer=[[AVAudioPlayer alloc] initWithContentsOfURL:url1 error:NULL];
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
    [[AVAudioSession sharedInstance] setActive: YES error: nil];
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

    [appdelegate.sharedplayer setVolume:appdelegate.volumeDelegate];

    [appdelegate.sharedplayer play];
4

3 回答 3

3

当设备被密码锁定时,Apple iOS 会限制数据访问。所以你将 NSDocumetDirectory 转换为 NSData 的歌曲。并分配 AVAudioPlayer 它工作。

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:saveFileName];
NSURL *url1 = [[NSURL alloc] initFileURLWithPath: path];
NSData *data=[[NSData alloc]initWithContentsOfURL:url1];
appdelegate.sharedplayer=[[AVAudioPlayer alloc] initWithData:data error:nil];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[appdelegate.sharedplayer setVolume:appdelegate.volumeDelegate];
[appdelegate.sharedplayer play];
于 2013-11-15T09:08:20.383 回答
-1

您需要对 plist 文件进行一些更改。

a-设置您的应用程序不在后台运行 b-将后台模式设置为应用程序播放音频

使用此代码 AppDelege

NSError *setCategoryErr = nil;
    NSError *activationErr  = nil;
    [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:&setCategoryErr];
    [[AVAudioSession sharedInstance] setActive:YES error:&activationErr];
于 2013-11-13T17:05:04.873 回答
-1

尝试在你的 project.plist 中设置它

在你的 plist 中设置这个

于 2013-11-15T09:29:31.863 回答