4

iPod ライブラリから音楽を再生するアプリを開発中です。選択した項目をテーブルから取得し、詳細ビュー コントローラーに渡すことで、MPMediaPlayerController を介して音楽を再生しています。

MPMediaItem *item = (MPMediaItem *)self.detailItem;
MPMediaItemCollection *collection = [[MPMediaItemCollection alloc] initWithItems:@[item]];
[self.musicPlayer setQueueWithItemCollection:collection];
[self.musicPlayer play];

音楽の再生を開始します。バックグラウンドでの使用を有効にするために、Info.plist に次の値を設定しました。

UIBackgroundModes
 >Item 0 - audio

そして、それは機能します。アプリを閉じると、音楽が再生され続けます。だから今、コントロールセンターのオーディオコントロールを取得してアプリにメッセージを送信しようとしているので、読んだ後、いくつかのことをする必要があることがわかりました。そこで、UIResponder のサブクラスを作成し、次の行を追加しました。

- (BOOL)canBecomeFirstResponder {
    return YES;
}
- (void)remoteControlReceivedWithEvent:(UIEvent *)event {
    NSLog(@"CustomApp:remoteControlReceivedWithEvent:%@", event.description);
}

私は自分の AppDelegate をカスタム UIResponder のサブクラスにしました:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    self.window = [[MainWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    self.mainViewController = [[BrowserViewController alloc] initWithNibName:nil bundle:nil];
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:self.mainViewController];
    self.window.rootViewController = navigationController;
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];

    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    [self becomeFirstResponder];

    return YES;
}

この

- (void)applicationDidEnterBackground:(UIApplication *)application {
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    [self becomeFirstResponder];
}

さて、私がここにいる理由は、これがシミュレーターでは機能するがデバイスでは機能しないためであり、その理由がわかりません。シミュレーターでこれを起動し、コントロール センターを起動してオーディオ コントロールを押し始めると、カスタム UIResponder の NSLog がデバッガーに表示されますが、デバイスには表示されません。実際に何が起こるかというと、再生/一時停止ボタンは何もしません。次に、次または前のボタンを押すと、iPod アプリの次または前のトラックに移動し、その再生が開始されます。

この方程式には小さな何かが欠けているようですが、私にはわかりません。ドキュメントをできる限り検索しましたが、この状況に関連するものは何も見つかりませんでした。この特定の機能に関するドキュメントは非常に限られているようです。

4

3 に答える 3

1

この回答が古いことは知っていますが、iOS 10 でも同じ問題に悩まされていました。私のシミュレーターではロック画面のオーディオ コントロールが正しく表示されましたが、実際のデバイスでは表示されませんでした。私にとっての修正は、AVAudioSession のカテゴリを設定するときに、オプション AVAudioSessionCategoryOptionMixWithOthers を割り当てていないことを確認することでした。

これは、デバイスがロック画面のオーディオ コントロールを表示しないようにするのに十分でした。したがって、最終的に私の解決策は次のとおりです。

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker|AVAudioSessionCategoryOptionAllowAirPlay|AVAudioSessionCategoryOptionAllowBluetooth|AVAudioSessionCategoryOptionAllowBluetoothA2DP error:&error];
于 2016-10-07T22:42:46.173 に答える
0

あなたの rootViewController で:

- (void)viewDidLoad:(BOOL)animated
{
    [super viewDidAppear:animated];

    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    NSError *setCategoryError = nil;
    BOOL success = [audioSession setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker error:&setCategoryError];
    if (success) {
        NSError *activationError = nil;
        success = [audioSession setActive:YES error:&activationError];
        if (!success) {
            NSLog(@"%@", activationError);
        }
    }
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    [self becomeFirstResponder];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];

    [[UIApplication sharedApplication] endReceivingRemoteControlEvents];
    [self resignFirstResponder];
}

- (BOOL)canBecomeFirstResponder
{
    return YES;
}

- (void)remoteControlReceivedWithEvent:(UIEvent *)event
{
    if (event.type == UIEventTypeRemoteControl) {
        NSLog(@"Remote control event %i subtype %i", event.type, event.subtype);

        // example for headphones
        switch (event.subtype) {
            case UIEventSubtypeRemoteControlPlay:
                break;
            case UIEventSubtypeRemoteControlPause:
                break;
            case UIEventSubtypeRemoteControlStop:
                break;
            case UIEventSubtypeRemoteControlTogglePlayPause:
                break;
            case UIEventSubtypeRemoteControlNextTrack:
                break;
            case UIEventSubtypeRemoteControlPreviousTrack:
                break;
            case UIEventSubtypeRemoteControlEndSeekingBackward:
                break;
            case UIEventSubtypeRemoteControlEndSeekingForward:
                break;

            default:
                break;
        }
    }
}
于 2014-12-17T17:44:37.080 に答える
0

viewDidLoad() ダニロの答えでこのコードを試してください

-(void)viewDidLoad:(BOOL)animated 
{
    NSError *setCategoryErr = nil;
    NSError *activationErr  = nil;
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:0 error:&setCategoryErr];
    [[AVAudioSession sharedInstance] setActive: YES error: &activationErr];
}
于 2015-06-26T12:58:50.283 に答える