【问题标题】:Stop another app's background audio on iOS在 iOS 上停止另一个应用的背景音频
【发布时间】:2018-09-14 03:14:03
【问题描述】:

如何停止来自其他应用的背景音频?

我试过了

NSError *error;  
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];  
[[AVAudioSession sharedInstance] setActive:YES error:&error];

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)options- (void)applicationWillEnterForeground:(UIApplication *) application 中,但没有效果。

我的应用使用 The Amazing Audio Engine 播放音频,但本身不需要背景音频,因此我不想将“音频”键添加到 info.plist 中的“所需设备功能”

还有其他解决方案吗?

【问题讨论】:

    标签: ios audio background


    【解决方案1】:

    在他的论坛上获得了answer from Michael Tyson of The Amazing Audio Engine

    UInt32 allowMixing = NO;
    AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof (allowMixing), &allowMixing);
    

    或者如果您使用的是 The Amazing Audio Engine:
    audioController.allowMixingWithOtherApps = NO;

    (根据您使用的音频会话,默认情况下这似乎应该设置为 NO,但神奇的音频引擎并非如此)

    【讨论】:

      【解决方案2】:

      [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategorySoloAmbient error:nil];解决问题

      【讨论】:

      • 虽然此代码可能会回答问题,但提供有关此代码为何和/或如何回答问题的额外上下文可提高其长期价值。
      【解决方案3】:

      我在我的 iOS 应用程序中遇到了类似的问题,并通过“Bamsworld 的解决方案”解决了这个问题。详情请查看link

      解决办法: 在你的 appDelegate 中试试这个:

      - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
      {
      
          // set audio session category AVAudioSessionCategoryPlayAndRecord with no options
          success = [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayAndRecord error:nil];
      if (!success) {
              NSLog(@"setCategoryError");
          }
      
          // set audio session mode to default
          success = [[AVAudioSession sharedInstance] setMode:AVAudioSessionModeDefault error:nil];
          if (!success) {
              NSLog(@"setModeError");
          }
      
          // activate audio session
          success = [[AVAudioSession sharedInstance] setActive:YES error:nil];
          if (!success) {
              NSLog(@"activationError");
          }
          // ... etc.
      }
      

      【讨论】:

      • 对不起,出于对提供解决方案的好奇,我提供了链接。
      猜你喜欢
      • 2015-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多