我一直對(duì)音頻開(kāi)發(fā)還是比較有興趣的,所以想深入了解一下,在iOS中最重要的框架還是AVFoundation。
從AVSpeechSythesisVoice開(kāi)始,先來(lái)幾行代碼,可以從文本轉(zhuǎn)化為語(yǔ)音。
AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc] initWithString:@"hello world,please attention http://blog.csdn.net/u011397277"];
utterance.rate = 0.4f;
// utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-US"];
utterance.pitchMultiplier = 0.8f;
utterance.postUtteranceDelay = 0.1f;
[self.synthesizer speakUtterance:utterance];
-(AVSpeechSynthesizer *)synthesizer
{
if (!_synthesizer) {
_synthesizer = [[AVSpeechSynthesizer alloc] init];
}
return _synthesizer;
}
是不是很神奇,就幾行代碼實(shí)現(xiàn)了,需要導(dǎo)入頭文件
#import <AVFoundation/AVFoundation.h>
如果大家有興趣,可以查看這些源碼,對(duì)音頻的學(xué)習(xí)有很大的幫助。
github地址:https://github.com/tapharmonic/Learning-AV-Foundation
我在以前的工作中,遇到過(guò)這樣的問(wèn)題。應(yīng)用正在播放音樂(lè),當(dāng)電話鈴聲響起,手機(jī)被靜音,點(diǎn)擊手機(jī)的鎖屏鍵,插上耳機(jī)線,我們的應(yīng)用應(yīng)該如何處理?
問(wèn)題一
處理靜音的問(wèn)題:如果在開(kāi)發(fā)中手機(jī)調(diào)制"鈴聲/靜音",聲音在兩種狀態(tài)下切換,而我們希望的是不讓聲音消失,我們可以嘗試配置音頻回話,加入以下的代碼
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
AVAudioSession *session = [AVAudioSession sharedInstance];
NSError *error;
if (![session setCategory:AVAudioSessionCategoryPlayback error:&error]) {
NSLog(@"Category Error: %@", [error localizedDescription]);
}
if (![session setActive:YES error:&error]) {
NSLog(@"Activation Error: %@", [error localizedDescription]);
}
return YES;
}
問(wèn)題二
處理手機(jī)鎖屏的問(wèn)題:我試過(guò)QQ音樂(lè),酷狗音樂(lè),天天動(dòng)聽(tīng),如果你鎖屏,音樂(lè)還是會(huì)繼續(xù)播放的,但是你的手機(jī)應(yīng)用有可能不能繼續(xù)播放聲音,可以嘗試使用下面的方法解決,配置info.plist文件
添加箭頭的屬性。
問(wèn)題三
處理中斷的問(wèn)題,比如電話呼入、鬧鈴響起等,我曾經(jīng)處理過(guò)網(wǎng)絡(luò)的通話問(wèn)題,當(dāng)時(shí)我開(kāi)發(fā)的一款能夠使用流量,WIFI通話的app,當(dāng)電話呼入時(shí),這個(gè)怎么處理,當(dāng)電話呼入時(shí),手機(jī)是斷網(wǎng)的,我們測(cè)試過(guò),但是你連接WIFI這個(gè)是沒(méi)有影響的。我們當(dāng)時(shí)是加了一個(gè)通知處理的。經(jīng)過(guò)測(cè)試,當(dāng)電話呼入的時(shí)候,系統(tǒng)的本身是能夠處理這個(gè)問(wèn)題的,但是當(dāng)對(duì)方終止電話的時(shí)候,音頻是不會(huì)如預(yù)期的恢復(fù),面對(duì)這樣的問(wèn)題應(yīng)該如何解決。
這種情況我們可以添加通知,在你的應(yīng)用的播放通知類里面,可以添加以下的幾行代碼。
NSNotificationCenter *nsnc = [NSNotificationCenter defaultCenter];
[nsnc addObserver:self
selector:@selector(handleRouteChange:)
name:AVAudioSessionRouteChangeNotification
object:[AVAudioSession sharedInstance]];
然后在處理函數(shù)handleRouteChange:處理問(wèn)題。
我們要分析notification.userInfo的信息,可以參考下面的代碼。
-(void)handleInterruption:(NSNotification *)notification
{
NSDictionary *info = notification.userInfo;
AVAudioSessionInterruptionType type = [info[AVAudioSessionInterruptionTypeKey] unsignedIntegerValue];
if(type == AVAudioSessionInterruptionTypeBegan){
//處理問(wèn)題
}else{
//處理問(wèn)題
}
}
問(wèn)題四
處理線路改變的問(wèn)題,當(dāng)你插上耳機(jī)時(shí),我們希望聲音從耳機(jī)內(nèi)傳出,但我們拔掉耳機(jī)的時(shí)候,我們希望的是音樂(lè)停止播放,是的,stop Play。 同樣,我們可以添加一個(gè)通知處理??梢詤⒖枷旅娴拇a處理:
NSNotificationCenter *nsnc = [NSNotificationCenter defaultCenter];
[nsnc addObserver:self
selector:@selector(handleRouteChange:)
name:AVAudioSessionRouteChangeNotification
object:[AVAudioSession sharedInstance]];
下面是處理函數(shù)
- (void)handleRouteChange:(NSNotification *)notification {
NSDictionary *info = notification.userInfo;
AVAudioSessionRouteChangeReason reason =
[info[AVAudioSessionRouteChangeReasonKey] unsignedIntValue];
if (reason == AVAudioSessionRouteChangeReasonOldDeviceUnavailable) {
AVAudioSessionRouteDescription *previousRoute =
info[AVAudioSessionRouteChangePreviousRouteKey];
AVAudioSessionPortDescription *previousOutput = previousRoute.outputs[0];
NSString *portType = previousOutput.portType;
if ([portType isEqualToString:AVAudioSessionPortHeadphones]) {
[self stop];
[self.delegate playbackStopped];
}
}
}
以上就是一些處理音頻開(kāi)發(fā)一些問(wèn)題處理解決方法,這個(gè)音頻開(kāi)發(fā)內(nèi)容還是比較多的,要分很多情況,進(jìn)行處理,本文還是比較淺的幾個(gè)問(wèn)題。
轉(zhuǎn)自:http://blog.csdn.net/u011397277/article/details/52298457