gpt4 book ai didi

ios - 如何在控制中心显示搜索轨道持续时间

转载 作者:技术小花猫 更新时间:2023-10-29 11:03:57 25 4
gpt4 key购买 nike

如何将歌曲名称和轨道持续时间等歌曲信息传递到控制中心。播放器可以很好地播放音乐,播放和暂停控件也可以正常工作。

玩苹果的音乐应用

enter image description here enter image description here

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

enter image description here enter image description here

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

最佳答案

您正在播放 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。

关于ios - 如何在控制中心显示搜索轨道持续时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21041075/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com