• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

iOS,MPNowPlayingInfoCenter 暂停/播放按钮不起作用?

[复制链接]
菜鸟教程小白 发表于 2022-12-12 14:07:41 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我意识到这个问题之前已经被问过,但我的代码与其他答案相同(所以我认为)。当新歌曲成为当前歌曲时,图片和图像显示良好并更新。我似乎无法让播放/暂停和跳过按钮正常工作。这是他使用的代码。

-(void)viewWillAppearbool)animated
{
        [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
        [self becomeFirstResponder];  
}

-(void)viewWillDisappearBOOL)animated
{
    [super viewWillDisappear:YES];
    [[UIApplication sharedApplication] endReceivingRemoteControlEvents];
    [self resignFirstResponder];

}
-(BOOL)canBecomeFirstResponder
{
    return YES;
}

-(void)remoteControlReceivedWithEventUIEvent *)event
{
    if (event.type == UIEventTypeRemoteControl) {

        switch (event.subtype) {

            case UIEventSubtypeRemoteControlTogglePlayPause:[self playPauseButtonPressed:nil];
                NSLog(@"play pause button remote pressed");

                break;

            case UIEventSubtypeRemoteControlBeginSeekingForward:[self skipButtonPressed:nil];
                NSLog(@"Skip remote pressed");
                break;

            default: break;
        }
    }
}



Best Answer-推荐答案


使用此代码:

    switch (receivedEvent.subtype) {
        case UIEventSubtypeRemoteControlPlay:
            [player play];
            break;

        case UIEventSubtypeRemoteControlPause:
            [player pause];
            break;

        case UIEventSubtypeRemoteControlPreviousTrack:
            [player previous];
            break;

        case UIEventSubtypeRemoteControlNextTrack:
            [player next];
            break;

        default:
            break;
    }

UIEventSubtypeRemoteControlBeginSeekingForward 是当你长按下一个/上一个按钮时播放器寻找当前歌曲的时间。

确保您设置了 Audio Session 类别:

    NSError *setCategoryErr = nil;
    NSError *activationErr  = nil;
    [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:&setCategoryErr];
    [[AVAudioSession sharedInstance] setActive:YES error:&activationErr];

您还需要在暂停/恢复播放器时设置 MPNowPlayingInfoCenter 的播放速率,以便正确显示播放/暂停按钮。

        MPNowPlayingInfoCenter *playingInfoCenter = [MPNowPlayingInfoCenter defaultCenter];

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

        MPMediaItemArtwork *albumArt = [[MPMediaItemArtwork alloc] initWithImage:[UIImage imageNamed"image"]];

        [songInfo setObject"your song" forKey:MPMediaItemPropertyTitle];
        [songInfo setObject"your artist" forKey:MPMediaItemPropertyArtist];
        [songInfo setObject"your album" forKey:MPMediaItemPropertyAlbumTitle];
        [songInfo setObject:[NSNumber numberWithDouble:songProgress] forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime];
        [songInfo setObject:[NSNumber numberWithDouble:songDuration] forKey:MPMediaItemPropertyPlaybackDuration];
        [songInfo setObject:[NSNumber numberWithDoubleisPaused ? 0.0f : 1.0f)] forKey:MPNowPlayingInfoPropertyPlaybackRate];
        [songInfo setObject:albumArt forKey:MPMediaItemPropertyArtwork];

        [playingInfoCenter setNowPlayingInfo:songInfo]; 

关于iOS,MPNowPlayingInfoCenter 暂停/播放按钮不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28389176/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap