我正在開發基於VoIP的IOS應用程序。有沒有辦法在VoIP應用程序來電時永遠持續響鈴?
有玩一些聲音來通知用戶兩種方式,當一個電話進來:
- 發送
UILocalNotification
有聲錄像。聲音將持續最多30秒; - 播放本地音樂資產
setKeepAliveTimeout:handler:
函數。但系統只給了我10秒鐘來完成操作。
有沒有什麼方法可以像原生Phone應用一樣永遠播放聲音?
我正在開發基於VoIP的IOS應用程序。有沒有辦法在VoIP應用程序來電時永遠持續響鈴?
有玩一些聲音來通知用戶兩種方式,當一個電話進來:
UILocalNotification
有聲錄像。聲音將持續最多30秒;setKeepAliveTimeout:handler:
函數。但系統只給了我10秒鐘來完成操作。有沒有什麼方法可以像原生Phone應用一樣永遠播放聲音?
恐怕@ Dan2552是正確的。
這裏是蘋果states:
那麼持續時間大於30秒聲音不被支持。如果 指定播放時間超過30秒的文件,則會播放默認的 聲音。
編輯:
播放音頻文件超過30秒(或永遠,對於緣故)可以通過使用AVAudioPlayer
從AVFoundation
@import AVFoundation; // module -> no need to link the framework
// #import <AVFoundation/AVFoundation.h> // old style
- (void)playAudio
{
NSString *path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"mp3"];
NSError *error = nil;
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&error];
if (!error) {
player.numberOfLoops = -1; // infinite loop
[player play];
} else {
NSLog(@"Audio player init error: %@", error.localizedDescription);
}
}
然後實現而不是設置本地通知的soundName屬性,您必須必須調用此方法d的主線程上:
[self performSelectorOnMainThread:@selector(playAudio) withObject:nil waitUntilDone:NO];
伊斯蘭教問的回答的問題是,AVAudioPlayer不會爲通知工作,因爲該應用程序會當你不使用的應用程序靜音。只有UILocalNotification可以讓您的應用程序在不活動時發出聲音。 30秒的限制是真實的。應該通過以另外30秒通知重複通知來完成。我不知道其他VOIP應用會持續多久,但即使使用傳統電話,通常也會有限制,例如一分鐘。
我的策略是:
後打電話第三通知這將允許時間的用戶1:40迴應電話。
您的代碼將是這個樣子:
UILocalNotification* notification = [[UILocalNotification alloc] init];
notification.alertBody = NSLocalizedString(@"FIRST_CALL", @"%@ is calling you!");
notification.soundName = @"normal_ring.mp3";
[[UIApplication sharedApplication] presentLocalNotificationNow:notification];
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(40 * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
if (!phone.isRinging)
{
// Somebody picked it up already in the mean time
return;
}
UILocalNotification* notification = [[UILocalNotification alloc] init];
notification.alertBody = NSLocalizedString(@"HURRY_CALL", @"Hurry, you are about to miss a call from %@!");
notification.soundName = @"hurry_ring.mp3";
[[UIApplication sharedApplication] presentLocalNotificationNow:notification];
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
if (!phone.isRinging)
{
// Somebody picked it up already in the mean time
return;
}
UILocalNotification* notification = [[UILocalNotification alloc] init];
notification.alertBody = NSLocalizedString(@"MISSED_CALL", @"You've just missed a call from %@");
notification.soundName = @"missed_call.mp3";
[[UIApplication sharedApplication] presentLocalNotificationNow:notification];
});
});
如果您使用的代碼,請記住,您首先需要確保應用程序在後臺運行,因此UILocalNotification將是你唯一的選擇。在前臺使用普通的AVAudioPlayer時,要靈活得多。第二件事是,當用戶響應呼叫時,應用程序變爲前景,因此應該將警報靜音並讓AVAudioPlayer接管。
您希望或多或少地在警報和應用內聲音之間平滑過渡,以便爲用戶提供完美的體驗,因此最好的方法是測量鈴聲之間的時間並讓AVAudioPlayer以精確的在新循環開始時的正確時間。
第二個問題是讓應用程序在後臺保持活動狀態。這個簡單的代碼將會不時地輪詢應用程序,以保持它的活力。它從工作應用,不只是這個,這個代碼投票如果計時器仍在運行,睡五秒鐘,當它來直:
UIBackgroundTaskIdentifier backgroundTask = [application beginBackgroundTaskWithExpirationHandler:^{
}];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[timerExecuted timerUpdate];
while (!timerExecuted.isStopped)
{
[NSThread sleepForTimeInterval:5];
}
[application endBackgroundTask:backgroundTask];
});
鑑於Skype的起着〜25第二聲,我認爲這是比較節省假設30秒是限制。 – Dan2552