我正在研究基于VoIP的
IOS应用程序.
有两种方法可以播放一些声音,以便在来电时通知用户:
>发送带声音的UILocalNotification.声音将持续最多30秒;
>在setKeepAliveTimeout:handler:function中播放本地音乐资产.但系统只给我10秒钟的时间来完成操作.
有没有办法像本机手机应用程序一样永远播放声音?
最佳答案 我害怕@Dan2552是正确的.
这是Apple 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];