如何在特定的View Controller上循环音频

Aus*_*inj 1 iphone audio xcode ios

-(void)viewDidAppear:(BOOL)animated

{
    CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef soundFileURLRef;
    soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"Intro",
                                              CFSTR ("wav"), NULL);
    UInt32 soundID;
    AudioServicesCreateSystemSoundID (soundFileURLRef, &soundID);
    AudioServicesPlaySystemSound (soundID);
}
Run Code Online (Sandbox Code Playgroud)

我是这个的初学者.我正在尝试在视图控制器的后台播放一个wav文件.我最初制作了一个30秒的wav文件,它不会播放,我将音频文件的长度减少到10秒,它在视图控制器上播放正常.我的问题是,如何循环播放此音频文件和/或播放更长的音频文件.此外,当我离开视图控制器时,音频是否会停止.

我正在使用xCode 4.3我在iPhone4和4s上运行应用程序(iOS 5.1)

提前致谢...

M J*_*sse 5

Apple文档声明

http://developer.apple.com/library/ios/#documentation/AudioToolbox/Reference/SystemSoundServicesReference/Reference/reference.html

您可以使用系统声音服务播放短(30秒或更短)的声音.界面不提供电平,定位,循环或定时控制,并且不支持同时播放:您一次只能播放一个声音.您可以使用System Sound Services提供声音警报.在某些iOS设备上,警报可能包括振动.

因此,使用系统声音时没有循环可用.

你可以查看这个教程 http://mobileorchard.com/easy-audio-playback-with-avaudioplayer/

- (void)viewDidLoad {
[super viewDidLoad];

NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/audiofile.mp3", [[NSBundle mainBundle] resourcePath]]];

NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = -1;

if (audioPlayer == nil)
    NSLog([error description]);
else
    [audioPlayer play];

}
Run Code Online (Sandbox Code Playgroud)

它声明如果将numberOfLoops属性设置为负数会导致audioPlayer无限循环

并且不要忘记将AVFoundation框架添加到您的项目中