0

即使用户更改视图控制器等,我也想在整个应用程序中播放配乐。我有一个来自配乐的循环,我想在它完成后重复。我目前正在通过以下方式播放声音:

NSString *soundFile = [[NSBundle mainBundle]
                       pathForResource:@"Click03" ofType:@"wav"];
AVAudioPlayer  *audioPlayer = [[AVAudioPlayer alloc]
               initWithContentsOfURL:[NSURL fileURLWithPath: soundFile]
               error:nil];
[audioPlayer start];
 // or [audioPlayer stop];

我不太确定这是否是播放声音的最佳方式。有人可以向我解释如何做我上面问的吗?提前致谢!

4

2 回答 2

1

基本上你正在尝试播放背景音频..

检查以下链接,这将对您有所帮助

使用 AVAudioplayer 在后台播放音乐

http://www.sagorin.org/2011/11/29/ios-playing-audio-in-background-audio/

如需重复音频,请查看此链接

如何重复 AVAudioPlayer?

于 2012-09-24T06:23:51.037 回答
0

编写代码以在 Appdelegate 中播放声音。

-(void)PlaySound:(NSString*)strSoundFileName 
{
    fileURL=[[NSURL alloc] initFileURLWithPath:strSoundFileName];
    audioPlayer=[[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
    audioPlayer.delegate=self;
    [audioPlayer prepareToPlay];

    audioPlayer.currentTime=0;
    [audioPlayer play];
    audioPlayer.numberOfLoops=-1;
}
于 2012-09-24T06:24:47.627 回答