【问题标题】:Play a background sound in WatchKit app在 WatchKit 应用中播放背景声音
【发布时间】:2015-05-14 22:23:12
【问题描述】:

我在 WatchKit 中有一个按钮,可以像这样向 iPhone 主应用发送通知。

-(IBAction) startSound
{
    //turn sound on
    NSString *requestString = [NSString stringWithFormat:@"startSound"]; // This string is arbitrary, just must match here and at the iPhone side of the implementation.
    NSDictionary *applicationData = [[NSDictionary alloc] initWithObjects:@[requestString] forKeys:@[@"startSound"]];

    [WKInterfaceController openParentApplication:applicationData reply:^(NSDictionary *replyInfo, NSError *error) {
        //NSLog(@"\nReply info: %@\nError: %@",replyInfo, error);
    }];
}

在我的 iPhone 应用程序委托中,我添加了以下代码。

- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void(^)(NSDictionary *replyInfo))reply
{
    NSLog(@"handleWatchKitExtensionRequest ...");

    NSMutableDictionary *mutDic = [[NSMutableDictionary alloc] init];

    //This block just asks the code put after it to be run in background for 10 mins max
    __block UIBackgroundTaskIdentifier bgTask;
    bgTask = [application beginBackgroundTaskWithName:@"MyTask" expirationHandler:^{
        bgTask = UIBackgroundTaskInvalid;
    }];

    NSString *request = [userInfo objectForKey:@"startSound"];
    if ([request isEqualToString:@"startSound"])
    {
        NSString *soundFilePath = [[NSBundle mainBundle] pathForResource: @"warning" ofType: @"mp3"];
        NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];

        myAudioPlayer1 = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
        myAudioPlayer1.numberOfLoops = -1; //inifinite
        [myAudioPlayer1 play];
    }

    reply(nil); //must reply with something no matter what

    //once code is all done and the reply has been sent only then end the bg-handler
    [application endBackgroundTask:bgTask];
}

然而,当我的应用程序进行 Apple 审核时,它被拒绝了,原因是我的应用程序必须在前台运行才能使声音功能正常工作。我错过了什么?

10.6 - Apple 和我们的客户高度重视简单、精致、富有创意且经过深思熟虑的界面。他们需要做更多的工作,但 值得。苹果设定了很高的标准。如果您的用户界面很复杂或 不太好,可能会被拒

10.6 详情

我们仍然发现您的 Apple Watch 应用需要包含该应用 在 iPhone 的前台运行以播放警报器 声音,这提供了糟糕的用户体验。

后续步骤

请参阅 UIApplicationDelegate 协议参考来实现 此方法并使用它来响应来自 Apple Watch 的请求 应用程序。

因为这个方法很可能在你的应用处于 后台,调用 beginBackgroundTaskWithName:expirationHandler: 实现开始时的方法和 endBackgroundTask: 处理完回复并执行回复后的方法 堵塞。启动后台任务可确保您的应用不 在它有机会发送回复之前被暂停。

【问题讨论】:

    标签: ios watchkit apple-watch


    【解决方案1】:

    音频剪辑有多长?在您共享的代码中,reply() 似乎几乎会立即被调用,这不会让剪辑有机会播放。您应该延迟调用 reply() 直到您的音频剪辑完成。

    【讨论】:

      【解决方案2】:

      我在我的 WatchKit 应用中做了一些非常相似的事情。当用户点击一个按钮时,我会从我的 iPhone 应用程序中播放音频,而我的 iPhone 应用程序不需要在前台运行。我必须做两件事才能让它发挥作用。第一个是使用beginBackgroundTaskWithName 设置后台任务,我可以看到您正在这样做。第二个是在我的后台模式中启用后台音频功能。

      除了这两个之外,我不记得做过任何其他事情来让它工作。在我添加支持之前,我的应用中确实已经有背景音频工作,因此可以使用 MPNowPlayingInfoCenter 控制它。

      【讨论】:

      • 谢谢,但苹果拒绝了其他手表应用之一,称背景音频不适合用于此目的
      • 正确。短片不需要背景音频模式。
      • 感谢@bgiham 提供的信息,我对短片一无所知。在我的应用中,用户可能会播放 30 分钟的音频,因此 Apple 没有说明背景音频不适合它。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-26
      • 2013-07-05
      • 1970-01-01
      相关资源
      最近更新 更多