如何在控制中心显示寻道轨迹持续时间 [英] how to show seek track duration on control center

查看:202
本文介绍了如何在控制中心显示寻道轨迹持续时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



使用Apple的音乐应用






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




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

//更改默认输出音频路线
UInt32 doChangeDefaultRoute = 1;
AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryDe​​faultToSpeaker,
sizeof(doChangeDefaultRoute),& doChangeDefaultRoute);

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

//确保我们可以收到远程控制事件
- (BOOL)canBecomeFirstResponder {
返回YES;
}

- (void)remoteControlReceivedWithEvent:(UIEvent *)event
{
//如果是远程控制事件正确处理它
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){
//像往常一样在这里工作你的初始化
}

返回自我;
}

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

- (无效)pauseAudio
{
NSLog(@暂停);
[self.audioPlayer pause];
}

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

//声明音频文件位置并设置播放器
NSError *错误;
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:songURL error:& error];
[self.audioPlayer setNumberOfLoops:-1];

if(error)
{
NSLog(@%@,[error localizedDescription]);
}
else
{
//将音频加载到内存
[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];
}


解决方案

你是否正在播放iPod中的歌曲曲库 ?
如果是,你应该使用MPMusicPlayerViewController,它为搜索栏属性和信息中心提供了内置功能。<​​/ p>

我可以在你的代码中看到你使用过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。


How do i pass the song info such as song name and track duration to control center. Player plays music fine and the play and pause control works fine though.

Playing with apple's music app

Playing with my app with the code below, how to pass song information in order to display it ?

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

解决方案

Are you playing songs from iPods music library ? If yes than You should you MPMusicPlayerViewController, it provides in-build functionality for seek bar properties as well as Info center.

I can see here in your code you have used AVAudioPlayer, We can set only below details using AVAudioPlayer class, but can't access seekbar change event in our app.

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]

Here we need to count album.playrDur and album.elapsedTime as per required format.

这篇关于如何在控制中心显示寻道轨迹持续时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆