- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self test2];
}
// 播放声音
- (void)test0 {
SystemSoundID outSystemSoundID;
NSURL *inFileURL = [[NSBundle mainBundle] URLForResource:@"yaobuqi" withExtension:@"wav"];
// 创建服务
AudioServicesCreateSystemSoundID((__bridge CFURLRef)(inFileURL), &outSystemSoundID);
// 播放
//AudioServicesPlaySystemSound(outSystemSoundID); // 播放且震动
AudioServicesPlayAlertSound(outSystemSoundID); // 仅播放
}
// 播放音乐
- (void)test1 {
NSURL *url = [[NSBundle mainBundle] URLForResource:@"匆匆那年" withExtension:@"mp3"];
NSError *error = nil;
if (!self.player) {
// 创建播放器
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
if (error) {
NSLog(@"%@", error);
return;
}
// 准备播放
[self.player prepareToPlay];
}
[self play];
}
- (void)play {
if (self.player.isPlaying) {
// 暂停
[self.player pause];
} else {
// 播放
[self.player play];
}
}
// 录音
- (void)test2 {
NSURL *url = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:@"record.wav"]];
NSError *error = nil;
// 创建录音器
self.recorder = [[AVAudioRecorder alloc] initWithURL:url settings:nil error:&error];
if (error) {
NSLog(@"%@", error);
return;
}
// 准备录音
[self.recorder prepareToRecord];
// 开始
[self.recorder record];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[self.recorder stop];
}
音频播放
最新推荐文章于 2024-05-11 14:55:52 发布