bugfix:錄音的時候,webview不能播放視頻

現象:

使用webview顯示html,html中有播放視頻,同時設備正在錄音。

模擬器中沒有問題,錄音可以生成,視頻也可以播放。

但是真機中,錄音可以生成,但是視頻無法播放。

真機中報錯:

[0x3b05518c] Received corrupt data. Property list is  NULL
ERROR:     [0x3b05518c] AVAudioSessionUtilities.h:88: GetProperty_DefaultToZero: AudioSessionGetProperty ('tfac') failed with error: 'tahw'


原因:

根據報錯信息,找到一篇文章:

http://stackoverflow.com/questions/22222501/audiosessiongetproperty-tfac-failed-with-error-tahw

// Configure audio session
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];

其中提到了AVAudioSession這個類。

查看下項目中對應的代碼,由於錄音的時候,沒有考慮播放的問題,所以項目中直接使用的AVAudioSessionCategoryRecord

AVAudioSession *session = [AVAudioSession sharedInstance];
NSError *sessionError;
[session setCategory:AVAudioSessionCategoryRecord error:&sessionError];


這就是導致錄音時,不能播放視頻的原因了。

解決辦法:

查看AVAudioSessionCategory其他的值:

#pragma mark -- Values for the category property --

/*  Use this category for background sounds such as rain, car engine noise, etc.  
 Mixes with other music. */
AVF_EXPORT NSString *const AVAudioSessionCategoryAmbient;
	
/*  Use this category for background sounds.  Other music will stop playing. */
AVF_EXPORT NSString *const AVAudioSessionCategorySoloAmbient;

/* Use this category for music tracks.*/
AVF_EXPORT NSString *const AVAudioSessionCategoryPlayback;

/*  Use this category when recording audio. */
AVF_EXPORT NSString *const AVAudioSessionCategoryRecord;

/*  Use this category when recording and playing back audio. */
AVF_EXPORT NSString *const AVAudioSessionCategoryPlayAndRecord;

/*  Use this category when using a hardware codec or signal processor while
 not playing or recording audio. */
AVF_EXPORT NSString *const AVAudioSessionCategoryAudioProcessing;

/*  Use this category to customize the usage of available audio accessories and built-in audio hardware.
 For example, this category provides an application with the ability to use an available USB output 
 and headphone output simultaneously for separate, distinct streams of audio data. Use of 
 this category by an application requires a more detailed knowledge of, and interaction with, 
 the capabilities of the available audio routes.  May be used for input, output, or both.
 Note that not all output types and output combinations are eligible for multi-route.  Input is limited
 to the last-in input port. Eligible inputs consist of the following:
	AVAudioSessionPortUSBAudio, AVAudioSessionPortHeadsetMic, and AVAudioSessionPortBuiltInMic.  
 Eligible outputs consist of the following: 
	AVAudioSessionPortUSBAudio, AVAudioSessionPortLineOut, AVAudioSessionPortHeadphones, AVAudioSessionPortHDMI, 
	and AVAudioSessionPortBuiltInSpeaker.  
 Note that AVAudioSessionPortBuiltInSpeaker is only allowed to be used when there are no other eligible 
 outputs connected.  */
AVF_EXPORT NSString *const AVAudioSessionCategoryMultiRoute NS_AVAILABLE_IOS(6_0);

根據需要同時錄音、播放視頻的需求,改爲:

AVAudioSession *session = [AVAudioSession sharedInstance];
NSError *sessionError;
[session setCategory:AVAudioSessionCategoryPlayAndRecord
         withOptions:AVAudioSessionCategoryOptionMixWithOthers
               error:&sessionError];

參考資料:

http://stackoverflow.com/questions/22222501/audiosessiongetproperty-tfac-failed-with-error-tahw

iOS AudioSession詳解


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章