Improved error handling for media items

- Display a meaningful error message to the user when adding or trying
to play a video
- Make sure the video placeholder is visible even after switching pages
preferencesAboutTextFull
Craig Watson 9 years ago
parent 9d5105bad9
commit 3f73f25f6f
  1. 48
      src/domain/UBGraphicsMediaItem.cpp
  2. 6
      src/domain/UBGraphicsMediaItem.h

@ -71,6 +71,8 @@ UBGraphicsMediaItem::UBGraphicsMediaItem(const QUrl& pMediaFileUrl, QGraphicsIte
, mInitialPos(0)
{
mErrorString = "";
mMediaObject = new QMediaPlayer(this);
mMediaObject->setMedia(pMediaFileUrl);
@ -141,6 +143,9 @@ UBGraphicsVideoItem::UBGraphicsVideoItem(const QUrl &pMediaFileUrl, QGraphicsIte
connect(mMediaObject, SIGNAL(stateChanged(QMediaPlayer::State)),
this, SLOT(mediaStateChanged(QMediaPlayer::State)));
connect(mMediaObject, static_cast<void(QMediaPlayer::*)(QMediaPlayer::Error)>(&QMediaPlayer::error),
this, &UBGraphicsVideoItem::mediaError);
setAcceptHoverEvents(true);
update();
@ -346,6 +351,11 @@ void UBGraphicsMediaItem::stop()
void UBGraphicsMediaItem::togglePlayPause()
{
if (!mErrorString.isEmpty()) {
UBApplication::showMessage("Can't play media: " + mErrorString);
return;
}
if (mMediaObject->state() == QMediaPlayer::StoppedState)
mMediaObject->play();
@ -378,8 +388,29 @@ void UBGraphicsMediaItem::togglePlayPause()
void UBGraphicsMediaItem::mediaError(QMediaPlayer::Error errorCode)
{
if (errorCode != QMediaPlayer::NoError)
UBApplication::showMessage(mMediaObject->errorString());
// QMediaPlayer::errorString() isn't very descriptive, so we generate our own message
switch (errorCode) {
case QMediaPlayer::NoError:
mErrorString = "";
break;
case QMediaPlayer::ResourceError:
mErrorString = tr("Media resource couldn't be resolved");
break;
case QMediaPlayer::FormatError:
mErrorString = tr("Unsupported media format");
break;
case QMediaPlayer::ServiceMissingError:
mErrorString = tr("Media playback service not found");
break;
default:
mErrorString = tr("Media error: ") + QString(errorCode) + " (" + mMediaObject->errorString() + ")";
}
if (!mErrorString.isEmpty() ) {
UBApplication::showMessage(mErrorString);
qDebug() << mErrorString;
}
}
void UBGraphicsMediaItem::copyItemParameters(UBItem *copy) const
@ -569,6 +600,19 @@ void UBGraphicsVideoItem::mediaStateChanged(QMediaPlayer::State state)
}
void UBGraphicsVideoItem::activeSceneChanged()
{
// Update the visibility of the placeholder, to prevent it being hidden when switching pages
setPlaceholderVisible(!mErrorString.isEmpty());
UBGraphicsMediaItem::activeSceneChanged();
}
void UBGraphicsVideoItem::mediaError(QMediaPlayer::Error errorCode)
{
setPlaceholderVisible(errorCode != QMediaPlayer::NoError);
}
/**
* @brief Set the brush and fill to display a black rectangle
* @param visible If true, a black rectangle is displayed in place of the video

@ -149,6 +149,8 @@ protected:
QGraphicsPixmapItem *mLinkedImage;
qint64 mInitialPos;
QString mErrorString;
};
class UBGraphicsAudioItem: public UBGraphicsMediaItem
@ -183,6 +185,10 @@ public slots:
void videoSizeChanged(QSizeF newSize);
void hasVideoChanged(bool hasVideo);
void mediaStateChanged(QMediaPlayer::State state);
void activeSceneChanged();
protected slots:
void mediaError(QMediaPlayer::Error errorCode);
protected:

Loading…
Cancel
Save