12

iOS 7 の音楽アプリで曲を再生する場合、ユーザーはスライダーを使用して、ロック画面/コントロール センターで曲の位置を変更できます。スライダーがアクティブです:

ここに画像の説明を入力

しかし、アプリのユーザーで音楽を再生するときはできません。スライダーがアクティブではありません:

ここに画像の説明を入力

アプリでこれらの機能を有効にするにはどうすればよいですか?

4

3 に答える 3

13

iOS 9.1 以降では、MPRemoteCommandCenter を使用してトラック位置を変更できます。

if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_9_0) {
            MPRemoteCommandCenter *commandCenter = [MPRemoteCommandCenter sharedCommandCenter];
            [commandCenter.changePlaybackPositionCommand setEnabled:true];
            [commandCenter.changePlaybackPositionCommand addTarget:self action:@selector(changedThumbSliderOnLockScreen:)];
        }

と方法

- (MPRemoteCommandHandlerStatus)changedThumbSliderOnLockScreen:(MPChangePlaybackPositionCommandEvent *)event
{
    // change position
    [self setCurrentPlaybackTime:event.positionTime];
    // update MPNowPlayingInfoPropertyElapsedPlaybackTime
    [[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:songInfo];

    return MPRemoteCommandHandlerStatusSuccess;
}
于 2016-10-16T21:52:37.683 に答える
5

私は同じものを探していましたが、これが可能だとは思いません。この投稿を参照してください。

iOS ロック画面コントロール パネルでオーディオ スクラバーを有効にする方法

また、Spotify や Soundcloud などの人気のあるアプリには、これが実装されていません。

ロック画面に現在の音楽を表示する方法を探している場合は、次の手順を実行する必要があります。

まず、新しいトラックを再生するときに NowPlayingInfo を更新します。

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

    [songInfo setObject:trackTitle forKey:MPMediaItemPropertyTitle];
    [songInfo setObject:artistName forKey:MPMediaItemPropertyArtist];
    [songInfo setObject:duration forKey:MPMediaItemPropertyPlaybackDuration];
    [songInfo setObject:releaseDate forKey:MPMediaItemPropertyReleaseDate];
    [songInfo setValue:playbackRate forKey:MPNowPlayingInfoPropertyPlaybackRate];
    [songInfo setObject:elapsedTime forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime];
    [songInfo setObject:albumArtImage forKey:MPMediaItemPropertyArtwork];
    [[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:songInfo];

ロック画面からのイベントを処理するには、まずリモコンからのイベントの受信を開始するようにアプリに指示する必要があります。次のコードを使用して、AppDelegate のアプリケーション didFinishLaunchingWithOptions でこれを行います

 // Turn on remote control event delivery
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

次に、キャプチャされたイベントを処理するために remoteControlReceivedWithEvent メソッドを実装する必要があります。APPDelegate で、次のメソッドを追加します

- (void)remoteControlReceivedWithEvent:(UIEvent *)receivedEvent {

 if (receivedEvent.type == UIEventTypeRemoteControl) {

    switch (receivedEvent.subtype) {
        case UIEventSubtypeRemoteControlPause:
            //pause code here
            break;

        case UIEventSubtypeRemoteControlPlay:
             //play code here
            break;

        case UIEventSubtypeRemoteControlPreviousTrack:
            // previous track code here
            break;

        case UIEventSubtypeRemoteControlNextTrack:
            //next track code here
            break;

        default:
            break;
    }
 }

}

Apple ドキュメントの MPNowPlayingInfoCenter に関する詳細情報 -> https://developer.apple.com/library/ios/documentation/mediaplayer/reference/MPNowPlayingInfoCenter_Class

于 2014-01-17T09:31:42.187 に答える