我面临的问题是玩耍。当我记录存储在文档目录中的文件格式时,即使是正在进行记录的大小。当我的play
函数正在调用时,我将其转换为NSData
,但它没有播放任何内容。
- (void)record{
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;
}
if(recorder)
{
if(recorder.recording)
[recorder stop];
[recorder release];
recorder = nil;
}
NSString *fileName = nil;
if(![myItem audioURL]){
fileName = [fileFormatter stringFromDate:[NSDate date]];
[myItem setAudioURL:fileName];
} else{
fileName = [myItem audioURL];}
NSString *pathString = [NSString stringWithFormat:@"%@/%@", [delegate applicationDocumentsDirectory], fileName];
NSDictionary *recordSettings = [[NSDictionary alloc] initWithObjectsAndKeys:
[NSNumber numberWithFloat: 44100.0],AVSampleRateKey,
[NSNumber numberWithInt: kAudioFormatAppleIMA4],AVFormatIDKey,
[NSNumber numberWithInt: 1], AVNumberOfChannelsKey,
[NSNumber numberWithInt: AVAudioQualityMax],AVEncoderAudioQualityKey,nil];
recorder = [[AVAudioRecorder alloc] initWithURL: [NSURL fileURLWithPath:pathString] settings: recordSettings error: nil];
recorder.delegate = self;
if ([recorder prepareToRecord] == YES){
[recorder record];
recording = YES;
[pauseButton setImage:[UIImage imageNamed:@"stop.png"] forState:UIControlStateNormal];
[pauseButton setEnabled:YES];
[playButton setEnabled:NO];
[recordButton setEnabled:NO];
[self beginAnimation];
// NSError *error=nil;
// if(![context save: &error])
// {
// //Couldn't save
// }
}
}
- (void)play{
/* [self silenced];
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,
sizeof (audioRouteOverride),&audioRouteOverride);
UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory);
*/
if(pausedPlayback){
[player play];
playing = YES;
pausedPlayback = NO;
[pauseButton setEnabled:YES];
[playButton setEnabled:NO];
[self beginAnimation];
return;
}
NSString *fileName = nil;
if(![myItem audioURL]){
return;
} else
fileName = [myItem audioURL];
NSLog(@"file name :%@",fileName);
// NSError *error;
NSString *pathString = [NSString stringWithFormat:@"%@/%@", [delegate applicationDocumentsDirectory], fileName];
NSLog(@"Path : %@",pathString);
// player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL URLWithString:pathString] error:nil] ;
NSData *soundData = [NSData dataWithContentsOfURL:[NSURL URLWithString:pathString]];
player = [[AVAudioPlayer alloc] initWithData:soundData error: nil];
player.delegate = self;
player.volume=1.0;
// Play the audio
[player prepareToPlay];
[player play];
playing = YES;
[recordButton setEnabled:NO];
[pauseButton setEnabled:YES];
[playButton setEnabled:NO];
// if (player==nil) {
// NSLog(@"%@",[error description]);
// }
// else
// {
/* player.volume=1.0;
NSLog(@"about to play");
[player prepareToPlay];
[player play];
NSLog(@"player play");
[player setDelegate:self];
playing = YES;
[recordButton setEnabled:NO];
[pauseButton setEnabled:YES];
[playButton setEnabled:NO];*/
[self beginAnimation];
// }
}
- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)_recorder successfully:(BOOL)flag{
NSLog (@"audioRecorderDidFinishRecording:successfully:");
[recorder release];
recorder = nil;
}
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)_player successfully:(BOOL)flag{
if(![player isPlaying])
{
[player stop];
}
[player release];
player = nil;
playing = NO;
[playButton setEnabled:YES];
[pauseButton setEnabled:NO];
[recordButton setEnabled:YES];
[self stopAnimation];
}