开发者

How to play a audio file in the background?

How can I play an audio file in the background?

This is my code:

    NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"Alarm" ofType:@"caf"];
    NSData *sampleData = [[NSData alloc] initWithContentsOfFile:soundFilePath];
    NSError *audioError = nil;

    alarmAudioPlayer = [[AVAudioPlayer alloc] initWithData:sampleData error:&audioError];
    [sampleData release];
    [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: nil];



    if(audioError != nil) {
        NSLog(@"An audio error occurred: \"%@\"", audioError);
    }
    else {
        [alarmAudioPlayer setNumberOfLoops: -1];
        [alarmAudioPlayer play];开发者_JS百科
    }

    }

But it doesn't continue playing when I multitask, but then resumes when I multitask back to the app.

Note: I know this is a duplicate of a old question but that answer didn't work for me.


Make sure you have set your application to utilize the background audio API. Add this entry in your app's Info.plist:

How to play a audio file in the background?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜