2011-06-03 35 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

相關問題