2010-08-18 78 views

回答

3

那它取決於你想要什麼樣的聲音。

以下介紹如何使用AVFoundation音頻框架播放聲音。

#import <UIKit/UIKit.h> 

     @class AVAudioPlayer; 

     @interface AudioPlayer : UIViewController { 
      IBOutlet UIButton *playButton; 
      IBOutlet UIButton *stopButton; 
      AVAudioPlayer *audioPlayer; 
     } 

     @property (nonatomic, retain) IBOutlet UIButton *playButton; 
     @property (nonatomic, retain) IBOutlet UIButton *stopButton; 
     @property (nonatomic, retain) AVAudioPlayer *audioPlayer; 

     -(IBAction)play; 
     -(IBAction)stop; 

     @end 

    - (void)viewDidLoad { 
     [super viewDidLoad]; 

     // Get the file path to the song to play. 
     NSString *filePath = [[NSBundle mainBundle] pathForResource:@"TNG_Theme" 
                  ofType:@"mp3"]; 

     // Convert the file path to a URL. 
     NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath]; 

     //Initialize the AVAudioPlayer. 
     self.audioPlayer = [[AVAudioPlayer alloc] 
           initWithContentsOfURL:fileURL error:nil]; 

     // Preloads the buffer and prepares the audio for playing. 
     [self.audioPlayer prepareToPlay]; 

     [filePath release]; 
     [fileURL release]; 

    } 

-(IBAction)play { 

    // Make sure the audio is at the start of the stream. 
    self.audioPlayer.currentTime = 0; 

    [self.audioPlayer play]; 

} 

-(IBAction)stop { 

    [self.audioPlayer stop]; 

} 
+0

嗨Fulvio, 我只是在用戶按下不正確的鍵時發出標準系統嘟嘟聲。 – WaterBoy 2010-08-18 04:24:22

1

AudioServicesPlaySystemSound是一件你可以做的簡單的聲音。

+0

如果用戶做錯了什麼,我只是在標準的iPhone嗶聲之後? – WaterBoy 2010-08-18 04:23:39

+0

「標準iPhone嗶聲」是什麼意思? – 2010-08-18 05:39:54

+0

仍然不確定標準iPhone蜂鳴聲的含義。 – fuzz 2010-09-03 06:34:30