2013-04-22 48 views
0

這是一個新手問題。iOS按鈕無法播放按鈕水龍頭上的音頻文件

我有一個非常簡單的應用程序是應該當主視圖按鈕被竊聽只播放音頻文件。

我使用的XCode 4.6版。

我增加了一個音頻文件「audioclip.wav」我的項目。我已添加AVFoundation.framework

我的項目只有ViewController.hViewController.m

我雙擊並使用助理編輯器創建我的IBAction連接從按鈕拖在故事板.h文件。

我ViewController.h:

#import <UIKit/UIKit.h> 
#import<AVFoundation/AVAudioPlayer.h> 

@interface ViewController : UIViewController <AVAudioPlayerDelegate> { 
} 
- (IBAction)playAudio:(id)sender; 

@end 

我ViewController.m:

#import "ViewController.h" 

@interface ViewController() 

@end 

@implementation ViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

- (IBAction)playAudio:(id)sender { 
    AVAudioPlayer *audioPlayer; 
    NSString *audioPath = [[NSBundle mainBundle] pathForResource:@"audioclip" ofType:@"wav" ]; 
    NSURL *audioURL = [NSURL fileURLWithPath:audioPath]; 
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:nil]; 
    [audioPlayer play]; 
} 

@end 

無論出於何種原因(可能是一些愚蠢的我離開了)的audioclip.wav文件不,當我點擊播放按鈕。我已經在iPhone 6.1模擬器和我的設備上進行了測試。

+0

檢查IBOutlet中和按鈕操作? – Balu 2013-04-22 04:18:05

+0

@ brno792檢查您是否在TouchUpInside方法上連接了按鈕。 – 2013-04-22 04:21:28

+0

看到我的答案.. http:// stackoverflow。com/questions/16033723/audio-file-not-working-on-device-but-it-is-working-on-simulator/16033796#16033796 – 2013-04-22 04:23:12

回答

6

我認爲有一些問題,wav文件。有些人似乎開始工作,但我已經嘗試了幾個不同的文件,而且他們都沒有播放。在下面的代碼中註釋掉的mp3和m4a文件運行良好。沒有必要對委託做任何事情,這是可選的。

#import "ViewController.h" 
    #import <AVFoundation/AVFoundation.h> 

    @interface ViewController() 
    @property (strong,nonatomic) AVAudioPlayer *audioPlayer; 
    @end 

    @implementation ViewController 


    - (IBAction)playAudio:(id)sender { 
     //NSString *audioPath = [[NSBundle mainBundle] pathForResource:@"11 Prayer" ofType:@"mp3" ]; 
     //NSString *audioPath = [[NSBundle mainBundle] pathForResource:@"Sunshine" ofType:@"m4a" ]; 
     NSString *audioPath = [[NSBundle mainBundle] pathForResource:@"space" ofType:@"wav" ]; 
     NSURL *audioURL = [NSURL fileURLWithPath:audioPath]; 
     NSError *error; 
     self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:&error]; 
     [self.audioPlayer play]; 
    } 

如果我在錯誤日誌中與wav文件,我得到:

Error Domain=NSOSStatusErrorDomain Code=1685348671 "The operation couldn’t be completed. (OSStatus error 1685348671.)" 
+0

Thankyou @rdelmar – brno792 2013-04-22 05:26:00

+0

@ brno792,你真的明白了嗎?如果是這樣,怎麼樣?我想知道。 – rdelmar 2013-04-22 05:29:29

+0

我沒有@property所以也許就是這樣。或者我有錯誤=零,而不是錯誤=&錯誤,所以也許這也是它。它現在有用,所以謝謝。我仍然使用.wave。如果我想在按下按鈕時隨機選擇一個音頻文件從3或4播放,我該怎麼辦?謝謝。 – brno792 2013-04-22 05:32:21

0

嘗試這樣,

 NSString *audio=[[NSBundle mainBundle]pathForResource:@"audioclip" ofType:@"wav"]; 
    NSURL *url=[[NSURL alloc]initFileURLWithPath:audio]; 
    avPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil]; 
    avPlayer.delegate = self; 
    [avPlayer prepareToPlay]; 
    [avPlayer play]; 

和檢查一次IBOutlet連接與否和使用斷點檢查一下按鍵的操作方法。

+0

只是方法調用iboutlet不是強制的,而是適當的事件應該觸發。 – 2013-04-22 04:20:50

+0

謝謝,所以我嘗試了你的解決方案。我不得不添加AVAudioPlayer * avPlayer = ....我也檢查了IBOutlet,我收到了操作:playAudio:Button Touch Up Inside。所以我認爲它是連接的。我仍然沒有聽到任何聲音。 – brno792 2013-04-22 05:01:36

+0

一旦檢查您的音頻文件是否有任何音樂或不/ /? – Balu 2013-04-22 05:03:07

1

請儘量使用這一個..

- (IBAction)playAudio:(id)sender 
{ 
    NSURL *url=[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"audioclip" ofType:@"wav"]]; 
    NSData *data =[NSData dataWithContentsOfURL:url]; 
    audioPlayer = [[AVAudioPlayer alloc] initWithData:data error:nil]; 
    audioPlayer.delegate = self; 
    [audioPlayer setNumberOfLoops:0]; 

    [audioPlayer play]; 
} 

我希望這將幫助ü

0

,我可以看到你所做的代碼非常簡單,所以它的製作方式確實需要它不一個大的wav文件。

和保存您需要的音頻文件削減到像10秒下。

0

我遇到這個問題時使用了.m4a。對我來說,不執行AVAudioSession是問題

VAR會話= AVAudioSession.sharedInstance()

do { 
     session.setCategory(AVAudioSessionCategoryPlayAndRecord) 
     try session.setActive(true) 
    } catch { 
     gf.displayAlert("problem encountered", userMessageTitle: "") 
     self.navigationController?.popViewControllerAnimated(true) 
    }