我正在尝试编写一个允许我录制消息的应用,然后让用户回放他们录制的内容,但由于某些原因,此代码无法正常工作。当调用playAction方法时,我听到的只有空气。有任何想法吗?
- (void) playAction
{
NSLog(@"PlaY action");
NSError *error;
AVAudioPlayer *player = [[AVAudioPlayer alloc]
initWithContentsOfURL:[NSURL fileURLWithPath:recorderFilePath] error:&error];
// AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithData:self.audioData error:&error];
[player prepareToPlay];
[player play];
}
- (void) startRecording
{
[self.timer fire];
self.recordButton.userInteractionEnabled = NO;
self.stopButton.hidden = NO;
self.playButton.hidden = YES;
self.counterLabel.hidden = NO;
self.viewTitle.hidden = YES;
self.shortBlurb.hidden = YES;
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError *err = nil;
[audioSession setCategory :AVAudioSessionCategoryPlayAndRecord error:&err];
if(err){
NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
return;
}
[audioSession setActive:YES error:&err];
err = nil;
if(err){
NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
return;
}
NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc] init];
[recordSetting setValue :[NSNumber numberWithInt:kAudioFormatLinearPCM] forKey:AVFormatIDKey];
[recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
[recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey];
[recordSetting setValue :[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
[recordSetting setValue :[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey];
[recordSetting setValue :[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsFloatKey];
// Create a new dated file
NSDate *now = [NSDate dateWithTimeIntervalSinceNow:0];
NSString *caldate = [now description];
recorderFilePath = [NSString stringWithFormat:@"%@/%@.caf", DOCUMENTS_FOLDER, caldate];
NSURL *url = [NSURL fileURLWithPath:recorderFilePath];
err = nil;
recorder = [[ AVAudioRecorder alloc] initWithURL:url settings:recordSetting error:&err];
if(!recorder){
NSLog(@"recorder: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
UIAlertView *alert =
[[UIAlertView alloc] initWithTitle: @"Warning"
message: [err localizedDescription]
delegate: nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
return;
}
//prepare to record
[recorder setDelegate:self];
[recorder prepareToRecord];
recorder.meteringEnabled = YES;
BOOL audioHWAvailable = audioSession.inputAvailable;
if (! audioHWAvailable) {
UIAlertView *cantRecordAlert =
[[UIAlertView alloc] initWithTitle: @"Warning"
message: @"Audio input hardware not available"
delegate: nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[cantRecordAlert show];
return;
}
// start recording
[recorder recordForDuration:(NSTimeInterval) 7];
}
- (void) stopRecording{
self.counterLabel.hidden = YES;
self.recordButton.userInteractionEnabled = YES;
self.viewTitle.hidden = NO;
self.shortBlurb.hidden = NO;
self.recordButton.hidden = NO;
self.playButton.hidden = NO;
self.stopButton.hidden = YES;
[recorder stop];
NSURL *url = [NSURL fileURLWithPath: recorderFilePath];
NSError *err = nil;
self.audioData = [NSData dataWithContentsOfFile:[url path] options: 0 error:&err];
if(!audioData)
{
NSLog(@"audio data: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
}
//[editedObject setValue:[NSData dataWithContentsOfURL:url] forKey:editedFieldKey];
//[recorder deleteRecording];
NSFileManager *fm = [NSFileManager defaultManager];
err = nil;
//[fm removeItemAtPath:[url path] error:&err];
if(err)
NSLog(@"File Manager: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
}
- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *) aRecorder successfully:(BOOL)flag
{
NSLog (@"audioRecorderDidFinishRecording:successfully:");
// your actions here
}
答案 0 :(得分:1)
对我来说很好,除了:
尝试更改此内容:
AVAudioPlayer *player = [[AVAudioPlayer alloc]...
为:
@property(nonatomic, strong) AVAudioPlayer *player;
self.player = [[AVAudioPlayer alloc].....
希望这有帮助。