在我的应用中,我写了记录
NSArray *filePaths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *recordingDirectory = [filePaths objectAtIndex: 0];
NSString *resourcePath = [recordingDirectory stringByAppendingString:@"/sound.caf"];
self.soundFileURL = [NSURL fileURLWithPath:resourcePath];
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
audioSession.delegate = self;
[audioSession setActive: YES error: nil];
[[AVAudioSession sharedInstance] setCategory : AVAudioSessionCategoryRecorderror: nil];
NSDictionary *recordSettings =
[[NSDictionary alloc] initWithObjectsAndKeys:
[NSNumber numberWithFloat: 44100.0], AVSampleRateKey,
[NSNumber numberWithInt: kAudioFormatAppleLossless], AVFormatIDKey,
[NSNumber numberWithInt: 1], AVNumberOfChannelsKey,
[NSNumber numberWithInt: AVAudioQualityMax], AVEncoderAudioQualityKey,
nil];
AVAudioRecorder *newRecorder = [[AVAudioRecorder alloc] initWithURL: soundFileURL settings: recordSettings error: nil];
[recordSettings release];
self.soundRecorder = newRecorder;
[newRecorder release];
soundRecorder.delegate = self;
[soundRecorder prepareToRecord];
[soundRecorder record];
recording = YES;
我写停止录音
[soundRecorder stop];
recording = NO;
self.soundRecorder = nil;
我为播放按钮写
AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL: self.soundFileURL error: nil];
[fileURL release];
self.player = newPlayer;
[newPlayer release];
[player prepareToPlay];
[player setDelegate: self];
[button setTitle : @"Pause"forState: UIControlStateHighlighted];
[button setTitle : @"Pause"forState: UIControlStateNormal];
[player play];
在iPhone模拟器中一切正常
我记录然后停止播放,一切正常。
但在我的iPhone设备上没有声音。 任何帮助都会非常感激
答案 0 :(得分:2)
我有类似的问题。然后我设置AudioSession类别进行播放:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
希望这有帮助。