2013-07-12 112 views
-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]; 
} 
+2

那麼......錯誤包含了什麼? – borrrden

+0

哪裏測試設備或模擬器? – Alex

+0

@Alex在模擬器上 – user2322662

回答

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 
    { 

    } 

請確保您連接到您的播放按鈕是否正確,並檢查您是否正確添加了音頻文件到項目資源。

相關問題