我想制作带有文字的标签,当标签变成我想要的文字时, 我希望只播放一次声音
我的意思是我有一个按钮,当点击它时会检查标签文本,如果标签文本是我想要它播放的声音(声音时间不超过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 :(得分:1)
好的,首先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