开发者

make a **beep sound** in my application? [duplicate]

开发者 https://www.devze.com 2023-03-09 23:19 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: How do you make an Iphone beep
This question already has answers here: Closed 11 years ago.

Possible Duplicate:

How do you make an Iphone beep

hi, i have text box and a image view. i have two images named as CAT and DOG. when i entered texts in my开发者_如何转开发 text box ,image view will display the curresponding named images. if i entered a word that is not DOG and CAT(means: there is no image in my app same as text box value name) or there is no url found i need to make a beep sound. can any one tell me a good way to do it. now i am simply showing an image named as "no image". i need to make a beep sound at this situation.


We can use AVAudioPlayer:

#import <UIKit/UIKit.h>
@class AVAudioPlayer;

@interface AudioPlayer : UIViewController {
  AVAudioPlayer *audioPlayer;
}

@property (nonatomic, retain) AVAudioPlayer *audioPlayer;

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

@end

@implementation AudioPlayer

- (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];
}

To play

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

To stop

  [self.audioPlayer stop];


See this thread

How do you make an iPhone beep?

Another thread on this is

has Iphone built in beep sound effect

You may also wanted to see this video

http://www.youtube.com/watch?v=iUDnUAveqtU

0

精彩评论

暂无评论...
验证码 换一张
取 消