2011-08-29 196 views
-3

嗨,我試圖播放頁面轉動聲音,當我點擊「下一步」按鈕,帶我到下一頁,但聲音不來,而當我按下「播放「音樂按鈕,它播放音樂,請任何人都可以在這裏幫助。 下面是代碼:難以播放音頻文件

- (IBAction)next { 

    // This part plays the next page turn noise 
    NSURL *this = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/next.mp3", [[NSBundle mainBundle] resourcePath]]]; 

    NSError *error; 
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:this error:&error]; 
    [audioPlayer setDelegate:self]; 
    audioPlayer.numberOfLoops = 0; 
    [audioPlayer play]; 

    // This part takes us to the next view 
    Rabbana2 *rab = [[Rabbana2 alloc] initWithNibName:@"Rabbana2" bundle:nil]; 
    [UIView beginAnimations:@"flipView" context:Nil]; 
    [UIView setAnimationDuration:2]; 
    [UIView setAnimationCurve:UIViewAnimationOptionCurveEaseInOut]; 
    [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES]; 

    [self.view addSubview:rab.view]; 
    [UIView commitAnimations]; 

} 

// This button plays the audio 
- (IBAction)play { 

    if(clicked == 0){ 
     clicked = 1; 
     NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/rabbana1.wav", [[NSBundle mainBundle] resourcePath]]]; 

     NSError *error; 
     audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error]; 
     [audioPlayer setDelegate:self]; 
     audioPlayer.numberOfLoops = 0; 


     [audioPlayer play]; 
     [start setImage:[UIImage imageNamed:@"Sstop.png"] forState:UIControlStateNormal]; 

    } 
    else{ 
     [audioPlayer release]; 
     clicked = 0; 
     [start setImage:[UIImage imageNamed:@"Pplay.png"] forState:UIControlStateNormal]; 
    } 

} 

//If user does not do anything by the end of the sound set the button to start 
- (void) audioPlayerDidFinishPlaying: (AVAudioPlayer *) player successfully: (BOOL) flag { 
    if (flag==YES) { 
     clicked = 0; 
     [start setImage:[UIImage imageNamed:@"Pplay.png"] forState:UIControlStateNormal]; 
    } 
+1

我什麼也看不到你的代碼明顯錯誤,你會得到任何警告,錯誤等?你確定聲音文件源的URL構造正確嗎? –

+0

[Cocoa audio issues]可能的重複(http://stackoverflow.com/questions/7231346/cocoa-audio-issues) – coneybeare

+1

如果你想得到任何答案,我建議你閱讀http://stackoverflow.com/faq 。重複的問題將被關閉 – coneybeare

回答

0

我將確保您的音頻文件被正確編碼。有可能您的代碼正在執行回放,但是因爲您可能有不正確編碼的音頻文件,它將無法播放。

這裏是一個命令行轉換爲WAV文件CAF(其中iPhone的愛)

afconvert -f卡福-d LEI16轉換@ 44100 -c 1 in.wav out.caf

+1

看起來他遇到問題的「下一個」聲音是.mp3,那麼轉換可能會是什麼樣子? –