当玩家玩游戏时,我的应用程序会崩溃

时间:2013-07-12 07:30:09

标签: ios

我有一个非常简单的应用程序,它应该只在视图上播放音频文件。相反,我的Xcode崩溃了。

我正在使用Xcode版本6.我已经实现了AVFoundation Framework

    - (void)viewDidLoad
      {
        [super viewDidLoad];
        NSString *audioPath = [[NSBundle mainBundle] pathForResource:@"Crowd_cheering"                            
        ofType:@"m4a" ];  
        NSURL *audioURL = [NSURL fileURLWithPath:audioPath];
        NSError *error;
        self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:&error];
        [self.player play];
}

1 个答案:

答案 0 :(得分:1)

//尝试使用错误条件:

     NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
        pathForResource:@"Moderato"
        ofType:@"mp3"]];

    NSError *error;
    _audioPlayer = [[AVAudioPlayer alloc]
                     initWithContentsOfURL:url
                     error:&error];
    if (error)
    {
            NSLog(@"Error in audioPlayer: %@",
                  [error localizedDescription]);
    } else {
            _audioPlayer.delegate = self;
            [_audioPlayer prepareToPlay];
    }

//在此之前添加导入AVFoundation / AVFoundation.h并添加委托AVAudioPlayerDelegate

//然后像这样实现AVAudioPlayerDelegate协议方法

    -(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
     {

     }
    -(void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error
     {

     }
    -(void)audioPlayerBeginInterruption:(AVAudioPlayer *)player
     {

     }
    -(void)audioPlayerEndInterruption:(AVAudioPlayer *)player
    {

    }

确保你正确连接了你的播放按钮,还检查你是否正确地将音频文件添加到项目资源中。