1
0
Fork 0
mirror of https://github.com/HenkKalkwater/harbour-sailfin.git synced 2024-12-22 14:05:18 +00:00

Remove complicated QMediaPlayer switching

THe idea behind the switching was to provide gapless playback, but it
made the code too complicated and to intertwined with QtMultiMedia.

I might attempt to implement it later, but I probably will put the code
in a separate class to make it easier to swap the QtMultiMedia
implementation with a multimedia implementation for a player with better
support for gappless playback.
This commit is contained in:
Chris Josten 2021-08-02 00:52:21 +02:00
parent 86672be051
commit 242ccc90c1
2 changed files with 10 additions and 45 deletions

View file

@ -226,21 +226,12 @@ private:
/// Pointer to the current media player.
QMediaPlayer *m_mediaPlayer = nullptr;
// There are 2 media players over here, so one is able to preload the next song
// before the other starts playing
/// Media player 1
QMediaPlayer *m_mediaPlayer1;
/// Media player 2
QMediaPlayer *m_mediaPlayer2;
Model::Playlist *m_queue = nullptr;
int m_queueIndex = 0;
bool m_resumePlayback = true;
// Helper methods
void setItem(QSharedPointer<Model::Item> newItem);
void swapMediaPlayer();
void setStreamUrl(const QUrl &streamUrl);
void setPlaybackState(QMediaPlayer::State newState);

View file

@ -37,12 +37,10 @@ namespace ViewModel {
PlaybackManager::PlaybackManager(QObject *parent)
: QObject(parent),
m_item(nullptr),
m_mediaPlayer1(new QMediaPlayer(this)),
m_mediaPlayer2(new QMediaPlayer(this)),
m_mediaPlayer(new QMediaPlayer(this)),
m_urlFetcherThread(new ItemUrlFetcherThread(this)),
m_queue(new Model::Playlist(this)) {
// Set up connections.
swapMediaPlayer();
m_updateTimer.setInterval(10000); // 10 seconds
m_updateTimer.setSingleShot(false);
@ -52,6 +50,15 @@ PlaybackManager::PlaybackManager(QObject *parent)
connect(&m_updateTimer, &QTimer::timeout, this, &PlaybackManager::updatePlaybackInfo);
connect(m_urlFetcherThread, &ItemUrlFetcherThread::itemUrlFetched, this, &PlaybackManager::onItemExtraDataReceived);
m_urlFetcherThread->start();
connect(m_mediaPlayer, &QMediaPlayer::stateChanged, this, &PlaybackManager::mediaPlayerStateChanged);
connect(m_mediaPlayer, &QMediaPlayer::positionChanged, this, &PlaybackManager::mediaPlayerPositionChanged);
connect(m_mediaPlayer, &QMediaPlayer::durationChanged, this, &PlaybackManager::mediaPlayerDurationChanged);
connect(m_mediaPlayer, &QMediaPlayer::mediaStatusChanged, this, &PlaybackManager::mediaPlayerMediaStatusChanged);
connect(m_mediaPlayer, &QMediaPlayer::videoAvailableChanged, this, &PlaybackManager::hasVideoChanged);
// I do not like the complicated overload cast
connect(m_mediaPlayer, SIGNAL(error(QMediaPlayer::Error)), this, SLOT(mediaPlayerError(QMediaPlayer::Error)));
}
void PlaybackManager::onDestroyed() {
@ -184,7 +191,6 @@ void PlaybackManager::playItemInList(ItemModel *playlist, int index) {
void PlaybackManager::next() {
m_mediaPlayer->stop();
m_mediaPlayer->setMedia(QMediaContent());
swapMediaPlayer();
if (m_nextItem.isNull()) {
setItem(m_queue->nextItem());
@ -203,7 +209,6 @@ void PlaybackManager::previous() {
m_nextStreamUrl = m_streamUrl;
m_streamUrl = QString();
m_nextItem = m_item;
swapMediaPlayer();
m_queue->previous();
setItem(m_queue->currentItem());
@ -258,32 +263,6 @@ void PlaybackManager::postPlaybackInfo(PlaybackInfoType type) {
m_apiClient->setDefaultErrorHandler(rep);
}
void PlaybackManager::swapMediaPlayer() {
if (m_mediaPlayer != nullptr) {
disconnect(m_mediaPlayer, &QMediaPlayer::stateChanged, this, &PlaybackManager::mediaPlayerStateChanged);
disconnect(m_mediaPlayer, &QMediaPlayer::positionChanged, this, &PlaybackManager::mediaPlayerPositionChanged);
disconnect(m_mediaPlayer, &QMediaPlayer::durationChanged, this, &PlaybackManager::mediaPlayerDurationChanged);
disconnect(m_mediaPlayer, &QMediaPlayer::mediaStatusChanged, this, &PlaybackManager::mediaPlayerMediaStatusChanged);
disconnect(m_mediaPlayer, &QMediaPlayer::videoAvailableChanged, this, &PlaybackManager::hasVideoChanged);
// I do not like the complicated overload cast
disconnect(m_mediaPlayer, SIGNAL(error(QMediaPlayer::Error)), this, SLOT(mediaPlayerError(QMediaPlayer::Error)));
}
if (m_mediaPlayer == m_mediaPlayer1) {
m_mediaPlayer = m_mediaPlayer2;
emit mediaPlayerChanged(m_mediaPlayer);
} else {
m_mediaPlayer = m_mediaPlayer1;
emit mediaPlayerChanged(m_mediaPlayer);
}
connect(m_mediaPlayer, &QMediaPlayer::stateChanged, this, &PlaybackManager::mediaPlayerStateChanged);
connect(m_mediaPlayer, &QMediaPlayer::positionChanged, this, &PlaybackManager::mediaPlayerPositionChanged);
connect(m_mediaPlayer, &QMediaPlayer::durationChanged, this, &PlaybackManager::mediaPlayerDurationChanged);
connect(m_mediaPlayer, &QMediaPlayer::mediaStatusChanged, this, &PlaybackManager::mediaPlayerMediaStatusChanged);
connect(m_mediaPlayer, &QMediaPlayer::videoAvailableChanged, this, &PlaybackManager::hasVideoChanged);
// I do not like the complicated overload cast
connect(m_mediaPlayer, SIGNAL(error(QMediaPlayer::Error)), this, SLOT(mediaPlayerError(QMediaPlayer::Error)));
}
void PlaybackManager::componentComplete() {
if (m_apiClient == nullptr) qWarning() << "No ApiClient set for PlaybackManager";
m_qmlIsParsingComponent = false;
@ -410,11 +389,6 @@ void PlaybackManager::onItemExtraDataReceived(const QString &itemId, const QUrl
emit playMethodChanged(m_playMethod);
m_mediaPlayer->setMedia(QMediaContent(url));
m_mediaPlayer->play();
} else if (!m_nextItem.isNull() && m_nextItem->jellyfinId() == itemId){
QMediaPlayer *otherMediaPlayer = m_mediaPlayer == m_mediaPlayer1 ? m_mediaPlayer2 : m_mediaPlayer1;
m_nextPlaySessionId = playSession;
m_nextStreamUrl = url.toString();
otherMediaPlayer->setMedia(QMediaContent(url));
} else {
qDebug() << "Late reply for " << itemId << " received, ignoring";
}