Skip to content

Commit

Permalink
Added error indicator for when an audio file can not be played; a lis…
Browse files Browse the repository at this point in the history
…t without a working file no longer gets stuck in an endless loop
  • Loading branch information
PhilInTheGaps committed Dec 20, 2023
1 parent 2a4bf52 commit aa18887
Show file tree
Hide file tree
Showing 16 changed files with 129 additions and 39 deletions.
22 changes: 19 additions & 3 deletions app/ui/tools/audio/AudioInfoView.qml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
pragma ComponentBehavior: Bound

import QtQuick
import QtQuick.Controls
import src
import IconFonts
import common

Item {
id: audio_info_frame
Expand Down Expand Up @@ -67,13 +68,27 @@ Item {
delegate: Item {
id: playlist_delegate

// TODO: type
required property AudioFile modelData
required property int index

width: playlist_view.width
width: error_indicator.visible ? playlist_view.width + error_indicator.width : playlist_view.width
height: playlist_text.height + 10

Text {
id: error_indicator

visible: playlist_delegate.modelData.hadError

font: FontAwesome.fontSolid
text: FontAwesome.triangleExclamation
color: SettingsManager.colors.error
anchors.top: parent.top
anchors.bottom: parent.bottom
anchors.left: parent.left
anchors.leftMargin: 5
verticalAlignment: Text.AlignVCenter
}

Text {
id: playlist_text
clip: true
Expand All @@ -83,6 +98,7 @@ Item {
font.pointSize: 10
anchors.centerIn: parent
width: parent.width - 10
leftPadding: error_indicator.visible ? error_indicator.width + error_indicator.anchors.leftMargin : 0
}

ToolTip {
Expand Down
2 changes: 1 addition & 1 deletion src/tools/audio/audiotool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ void AudioTool::next()
{
if (m_musicElementType == AudioElement::Type::Music)
{
musicPlayer.next();
musicPlayer.next(false);
}
}

Expand Down
8 changes: 7 additions & 1 deletion src/tools/audio/metadata/metadatareader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ void MetaDataReader::setMetaData(QMediaMetaData::Key key, const QVariant &value)
case QMediaMetaData::CoverArtImage: {
if (m_coverFile) m_coverFile->deleteLater();

if (value.value<QImage>().isNull())
{
m_metaData.cover({});
break;
}

m_coverFile = std::make_unique<QTemporaryFile>();

if (m_coverFile->open())
Expand Down Expand Up @@ -144,7 +150,7 @@ void MetaDataReader::loadMetaData(const QString &path, const QByteArray &data)

if (auto artist = tag->artist(); !artist.isEmpty())
{
m_metaData.artist(QString::fromStdString(artist.to8Bit(true)).split(", "));
m_metaData.artist(QString::fromStdString(artist.to8Bit(true)).split(", "_L1));
}

if (auto album = tag->album(); !album.isEmpty())
Expand Down
2 changes: 1 addition & 1 deletion src/tools/audio/players/audioplayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public slots:
virtual void stop() = 0;
virtual void setVolume(int linear, int logarithmic) = 0;
virtual void again() = 0;
virtual void next() = 0;
virtual void next(bool withError) = 0;

protected:
static auto normalizeVolume(int volume) -> float;
Expand Down
55 changes: 42 additions & 13 deletions src/tools/audio/players/bufferedaudioplayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ void BufferedAudioPlayer::setIndex(qsizetype index)
}
else
{
next();
next(true);
}
}
}
Expand Down Expand Up @@ -110,14 +110,14 @@ auto BufferedAudioPlayer::loadPlaylist() -> QFuture<void>
void BufferedAudioPlayer::handleUnsupportedMediaSource(const AudioFile &file)
{
qCWarning(gmAudioBufferedPlayer()) << "Media type" << file.source() << "is currently not supported.";
next();
next(true);
}

void BufferedAudioPlayer::onFileReceived(const Files::FileDataResult &result)
{
if (!result.success() || result.data().isEmpty())
{
next();
next(true);
return;
}

Expand Down Expand Up @@ -149,8 +149,6 @@ void BufferedAudioPlayer::stop()

m_mediaPlayer.stop();
state(AudioPlayer::State::Stopped);

m_playlist->clear();
}

void BufferedAudioPlayer::setVolume(int linear, int logarithmic)
Expand All @@ -164,14 +162,29 @@ void BufferedAudioPlayer::again()
m_mediaPlayer.setPosition(0);
}

void BufferedAudioPlayer::next()
void BufferedAudioPlayer::next(bool withError)
{
if (!m_element || m_playlist->isEmpty())
{
stop();
return;
}

if (withError)
{
auto *file = m_playlist->at(playlistIndex());
if (file)
{
file->hadError(true);
}

if (!m_playlist->hasElementsWithoutErrors())
{
stop();
return;
}
}

// Complete random
if (m_element->mode() == AudioElement::Mode::Random)
{
Expand Down Expand Up @@ -201,7 +214,16 @@ void BufferedAudioPlayer::next()
void BufferedAudioPlayer::onMediaPlayerPlaybackStateChanged(QMediaPlayer::PlaybackState newState)
{
qCDebug(gmAudioBufferedPlayer) << "Media player playback state changed:" << newState;
if (newState == QMediaPlayer::PlayingState) state(State::Playing);
if (newState == QMediaPlayer::PlayingState)
{
state(State::Playing);

auto *file = m_playlist->at(playlistIndex());
if (file)
{
file->hadError(false);
}
}
}

void BufferedAudioPlayer::onMediaStatusChanged(QMediaPlayer::MediaStatus status)
Expand All @@ -212,7 +234,7 @@ void BufferedAudioPlayer::onMediaStatusChanged(QMediaPlayer::MediaStatus status)
{
case QMediaPlayer::EndOfMedia:
qCDebug(gmAudioBufferedPlayer()) << "End of media was reached, playing next file ...";
next();
next(false);
break;
case QMediaPlayer::BufferingMedia:
state(State::Loading);
Expand All @@ -234,7 +256,7 @@ void BufferedAudioPlayer::onMediaPlayerErrorOccurred(QMediaPlayer::Error error,

if (error != QMediaPlayer::NoError)
{
next();
next(true);
}
}

Expand All @@ -244,7 +266,7 @@ void BufferedAudioPlayer::startPlaying()

if (m_element->mode() == AudioElement::Mode::Random)
{
next();
next(false);
return;
}

Expand Down Expand Up @@ -298,14 +320,21 @@ void BufferedAudioPlayer::loadWebFile(const QString &url)
m_mediaPlayer.setSource(QUrl(url));
m_mediaPlayer.play();
m_audioOutput.setMuted(false);

QMediaMetaData metaData;
metaData.insert(QMediaMetaData::Key::Title, "-");
metaData.insert(QMediaMetaData::Key::Author, "-");
metaData.insert(QMediaMetaData::Key::AlbumTitle, "-");
metaData.insert(QMediaMetaData::Key::CoverArtImage, QImage());
emit metaDataChanged(metaData);
}

void BufferedAudioPlayer::loadYouTubeFile(AudioFile &file)
{
const Services::VideoId id(file.url());
if (!id.isValid())
{
next();
next(true);
return;
}

Expand All @@ -314,7 +343,7 @@ void BufferedAudioPlayer::loadYouTubeFile(AudioFile &file)
.then([this, &file](const Services::YouTubeVideo &video) {
if (video.audioStreamUrl.isEmpty())
{
next();
next(true);
return;
}

Expand All @@ -333,7 +362,7 @@ void BufferedAudioPlayer::loadYouTubeFile(AudioFile &file)
emit metaDataChanged(metaData);
});
})
.onCanceled([this]() { next(); });
.onCanceled([this]() { next(true); });
}

void BufferedAudioPlayer::applyShuffleMode()
Expand Down
2 changes: 1 addition & 1 deletion src/tools/audio/players/bufferedaudioplayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public slots:
void stop() override;
void setVolume(int linear, int logarithmic) override;
void again() override;
void next() override;
void next(bool withError) override;

signals:
void playlistChanged();
Expand Down
10 changes: 8 additions & 2 deletions src/tools/audio/players/musicplayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,21 +81,27 @@ void MusicPlayer::handleUnsupportedMediaSource(const AudioFile &file)
break;
default:
qCCritical(gmAudioMusic()) << "loadMedia() is not implemented for type" << file.source();
next();
next(true);
break;
}
}

void MusicPlayer::loadSpotifyFile(const AudioFile &file)
{
if (!SpotifyPlayer::canPlay())
{
next(true);
return;
}

m_spotifyPlayer.play(file.url());
state(State::Playing);
}

void MusicPlayer::onSpotifySongEnded()
{
qCDebug(gmAudioMusic()) << "Spotify song ended, starting next song ...";
next();
next(false);
}

void MusicPlayer::onSpotifyStateChanged(State state)
Expand Down
6 changes: 4 additions & 2 deletions src/tools/audio/players/soundplayercontroller.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ public slots:
emit stopAll();
}
void setVolume(int linear, int logarithmic) override;
void next() override
{ /* Not Implemented */
void next(bool withError) override
{
/* Not Implemented */
Q_UNUSED(withError)
}
void again() override
{ /* Not Implemented */
Expand Down
9 changes: 7 additions & 2 deletions src/tools/audio/players/spotifyplayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ SpotifyPlayer::SpotifyPlayer(MetaDataReader &mDReader, QObject *parent)
connect(&m_metaDataTimer, &QTimer::timeout, this, &SpotifyPlayer::onMetaDataTimerTimeout);
}

auto SpotifyPlayer::canPlay() -> bool
{
return isSpotifyAvailable();
}

/// The current song has ended, stop any spotify activity and notify music player
void SpotifyPlayer::onDurationTimerTimeout()
{
Expand All @@ -42,7 +47,7 @@ auto SpotifyPlayer::isSpotifyAvailable() -> bool
return false;
}

qCWarning(gmAudioSpotify) << "Spotify connection is disabled.";
qCWarning(gmAudioSpotify) << "Not connected to Spotify.";
return false;
}

Expand Down Expand Up @@ -153,7 +158,7 @@ void SpotifyPlayer::pausePlay()
/**
* @brief Switch to next song in playlist
*/
void SpotifyPlayer::next()
void SpotifyPlayer::next(bool /*withError*/)
{
qCDebug(gmAudioSpotify) << "Skipping to next track ...";

Expand Down
4 changes: 3 additions & 1 deletion src/tools/audio/players/spotifyplayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ class SpotifyPlayer : public AudioPlayer
public:
SpotifyPlayer(MetaDataReader &mDReader, QObject *parent = nullptr);

[[nodiscard]] static auto canPlay() -> bool;

public slots:
void play(const QString &uri);
void play() override;
void pause() override;
void stop() override;
void pausePlay();
void next() override;
void next(bool withError) override;
void again() override;
void setVolume(int linear, int logarithmic) override;

Expand Down
10 changes: 10 additions & 0 deletions src/tools/audio/playlist/audioplaylist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,13 @@ void AudioPlaylist::clear()
{
m_files.clear();
}

auto AudioPlaylist::hasElementsWithoutErrors() const -> bool
{
foreach (const auto *file, m_files)
{
if (!file->hadError()) return true;
}

return false;
}
2 changes: 2 additions & 0 deletions src/tools/audio/playlist/audioplaylist.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class AudioPlaylist
void shuffle();
void clear();

[[nodiscard]] auto hasElementsWithoutErrors() const -> bool;

private:
QList<AudioFile *> m_files;
Type m_type = Type::Undefined;
Expand Down
Loading

0 comments on commit aa18887

Please sign in to comment.