你如何让iPhone发出哔哔声?

时间:2010-08-18 03:18:53

标签: iphone objective-c ios

什么代码允许我在iPhone上发出标准的哔声?

2 个答案:

答案 0 :(得分: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];

}

答案 1 :(得分:1)

AudioServicesPlaySystemSound是你能为简单的声音做的一件事。