106 if (toState != state) {
107 const auto fromState = std::exchange(state, toState);
108 if (toState == QMediaPlayer::PlayingState || fromState == QMediaPlayer::PlayingState)
109 emit q->playingChanged(toState == QMediaPlayer::PlayingState);
110 emit q->playbackStateChanged(toState);
130 setError(QMediaPlayer::NoError, {});
135 auto setErrorFn = [&](
136 QMediaPlayer::MediaStatus status,
137 QMediaPlayer::Error err,
138 const QString &errString)
140 control->setMedia(QUrl(),
nullptr);
141 control->mediaStatusChanged(status);
142 control->error(err, errString);
145 std::unique_ptr<QFile> file;
150 if (!media.isEmpty() && !stream && media.scheme() == QLatin1String(
"qrc")
154 file.reset(
new QFile(QLatin1Char(
':') + media.path()));
155 if (!file->open(QFile::ReadOnly)) {
158 QMediaPlayer::InvalidMedia,
159 QMediaPlayer::ResourceError,
160 QMediaPlayer::tr(
"Attempting to play invalid Qt resource"));
162 }
else if (
control->streamPlaybackSupported()) {
163 control->setMedia(media, file.get());
165#if QT_CONFIG(temporaryfile)
166#if defined(Q_OS_ANDROID)
167 QString tempFileName = QDir::tempPath() + media.path();
168 QDir().mkpath(QFileInfo(tempFileName).path());
169 std::unique_ptr<QTemporaryFile> tempFile { QTemporaryFile::createNativeFile(*file) };
170 if (tempFile.get() ==
nullptr) {
172 QMediaPlayer::InvalidMedia,
173 QMediaPlayer::ResourceError,
174 QMediaPlayer::tr(
"Failed to establish temporary file during playback"));
177 if (!tempFile->rename(tempFileName)) {
179 QMediaPlayer::InvalidMedia,
180 QMediaPlayer::ResourceError,
181 QStringLiteral(
"Could not rename temporary file to: %1").arg(tempFileName));
185 std::unique_ptr<QTemporaryFile> tempFile = std::make_unique<QTemporaryFile>();
189 const QString suffix = QFileInfo(*file).suffix();
190 if (!suffix.isEmpty())
191 tempFile->setFileTemplate(tempFile->fileTemplate() + QLatin1Char(
'.') + suffix);
194 if (!tempFile->open()) {
196 QMediaPlayer::InvalidMedia,
197 QMediaPlayer::ResourceError,
198 tempFile->errorString());
204 qint64 len = file->read(buffer,
sizeof(buffer));
207 tempFile->write(buffer, len);
211 file = std::move(tempFile);
212 control->setMedia(QUrl(QUrl::fromLocalFile(file->fileName())),
nullptr);
214 qWarning(
"Qt was built with -no-feature-temporaryfile: playback from resource file is not supported!");
219 QUrl url = qMediaFromUserInput(media);
220 if (url.scheme() == QLatin1String(
"content") && !stream) {
221 file.reset(
new QFile(media.url()));
225 control->setMedia(url, stream);
247QMediaPlayer::QMediaPlayer(QObject *parent)
248 : QObject(*
new QMediaPlayerPrivate, parent)
252 auto maybeControl = QPlatformMediaIntegration::instance()->createPlayer(
this);
254 d->control = maybeControl.value();
255 d->state = d->control->state();
257 qWarning() <<
"Failed to initialize QMediaPlayer" << maybeControl.error();
258 d->setError(QMediaPlayer::ResourceError, maybeControl.error());
663void QMediaPlayer::setSourceDevice(QIODevice *device,
const QUrl &sourceUrl)
668 if (d->source == sourceUrl && d->stream == device)
671 d->source = sourceUrl;
674 d->setMedia(d->source, device);
675 emit sourceChanged(d->source);
728void QMediaPlayer::setAudioBufferOutput(QAudioBufferOutput *output)
732 QAudioBufferOutput *oldOutput = d->audioBufferOutput;
733 if (oldOutput == output)
736 d->audioBufferOutput = output;
739 auto oldPlayer = QAudioBufferOutputPrivate::exchangeMediaPlayer(*oldOutput,
this);
741 oldPlayer->setAudioBufferOutput(
nullptr);
745 d->control->setAudioBufferOutput(output);
747 emit audioBufferOutputChanged();
775void QMediaPlayer::setAudioOutput(QAudioOutput *output)
778 auto oldOutput = d->audioOutput;
779 if (oldOutput == output)
781 d->audioOutput = output;
783 d->control->setAudioOutput(
nullptr);
785 oldOutput->setDisconnectFunction({});
787 output->setDisconnectFunction([
this](){ setAudioOutput(
nullptr); });
789 d->control->setAudioOutput(output->handle());
791 emit audioOutputChanged();
1010void QMediaPlayer::setVideoOutput(QObject *output)
1013 if (d->videoOutput == output)
1016 auto *sink = qobject_cast<QVideoSink *>(output);
1017 if (!sink && output) {
1018 auto *mo = output->metaObject();
1019 mo->invokeMethod(output,
"videoSink", Q_RETURN_ARG(QVideoSink *, sink));
1021 d->videoOutput = output;
1022 d->setVideoSink(sink);
1223void QMediaPlayer::setPlaybackOptions(
const QPlaybackOptions &options)
1226 if (std::exchange(d->playbackOptions, options) != options)
1227 emit playbackOptionsChanged();
1230void QMediaPlayer::resetPlaybackOptions()
1233 QPlaybackOptions defaultOptions{ };
1234 if (std::exchange(d->playbackOptions, defaultOptions) != defaultOptions)
1235 emit playbackOptionsChanged();