繁体   English   中英

当应用程序在前台时,IOS Play Sound接收推送通知

[英]IOS Play Sound on receiving push notification when app in foreground

如何在应用程序处于前台状态时接收推送通知时播放声音。

我的代码:

- (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 AVPLayer ,您应该尝试使用以下代码:

添加以下框架

#include <AudioToolbox/AudioToolbox.h>

并使用此代码,

    SystemSoundID soundID;
    CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef ref = CFBundleCopyResourceURL(mainBundle, (CFStringRef)@"mySoundName.wav", NULL, NULL);
    AudioServicesCreateSystemSoundID(ref, &soundID);
    AudioServicesPlaySystemSound(soundID);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM