【问题标题】:handleWatchKitExtensionRequest play background audio on iphonehandleWatchKitExtensionRequest 在 iphone 上播放背景音频
【发布时间】:2015-04-30 17:26:48
【问题描述】:

我正在试验一个 WatchKit 应用程序,该应用程序将在 iphone 上触发音频警报。我已经设置好触发,每 5 秒重复一次。在 iphone 上,我有在后台播放短音频 wav 文件的逻辑。即使应用程序被最小化并播放音频,它也可以正常工作。我已经在 iphone 上测试了这个独立的,可以正常工作。

不起作用的步骤是手表上的触发器告诉父 Iphone 应用程序播放音频。它主要工作,除了 2 秒的音频警报可能被剪裁为 0.5 秒。它播放第一部分的啁啾声并被切断。每次我触发它时,它都会对整个 wav 声音进行这种削波的啁啾。

来自父应用的回复正常,我想知道一旦回复回来,所有后台处理是否都被完全切断。

我在应用程序中启用了音频的背景模式。

如何让手表触发应用在后台播放整个音频提醒?

// WatchApp InterfaceController 
- (void)pingIphone {
    [WKInterfaceController openParentApplication:@{@"requestString":@"findphone"} reply:^(NSDictionary *replyInfo, NSError *error) {
        NSLog(@"\nReply info: %@\nError: %@",replyInfo, error);
    }];
}

//  Iphone AppDelegate
- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void(^)(NSDictionary *replyInfo))reply {
    NSString * request = [userInfo objectForKey:@"requestString"];

    NSDictionary * replyContent = @{@"state":(application.applicationState == UIApplicationStateBackground ? @"back" : @"not back")};

    if ([request isEqualToString:@"findphone"]){
            ViewController *vc = [[ViewController alloc] init];
            [vc pingIphone];
    }

    reply(replyContent);
}

// Iphone ViewController
@property (strong, nonatomic)  AVQueuePlayer *player;

- (void)viewDidLoad {
    [super viewDidLoad];

    // get device's default audio level on start
    AudioSessionInitialize(NULL, NULL, NULL, NULL);
    AudioSessionSetActive(true);

    Float32 volume;
    UInt32 dataSize = sizeof(Float32);
    AudioSessionGetProperty (
                          kAudioSessionProperty_CurrentHardwareOutputVolume,
                         &dataSize,
                         &volume
                         );

    [[AVAudioSession sharedInstance] setActive:YES error:nil];
    [[AVAudioSession sharedInstance] addObserver:self forKeyPath:@"outputVolume" options:NSKeyValueObservingOptionNew context:nil];
}


(void)pingIphone {
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

    if([userDefaults boolForKey:@"vibrate_bool"] == YES){
        AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
    }

    for(int i = 0; i < 5; i++){
        [_player insertItem:[AVPlayerItem playerItemWithURL:[[NSBundle mainBundle] URLForResource:@"alarm" withExtension:@"wav"]] afterItem:nil];
    }

    //NSLog(@"volume: %f",[userDefaults floatForKey:@"volume"]);
    if (!(_player.rate > 0 && !_player.error)) {
        [self startAudio];
    }
}

- (void)startAudio {
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

    [[AVAudioSession sharedInstance] setDelegate: self];
    NSError *setCategoryError = nil;
    [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: &setCategoryError];

    NSString* path = [[NSBundle mainBundle] pathForResource:@"alarm"  ofType:@"wav"];

    _player = [[AVQueuePlayer alloc] init];

    for(int i = 0; i < 5; i++){
        [_player insertItem:[AVPlayerItem playerItemWithURL:[NSURL fileURLWithPath:path]] afterItem:nil];
    }

    _player.volume = [userDefaults floatForKey:@"volume"];
    [_player play];


    if (setCategoryError)
        NSLog(@"Error setting category! %@", setCategoryError);
}

【问题讨论】:

    标签: ios iphone audio background watchkit


    【解决方案1】:

    听起来您的应用正在被操作系统杀死。查看这个答案以获得一些建议:https://stackoverflow.com/a/29848521/3704092

    是的,如果你不使用后台任务,一旦你调用reply(),处理就会结束。

    【讨论】:

    • 我使用了后台任务,但它仍然在做同样的事情。我包括 [NSThread sleepForTimeInterval:2];在后台任务中的 _play 之后,它会停留足够长的时间来播放简短的警报。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-30
    • 2021-11-03
    • 1970-01-01
    • 2020-04-07
    • 2016-08-30
    相关资源
    最近更新 更多