那么,如果在(iOS)中保持某个按钮,我将如何制作声音文件播放和循环?当按钮松开时我还需要它停止吗?我试过搜索谷歌,只找到人们问的论坛,但没有真正的答案如何做到这一点。
答案 0 :(得分:2)
使用按钮控制事件触地,touchupinside和touchupoutside:
UIButton *theButton = [[UIButton alloc]initWithFrame:CGRectMake(40.0, 40.0, 200.0, 30.0)];
[theButton setTitle:@"button" forState:UIControlStateNormal];
[theButton setBackgroundColor:[UIColor redColor]];
[theButton addTarget:self action:@selector(startMusic) forControlEvents:UIControlEventTouchDown];
[theButton addTarget:self action:@selector(stopMusic) forControlEvents:UIControlEventTouchUpInside];
[theButton addTarget:self action:@selector(stopMusic) forControlEvents:UIControlEventTouchUpOutside];
编辑: 意识到你的问题可能更多是关于播放声音文件?!
有不同的框架,你可以使用AVAudioPlayer:
导入框架并定义播放器
#import <AVFoundation/AVFoundation.h>
AVAudioPlayer *newPlayer;
为主文件中的按钮创建您的ibactions:
-(IBAction)startMusic{
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource: @"mySoundFile"
ofType: @"wav"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL: fileURL error: nil];
newPlayer.numberOfLoops = -1; // Loop indefinately
[newPlayer play];
}
和
-(IBAction)stopMusic{
if(newPlayer isPlaying){
[newPlayer stop];
}
[newPlayer release];
}