我已经实现了以 m4a 格式录制音频文件的代码。它在 ipod 中运行良好,但在 iphone 4s 中无法播放。
我的代码如下:
-(IBAction)recordAudio:(id)sender
{
NSDictionary * recordSetting = [[NSMutableDictionary alloc] init];
[recordSetting setValue:[NSNumber numberWithInt:kAudioFormatAppleLossless] 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];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *task = [[NSUserDefaults standardUserDefaults]valueForKey:@"accidenttask"];
recordPath= [[NSString stringWithFormat:@"%@/%d.m4a",DOCUMENTS_FOLDER,a_id]retain];
url=[NSURL fileURLWithPath:recordPath];
NSError *err = nil;
if([recorder isRecording])
{
[recorder stop];
recorder = nil;
}
recorder = [[ AVAudioRecorder alloc] initWithURL:url settings:recordSetting error:&err];
if(err)
NSLog(@"ERROR : %@", err);
BOOL st = [recorder prepareToRecord];
if(!st){
NSLog(@"Failed");
}
recorder.meteringEnabled = YES;
BOOL status = [recorder record];
if(!status)
NSLog(@"Failed");
}
-(IBAction)stop:(id)sender
{
[stopButton setHidden:YES];
[playButton setHidden:NO];
[recordButton setHidden:NO];
[recorder stop];
}
-(IBAction)playAudio:(id)sender
{
[recorder stop];
if (!recorder.recording)
{
NSError *error;
recordPath= [[NSString stringWithFormat:@"%@/%d.m4a",DOCUMENTS_FOLDER,a_id]retain];
NSString *filename=[[filearry objectAtIndex:0]valueForKey:@"audiofile_path"];
url=[ NSURL fileURLWithPath:filename];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:filename] error:nil];
[audioPlayer setVolume:1.0];
[audioPlayer prepareToPlay];
[audioPlayer play];
}
}
可能是什么问题?