我想在有人用iPhone的麥克風說話時播放音頻。此代碼在iPhone 2G(OS 3.1.3)中完美工作,但在iPhone 4中進行測試時,該代碼無效。我檢查了API和方法的差異,但找不到任何。iOS 4.1和iOS3.1.3中AVAudioRecorder行爲的差異
- (void)viewDidLoad {
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/Audio.mp3", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.delegate=self;
if (audioPlayer == nil)
NSLog(@" %@ ", [error description]);
audioSession = [[AVAudioSession sharedInstance] retain];
[audioSession setActive:YES error: nil];
[super viewDidLoad]; }
的動作事件:
-(IBAction) micAction{
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error: nil];
NSURL *url = [NSURL fileURLWithPath:@"/dev/null"];
NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithFloat: 44100.0], AVSampleRateKey,
[NSNumber numberWithInt: kAudioFormatAppleLossless], AVFormatIDKey,
[NSNumber numberWithInt: 1], AVNumberOfChannelsKey,
[NSNumber numberWithInt: AVAudioQualityMax], AVEncoderAudioQualityKey,
nil];
NSError *error;
recorder = [[AVAudioRecorder alloc] initWithURL:url settings:settings error:&error];
if (recorder) {
[recorder prepareToRecord];
recorder.meteringEnabled = YES;
[recorder record];
levelTimer = [NSTimer scheduledTimerWithTimeInterval: 0.03 target: self selector: @selector(levelTimerCallback:) userInfo: nil repeats: NO];
} }
- (void)levelTimerCallback:(NSTimer *)t {
[recorder updateMeters];
if([recorder averagePowerForChannel:0]>-38){
if ([audioPlayer prepareToPlay]) {
[recorder stop];
[recorder release];
[levelTimer invalidate];
[self playSound];
}
}}
-(void)playSound{
[audioSession setCategory:AVAudioSessionCategoryPlayback error: nil];
if ([audioPlayer prepareToPlay])
[audioPlayer play]; }
爲了確保聲音在iPhone 4玩,我還設置了一個IBAction爲事件,這是工作的罰款。此外,爲了測試,我會設置一個標籤來顯示值
[recorder averagePowerForChannel:0
按下按鈕後。該標籤根據2G中的計時器更新自身,但在iPhone 4中,它僅顯示單擊事件中的一個值。
我無法調試iPhone 4中該方法的行爲,因爲我現在沒有設備。
任何人都可以請指出錯誤是什麼?
謝謝
還沒有,我現在就去做。將發佈results.thanks一堆 – 2010-10-26 05:52:06
沒有運氣,它不工作。 – 2010-10-26 09:34:52