如何在我在xcode中创建的动画中播放声音?

问题描述:

我设法创建了包含动画的应用程序。但是我想通过添加一个从网络上下载的.wav文件使其脱颖而出,更加有趣。我希望声音在动画期间播放。才开始编码一个月前,所以任何帮助,将深表赞赏如何在我在xcode中创建的动画中播放声音?

+0

请向我们展示代码示例以及您尝试过的任何迄今尚未运行的代码示例。 –

您可以使用AVAudioPlayer

只需创建一个方法,并将其连接到您的按钮或动画

-(void) playLoopingMusicInApplication { 
     AVAudioPlayer *audioPlayer; 

    // set the pathForResource: to the name of the sound file, and ofType: to AAC preferrably. 
    NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"fileName" ofType:@"WAV"]; 
    NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath]; 
    player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil]; 

    player.delegate = self; 

    //infinite 
    player.numberOfLoops = -1; 

    [player play]; 
    //[player release]; 
} 
  • PS - 你将需要获得AVFoundation框架,并AudioToolbox框架项目

希望这有助于!

+0

这非常棒。请问另一个问题:如何创建一个方法将其连接到我的动画? –