当我点击“下一步"时按钮它暂停音乐而不是自动转到下一首歌曲 [英] When I tap the "next" button it pauses the music instead of automatically going to the next song

查看:29
本文介绍了当我点击“下一步"时按钮它暂停音乐而不是自动转到下一首歌曲的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近使用 MPMusicPlayerController 实现了一个完美运行的播放/暂停按钮,我现在添加了下一首歌曲";按钮.但是当我点击这个下一首歌曲"时按钮它暂停音乐,当我点击播放"时按钮,然后播放下一首歌曲.我想知道是否有一种方法可以让我点击下一首歌曲"按钮它会自动播放下一首歌曲而不是暂停它.这是我的 2 个按钮的代码:

Hi I recently implemented a play/pause button which works perfectly using MPMusicPlayerController I have now added a "next song" button. But when I click this "next song" button it pauses the music and when I tap the "play" button then it plays the next song. I was wondering if there was a way where as I click the "next song" button it would automatically play the next song instead of pausing it. Heres my code for the 2 buttons:

var musicPlayer = MPMusicPlayerController.systemMusicPlayer

@IBAction func nextMusicTapped(_ sender: UIButton) {
    musicPlayer.skipToNextItem()
    musicPlayer.prepareToPlay()
    musicPlayer.play()
    
}


@IBAction func playPauseMusicTapped(_ sender: UIButton) {
    let state = musicPlayer.playbackState.rawValue
    if (state == 1) { //playing
        musicPlayer.pause()
        musicPlayPauseButton.setImage(UIImage(systemName: "play.fill"), for: .normal)
        musicNextButton.isHidden = true
    } else if (state == 2) { //paused
        musicPlayer.prepareToPlay()
        musicPlayer.play()
        musicPlayPauseButton.setImage(UIImage(systemName: "pause.fill"), for: .normal)
        musicNextButton.isHidden = false
    }
}

推荐答案

首先,根据 Apple 的文档,skipToNextItem() 执行以下操作:

First off, according to Apple's documentation, skipToNextItem() does the following:

开始播放播放队列中的下一个媒体项目;或者,如果音乐播放器没有播放,则将下一个媒体项指定为下一个要播放的媒体项.

Starts playback of the next media item in the playback queue; or, if the music player is not playing, designates the next media item as the next to be played.

所以换句话说,如果音乐播放器已经在播放一首歌曲,你不应该告诉它播放下一个项目,它应该自动开始播放.所以你不需要以下几行:

So in other words, if the music player is already playing a song, you shouldn't have to tell it to play the next item, it should automatically start playing. So you wouldn't need the following lines:

musicPlayer.prepareToPlay()
musicPlayer.play()

这也让我相信,在调用 skipToNextItem() 之前,您的音乐播放器以某种方式暂停了.尝试将 print(musicPlayer.playbackState.rawValue) 放在第 4 行之前.如果它打印 2,那么您的音乐播放器似乎通过单击下一首歌曲"而暂停.按钮,所以这意味着您的 IBAction 连接发生了一些奇怪的事情.我之前做过这个,我不小心用同一个按钮调用了两个不同的 IBAction 方法(由于首先设置插座的错误).在您的特定情况下,您可能会不小心同时调用 playPauseMusicTapped(暂停音乐)和 nextMusicTapped() 方法来切换歌曲,但这只是随机的想法.让我知道第 4 行之前的播放状态是什么,无论如何.要是知道就好了.

This also leads me to believe that somehow your music player is being paused prior to that skipToNextItem() being called. Try putting print(musicPlayer.playbackState.rawValue) before line 4. If it prints 2, then your music player seems to pause by clicking the "next song" button, so that would mean something weird is going on with your IBAction connections. I've done this before where I accidentally call two different IBAction methods with the same button (due to an error in setting up the outlets in the first place). In your particular case, you could be accidentally calling both the playPauseMusicTapped (which pauses the music), and the nextMusicTapped() method to switch the songs, but that's just a random thought. Let me know what that playback state before line 4 is, regardless. That would be good to know.

这篇关于当我点击“下一步"时按钮它暂停音乐而不是自动转到下一首歌曲的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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