【问题标题】:how to show seek track duration on control center如何在控制中心显示寻道持续时间
【发布时间】:2014-01-10 09:55:15
【问题描述】:

如何将歌曲信息(例如歌曲名称和曲目时长)传递给控制中心。 播放器可以正常播放音乐,但播放和暂停控制也可以正常工作。

玩苹果的音乐应用

使用下面的代码玩我的应用程序,如何传递歌曲信息以显示它?

//AppDelegate
    -(void)setupAudio
    {
        // Set AVAudioSession
        NSError *sessionError = nil;
        [[AVAudioSession sharedInstance] setDelegate:self];
        [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&sessionError];

        // Change the default output audio route
        UInt32 doChangeDefaultRoute = 1;
        AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryDefaultToSpeaker,
                                sizeof(doChangeDefaultRoute), &doChangeDefaultRoute);

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

    //Make sure we can recieve remote control events
    - (BOOL)canBecomeFirstResponder {
        return YES;
    }

    - (void)remoteControlReceivedWithEvent:(UIEvent *)event
    {
        //if it is a remote control event handle it correctly
        if (event.type == UIEventTypeRemoteControl) {
            if (event.subtype == UIEventSubtypeRemoteControlPlay)
            {
                NSLog(@"UIEventSubtypeRemoteControlPlay");
                [[AppMusicPlayer sharedService]playAudio];
            }
            else if (event.subtype == UIEventSubtypeRemoteControlPause)
            {
                NSLog(@"UIEventSubtypeRemoteControlPause");
                [[AppMusicPlayer sharedService]pauseAudio];
            }
            else if (event.subtype == UIEventSubtypeRemoteControlTogglePlayPause)
            {
                NSLog(@"UIEventSubtypeRemoteControlTogglePlayPause");
            }
        }
    }

    AppMusicPlayer.m
    + (id) sharedService
    {
        static dispatch_once_t _singletonPredicate;
        static AppMusicPlayer *_sharedObject = nil;

        dispatch_once(&_singletonPredicate, ^{
            _sharedObject = [[AppMusicPlayer alloc]init];
        });

        return _sharedObject;
    }

    - (id)init
    {
        self = [super init];

        if (self) {
            // Work your initialising here as you normally would
        }

        return self;
    }

    - (void)playAudio
    {
        [self.audioPlayer play];
    }

    - (void)pauseAudio
    {
        NSLog(@"pause");
        [self.audioPlayer pause];
    }

    - (void)playAudioFromURL:(NSURL *)songURL
    {
        [self.audioPlayer stop];

        //Declare the audio file location and settup the player
        NSError *error;
        self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:songURL error:&error];
        [self.audioPlayer setNumberOfLoops:-1];

        if (error)
        {
            NSLog(@"%@", [error localizedDescription]);
               }
        else
        {
            //Load the audio into memory
            [self.audioPlayer prepareToPlay];
            [self.audioPlayer play];
        }
    }

-(void)setUpRemoteControl
{
    NSDictionary *nowPlaying = @{MPMediaItemPropertyArtist: songItem.artistName,
                                 MPMediaItemPropertyAlbumTitle: songItem.albumTitle,
                                 MPMediaItemPropertyPlaybackDuration:songItem.playbackDuration,
                                 MPNowPlayingInfoPropertyPlaybackRate:@1.0f,
                                 MPMediaItemPropertyArtwork:[songMedia valueForProperty:MPMediaItemPropertyArtwork]

                                 };

    [[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:nowPlaying];
}

【问题讨论】:

标签: ios objective-c avaudioplayer avaudiosession control-center


【解决方案1】:

您正在播放 iPod 音乐库中的歌曲吗? 如果是,那么您应该使用 MPMusicPlayerViewController,它为搜索栏属性和信息中心提供内置功能。

我可以在你的代码中看到你使用了 AVAudioPlayer, 我们只能使用 AVAudioPlayer 类设置以下详细信息,但无法在我们的应用程序中访问 seekbar 更改事件。

NSMutableDictionary *albumInfo = [[NSMutableDictionary alloc] init];
UIImage *artWork = [UIImage imageNamed:album.imageUrl];
[albumInfo setObject:album.title forKey:MPMediaItemPropertyTitle];
[albumInfo setObject:album.auther forKey:MPMediaItemPropertyArtist];
[albumInfo setObject:album.title forKey:MPMediaItemPropertyAlbumTitle];
[albumInfo setObject:artworkP forKey:MPMediaItemPropertyArtwork];
[albumInfo setObject:album.playrDur forKey:MPMediaItemPropertyPlaybackDuration]
[albumInfo setObject:album.elapsedTime forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime]
[[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:albumInfo]

这里我们需要根据需要的格式统计album.playrDur和album.elapsedTime。

【讨论】:

  • 当我调用 advanceToNextItem 时,音频细节不会显示在锁定屏幕上。
  • @ABJ 没听懂,advanceToNextItem 是什么?你的意思是“>>”下一首歌按钮吗?如果是,那么它将从那里调用一个委托方法,您需要再次像上面一样更新下一首歌曲信息。
【解决方案2】:

你正在寻找这个:

- (IBAction)playButtonTouched:(id)sender {

   Class playingInfoCenter = NSClassFromString(@"MPNowPlayingInfoCenter");

    if (playingInfoCenter) {


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


        MPMediaItemArtwork *albumArt = [[MPMediaItemArtwork alloc] initWithImage: [UIImage imagedNamed:@"AlbumArt"]];

        [songInfo setObject:@"Audio Title" forKey:MPMediaItemPropertyTitle];
        [songInfo setObject:@"Audio Author" forKey:MPMediaItemPropertyArtist];
        [songInfo setObject:@"Audio Album" forKey:MPMediaItemPropertyAlbumTitle];
        [songInfo setObject:albumArt forKey:MPMediaItemPropertyArtwork];
        [[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:songInfo];


    }
}

【讨论】:

    【解决方案3】:

    我今天碰巧在做同样的事情,不知道答案,但在我的应用程序中,控制中心确实显示了应用程序名称。我希望能够显示我正在玩的东西的标题。我什至有一张我想展示的照片,比如专辑封面。我不认为我在做任何与你不同的事情,例如,我没有编写任何代码来向它发送应用名称。

    等一下……我看到有一个叫做 MPNowPlayingInfoCenter 的东西,我们应该研究一下……

    【讨论】:

    • 嗨,lepton,我已经用我想要的内容更新了我的问题,发现我无法寻找轨道.....
    【解决方案4】:

    不确定,但这段代码可能对您有所帮助..

       - (void) handle_NowPlayingItemChanged: (id) notification
    {
        MPMediaItem *currentItem = [musicPlayer nowPlayingItem];
        NSString *titleString = [currentItem valueForProperty:MPMediaItemPropertyTitle];
        if (titleString)
        {
            titleLabel.text = [NSString stringWithFormat:@"Title: %@",titleString];
        } 
        else 
        {
            titleLabel.text = @"Title: Unknown title";
        }
    }
    

    【讨论】:

    • 嗨@iMove,感谢您的回复,但这不是我要找的。用截图更新了我的问题
    猜你喜欢
    • 1970-01-01
    • 2012-10-07
    • 1970-01-01
    • 2021-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多