2011-06-03 58 views
0

可能重复:
How do you make an Iphone beep在我的应用程序中发出**哔声**

喜, 我有文本框和一个图像视图。 我有两个名为CAT和DOG的图像。 当我在文本框中输入文本时,图像视图将显示相应的命名图像。 如果我输入的字不是DOG和CAT(意思是:我的应用程序中没有与文本框中的值名称相同的图像),或者没有找到网址,我需要发出嘟嘟声。 任何人都可以告诉我一个很好的方法来做到这一点。 现在我只是显示一个名为“无图像”的图像。在这种情况下,我需要发出哔哔声。

+0

这不是Xcode的,但可可触摸相关。请使用适当的标签。 – vikingosegundo 2011-07-03 21:17:34

回答

3

我们可以用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]; 
} 

播放

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

要停止

[self.audioPlayer stop]; 
+0

+1对于TNG_Theme :) – Joshua 2013-08-18 15:06:19