我正在开发基于 VoIP 的 IOS 应用。
有两种方法可以在来电时播放一些声音来通知用户:
- 发送带有声音的
UILocalNotification
。声音最多持续 30 秒; - 在
setKeepAliveTimeout:handler:
函数中播放本地音乐 Assets 。但是系统只给了我10秒的操作时间。
有什么方法可以像原生电话应用一样永远播放声音吗?
最佳答案
恐怕@Dan2552 是正确的。
这是苹果 states :
Sounds that last longer than 30 seconds are not supported. If you specify a file with a sound that plays over 30 seconds, the default sound is played instead.
编辑:
使用 AVFoundation
中的 AVAudioPlayer
可以播放音频文件超过 30 秒(或永远播放)
@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 属性:
[self performSelectorOnMainThread:@selector(playAudio) withObject:nil waitUntilDone:NO];
关于ios - 有没有办法让 VoIP 应用程序中的来电永远响铃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19517460/