当应用程序处于前台状态时,如何在收到推送通知时播放声音。
我的代码:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
if (application.applicationState == UIApplicationStateActive)
{
//create an audio player and play the sound
NSString *path = [[NSBundle mainBundle] pathForResource:@"whistle" ofType:@"caf"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: path];
AVAudioPlayer *theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:NULL];
theAudio.volume = 1.0;
[theAudio prepareToPlay];
[theAudio play];
}
NSLog(@"didReceiveRemoteNotification %@",userInfo);
}
最佳答案
设置 AVPLayer
,您应该尝试使用以下代码:
添加以下框架
#include <AudioToolbox/AudioToolbox.h>
并使用此代码,
SystemSoundID soundID;
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef ref = CFBundleCopyResourceURL(mainBundle, (CFStringRef)@"mySoundName.wav", NULL, NULL);
AudioServicesCreateSystemSoundID(ref, &soundID);
AudioServicesPlaySystemSound(soundID);
关于当应用程序在前台时收到推送通知时的 IOS 播放声音,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24884435/