gpt4 book ai didi

ios - AVPlayer + MPNowPlayingInfoCenter 在模拟器上工作,但在设备上不工作

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:02:19 25 4
gpt4 key购买 nike

我正在使用 AVPlayer 来重现音乐。在 iOS 6+ 上,一切都在模拟器上运行良好。但是当我在设备(iPhone 5s、iOS 7.1)上运行它时,MPNowPlayingInfoCenter 显示来自任何其他音乐应用程序的信息:spotify/sound cloud/音乐播放器,而不是我的应用程序中的当前播放项目。

这是我用来在 MPNowPlayingInfoCenter 中设置音乐信息的代码:

- (void)setupNowPlayingInfoCenter
{
if (NSClassFromString(@"MPNowPlayingInfoCenter")) {
// Here enters fine.
MPNowPlayingInfoCenter *playingInfoCenter = [MPNowPlayingInfoCenter defaultCenter];

if (self.playingUrl) {
NSMutableDictionary *songInfo = self.songInfo;

MPMediaItemArtwork *albumArt = [[MPMediaItemArtwork alloc] initWithImage:[self.playingObject valueForKey:@"image"]];

[songInfo setObject:[self.playingObject valueForKey:@"song"] forKey:MPMediaItemPropertyTitle];
[songInfo setObject:[self.playingObject valueForKey:@"artist"] forKey:MPMediaItemPropertyArtist];
[songInfo setObject:[self.playingObject valueForKey:@"album"] forKey:MPMediaItemPropertyAlbumTitle];
[songInfo setObject:[NSNumber numberWithDouble:self.progress] forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime];
[songInfo setObject:[NSNumber numberWithDouble:self.duration] forKey:MPMediaItemPropertyPlaybackDuration];
[songInfo setObject:(self.isPaused ? @(0.0f) : @(1.0f)) forKey:MPNowPlayingInfoPropertyPlaybackRate];
[songInfo setObject:albumArt forKey:MPMediaItemPropertyArtwork];

playingInfoCenter.nowPlayingInfo = songInfo;
}

其他相关代码:

- (NSError *)initialize
{
NSError *error = nil;
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayback
error:&error];
if (error) {
NSLog(@"%@", [error description]);
return error;
}
[audioSession setActive:YES error:&error];
if (error) {
NSLog(@"%@", [_error description]);
return error;
}

[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
return error;
}

任何帮助将不胜感激。

最佳答案

好吧,我自己找到了答案。

我正在为每首歌曲创建新播放器,如下所示:

self.audioPlayer = [AVPlayer playerWithURL:url];

我改变了它:

AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:url];
[self.audioPlayer replaceCurrentItemWithPlayerItem:playerItem];

神奇地它开始起作用了。

关于ios - AVPlayer + MPNowPlayingInfoCenter 在模拟器上工作,但在设备上不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28418252/

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