【问题标题】:How to play notification sound when app goes in background?应用程序进入后台时如何播放通知声音?
【发布时间】:2015-10-23 13:28:24
【问题描述】:
-(void)loadAudioFileList
{
   audioFileList = [[NSMutableArray alloc] init];
   arrAudioList  = [[NSMutableArray alloc] init];

   NSFileManager *fileManager = [[NSFileManager alloc] init];
   NSURL *directoryURL = [NSURL URLWithString:@"/System/Library/Audio/UISounds"];
   NSArray *keys = [NSArray arrayWithObject:NSURLIsDirectoryKey];

   NSDirectoryEnumerator *enumerator = [fileManager
                                     enumeratorAtURL:directoryURL
                                     includingPropertiesForKeys:keys
                                     options:0
                                     errorHandler:^(NSURL *url, NSError *error) {
                                         // Handle the error.
                                         // Return YES if the enumeration should continue after the error.
                                         return YES;
                                     }];

   for (NSURL *url in enumerator)
   {
       NSError *error;
       NSNumber *isDirectory = nil;
       if (! [url getResourceValue:&isDirectory forKey:NSURLIsDirectoryKey error:&error])
       {
          // handle error
       }
       else if (! [isDirectory boolValue])
       {
          [audioFileList addObject:url];
          [arrAudioList addObject:[url lastPathComponent]];
       }
   }
}

我使用上述代码获取可用的系统音频文件。当应用程序在前台时,我可以使用下面的代码播放通知声音。

SystemSoundID soundID;
AudioServicesCreateSystemSoundID((__bridge_retained   CFURLRef)fileURL,&soundID);
AudioServicesPlaySystemSound(soundID);

但如果应用程序在后台,我将无法播放声音。我用谷歌搜索解决方案,但我没有找到任何解决方案。据我所知,如果我们将声音文件包含到我们的应用程序包中,即使应用程序在后台,我们也可以播放通知声音,但使用系统声音我们无法实现相同的效果。 任何帮助将不胜感激。

【问题讨论】:

    标签: ios objective-c audio avplayer


    【解决方案1】:

    您需要在启用音频服务的情况下启用后台模式。如下图所示

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-11
      • 1970-01-01
      • 1970-01-01
      • 2017-05-24
      • 2018-02-04
      • 2013-04-20
      • 1970-01-01
      • 2020-10-03
      相关资源
      最近更新 更多