如何在控制中心显示寻道轨迹持续时间

 秘密不能说雨 发布于 2023-02-04 17:18

如何将歌曲名称和曲目持续时间等歌曲信息传递给控制中心.播放器播放音乐很好,播放和暂停控制工作正常.

玩苹果的音乐应用程序

在此输入图像描述 在此输入图像描述

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

在此输入图像描述 在此输入图像描述

//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];
}

Samir.. 6

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

我可以在你的代码中看到你使用AVAudioPlayer,我们只能使用AVAudioPlayer类设置以下细节,但无法在我们的应用程序中访问搜索栏更改事件.

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.

1 个回答
  • 你在播放iPod音乐库中的歌曲吗?如果是,那么您应该使用MPMusicPlayerViewController,它为搜索栏属性以及信息中心提供了内置功能.

    我可以在你的代码中看到你使用AVAudioPlayer,我们只能使用AVAudioPlayer类设置以下细节,但无法在我们的应用程序中访问搜索栏更改事件.

    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.

    2023-02-04 17:22 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有