2010-08-18 70 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