当应用程序处于前台状态时

当应用程序处于前台状态时

本文介绍了当应用程序处于前台状态时,在接收到推送通知时,iOS Play的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How to play sound on receiving a push notification when the app is in the foreground state.

我的代码:

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

Instated of AVPLayer, you should try with following code:

添加以下框架

#include <AudioToolbox/AudioToolbox.h>

并使用此代码,

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

这篇关于当应用程序处于前台状态时,在接收到推送通知时,iOS Play的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-15 10:35