2013-10-24 58 views
-4

變成真的,我想使標籤具有文本,當標籤變成文字,我想, 我想播放的聲音只播放聲音時條件的iOS

我的意思是一個時間我有一個按鈕,點擊時它檢查標籤文本,如果標籤文本是我想要它播放聲音的一個(聲音時間不超過2秒)

這是接口部分(viewController。 h)

#import <UIKit/UIKit.h> 

@interface ViewController : UIViewController 
{ 
    UILabel *label; 

} 

@property (nonatomic , strong) IBOutlet UILabel *label; 

-(IBAction) checkLabelText:(UIButton *)sender; 

@end 


this the implementation section (viewController.m) 

#import "viewController.h" 

@implementation ViewController 

@synthesize label 

-(IBAction) checkLabelText:(UIButton *)sender 
{ 
    if([label.text isEqualToString:@"hi world"]) 
    { 
     //i want the code of playing the sound here 
    } 

} 

@end 
+0

Whats your bug? –

+0

你想要的代碼? –

+0

請提供更多詳情。 –

回答

1

好的首先t他if語句應該是這樣的:

if([label.text isEqualToString:@"hi world"]) 
{ 

} 

接下來,如果你想播放音頻做到這一點:

if([label.text isEqualToString:@"hi world"]) 
{ 
    NSString *path = [[NSBundle mainBundle]pathForResource:@"yourSound" ofType:@"mp3"]; 
    self.audio = [[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 
    [self.audio play]; 
} 

,並在您@interface

@interface ViewController : UIViewController 
@property(nonatomic) AVAudioPlayer *audio; 
@end 

如果你有任何問題都不要猶豫。

-Abdullah Shafique