请帮助我,我厌倦了使用stackOverflow中的以下答案获得答案
我的代码是
- (IBAction)RecordBtnClicked:(UIButton *)sender {
if (sender.isSelected) {
if (!recorder.recording) {
[session setActive:YES error:nil];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
temporaryRecFile = [prefs URLForKey:@"Test1"];
player = [[AVAudioPlayer alloc]initWithContentsOfURL:temporaryRecFile error:nil];
player.delegate = self;
player.volume = 1;
[player prepareToPlay];
[player play];
[sender setSelected:false];
}
`[recordBtn setImage:[UIImage imageNamed:@"a_recordp.png"] forState:UIControlStateSelected];
[recorder stop];
}`else{
if (player.playing) {
[player stop];
}`
if (!recorder.recording) {
[recordBtn setImage:[UIImage imageNamed:@"a_records.png"] forState:UIControlStateSelected];
NSString *documentPath = [pathComponent objectAtIndex:0];
NSString *pathTosave =[documentPath stringByAppendingPathComponent:[self fileNameForRecording]];
NSURL *url = [NSURL fileURLWithPath:pathTosave];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setURL:url forKey:@"test1"];
session = [AVAudioSession sharedInstance];
[session setActive:YES error:Nil];
// temporaryRecFile = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByDeletingLastPathComponent]];
[recorder record];
recordTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateTimeInterval) userInfo:nil repeats:YES];
}
[sender setSelected:true];
[discardBtn setHidden:false];
[saveBtn setHidden:false];
}
}
我有一个按钮第一次点击录制开始和2点击相同的按钮录制停止,第三次点击相同的按钮我想播放相同的声音,但我没有得到所需的文件。
temporaryRecFile = [prefs URLForKey:@"Test1"];
它给出零值? 如果我以错误的方式编码,请为我提供一些建议吗? 任何答案将不胜感激 提前致谢
答案 0 :(得分:0)
有时您无法依靠iOS
来保存数据,您需要通过同步来强制执行此操作。
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setURL:url forKey:@"test1"];
[prefs synchronize]; //this will ensure it is written to the disk
我可以给你的一个建议是分割那个按钮功能,你在这个IBAction中做得太多了(记录,停止记录和游戏)。也许最好有两个按钮recordOrStop()
和playOrStop()
,但这只是我的观点,因为它可以避免问题。