2013-10-26 178 views
1

我試圖播放聲音,當我點擊一個按鈕,聲音剪輯是10秒,應用程序運行良好,當我點擊按鈕沒有任何事情發生沒有錯誤或沒有任何東西,這是我的代碼當按鈕點擊時沒有聲音

.m file 
#import "ViewController.h" 
#import <AVFoundation/AVFoundation.h> 
- (IBAction)Play:(id)sender { 

    AVAudioPlayer *audioPlayer; 
    NSString *audioPath = [[NSBundle mainBundle]pathForResource:@"Blesgaes" ofType:@"mp3"]; 
    NSURL *audioURL =[NSURL fileURLWithPath:audioPath]; 
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:nil]; 
    [audioPlayer play]; 
} 
.h file 
#import <AVFoundation/AVFoundation.h> 


@interface ViewController : UIViewController 
- (IBAction)Play:(id)sender; 

我已經添加AVFoundation框架,我按照這些步驟

http://looksok.wordpress.com/2013/01/19/ios-tutorial-play-sound/ http://www.techotopia.com/index.php/Playing_Audio_on_an_iPhone_using_AVAudioPlayer_(iOS_6) http://code-and-coffee.blogspot.in/2012/09/how-to-play-audio-in-ios.html

什麼我d任何想法錯了。

+0

檢查連接播放:(ID)發送 –

+0

連接好 – Sleeperinn

+0

通NSError對象錯誤。 –

回答

1

下面是簡單的代碼: - 按鈕(IBAction爲)之間

- (IBAction)playAudio:(id)sender { 
    AVAudioPlayer *audioPlayer; 
    NSString *audioPath = [[NSBundle 
    mainBundle] pathForResource:@"Woof" 
    ofType:@"mp3"]; 
    NSURL *audioURL = [NSURL 
    fileURLWithPath:audioPath]; 
NSError *audioError = [[NSError alloc] init]; 
audioPlayer = [[AVAudioPlayer alloc]  
initWithContentsOfURL:audioURL 
error:&audioError]; 

    if (!audioError) { 
    [audioPlayer play]; 
    NSLog(@"Woof!"); 
} 
else { 
    NSLog(@"Error!"); 
} 
    }