-4

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

我正在使用 Xcode 版本 6。我已经实现了 AVFoundation 框架

    - (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];
}
4

1 回答 1

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];
    }

//在此之前添加import 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
    {

    }

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

于 2013-07-12T08:04:43.960 回答