我正在尝试播放带有永久循环的音乐文件。 我的代码是:
NSString *path = [[NSBundle mainBundle] pathForResource:@"theme" ofType:@"mp3" inDirectory:@"assets/audios" ];
NSURL *url = [NSURL fileURLWithPath:path];
NSError *error;
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = -1;
audioPlayer.volume = 1.0;
if (audioPlayer == nil){
NSLog(@"Error creating player: %@", error);
}
[audioPlayer prepareToPlay];
[audioPlayer play];
但它什么都没做。没有播放任何内容并且打印了任何消息日志。
以下代码效果很好:
NSString *path = [[NSBundle mainBundle] pathForResource:@"theme" ofType:@"mp3" inDirectory:@"assets/audios" ];
NSURL *url = [NSURL fileURLWithPath:path];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((CFURLRef)CFBridgingRetain(url), &soundID);
AudioServicesPlaySystemSound (soundID);
但我需要控制音量和循环。
我不知道我做错了什么。 任何人都可以帮助我吗?