内容表

上一话题

QMediaObject

下一话题

QMediaPlayerControl

QMediaPlayer

QMediaPlayer class allows the playing of a media source. 更多

Inheritance diagram of PySide2.QtMultimedia.QMediaPlayer

概要

信号

静态函数

详细描述

QMediaPlayer class is a high level media playback class. It can be used to playback such content as songs, movies and internet radio. The content to playback is specified as a QMediaContent 对象,可以认为是附加了额外信息的主 URL 或典型 URL。当提供采用 QMediaContent 回放也许可以开始。

player = new QMediaPlayer;
connect(player, SIGNAL(positionChanged(qint64)), this, SLOT(positionChanged(qint64)));
player->setMedia(QUrl::fromLocalFile("/Users/me/Music/coolsong.mp3"));
player->setVolume(50);
player->play();
											

QVideoWidget can be used with QMediaPlayer for video rendering and QMediaPlaylist 为访问播放列表功能。

playlist = new QMediaPlaylist;
playlist->addMedia(QUrl("http://example.com/movie1.mp4"));
playlist->addMedia(QUrl("http://example.com/movie2.mp4"));
playlist->addMedia(QUrl("http://example.com/movie3.mp4"));
playlist->setCurrentIndex(1);
player = new QMediaPlayer;
player->setPlaylist(playlist);
videoWidget = new QVideoWidget;
player->setVideoOutput(videoWidget);
videoWidget->show();
player->play();
											

Since QMediaPlayer QMediaObject ,可以使用几个 QMediaObject 函数对于像这样的事情:

  • Accessing the currently playing media’s metadata ( metaData() and 预定义元数据键 )

  • 校验媒体回放服务目前是否可用 ( availability() )

class QMediaPlayer ( [ parent=None [ , flags=QMediaPlayer.Flags() ] ] )
param parent

QObject

param flags

标志

构造 QMediaPlayer instance parented to parent 和采用 flags .

PySide2.QtMultimedia.QMediaPlayer. State

定义媒体播放器的当前状态。

常量

描述

QMediaPlayer.StoppedState

媒体播放器未播放内容,回放将从当前轨道的起始开始。

QMediaPlayer.PlayingState

媒体播放器目前正在播放内容。

QMediaPlayer.PausedState

媒体播放器已暂停回放,当前轨道的回放将从播放器暂停位置处再继续。

PySide2.QtMultimedia.QMediaPlayer. MediaStatus

Defines the status of a media player’s current media.

常量

描述

QMediaPlayer.UnknownMediaStatus

无法确定媒体状态。

QMediaPlayer.NoMedia

当前没有媒体。播放器处于 StoppedState .

QMediaPlayer.LoadingMedia

当前正加载媒体。播放器可能处于任何状态。

QMediaPlayer.LoadedMedia

The current media has been loaded. The player is in the StoppedState .

QMediaPlayer.StalledMedia

Playback of the current media has stalled due to insufficient buffering or some other temporary interruption. The player is in the PlayingState or PausedState .

QMediaPlayer.BufferingMedia

The player is buffering data but has enough data buffered for playback to continue for the immediate future. The player is in the PlayingState or PausedState .

QMediaPlayer.BufferedMedia

The player has fully buffered the current media. The player is in the PlayingState or PausedState .

QMediaPlayer.EndOfMedia

回放已到达当前媒体结尾。播放器处于 StoppedState .

QMediaPlayer.InvalidMedia

当前媒体无法播放。播放器处于 StoppedState .

PySide2.QtMultimedia.QMediaPlayer. Flag

常量

描述

QMediaPlayer.LowLatency

The player is expected to be used with simple audio formats, but playback should start without significant delay. Such playback service can be used for beeps, ringtones, etc.

QMediaPlayer.StreamPlayback

The player is expected to play QIODevice based streams. If passed to QMediaPlayer constructor, the service supporting streams playback will be chosen.

QMediaPlayer.VideoSurface

The player is expected to be able to render to a QAbstractVideoSurface output .

PySide2.QtMultimedia.QMediaPlayer. Error

定义媒体播放器错误条件。

常量

描述

QMediaPlayer.NoError

没有发生错误。

QMediaPlayer.ResourceError

A media resource couldn’t be resolved.

QMediaPlayer.FormatError

The format of a media resource isn’t (fully) supported. Playback may still be possible, but without an audio or video component.

QMediaPlayer.NetworkError

发生网络错误。

QMediaPlayer.AccessDeniedError

没有适当权限来播放媒体资源。

QMediaPlayer.ServiceMissingError

找不到有效回放服务,无法继续进行播放。开头

PySide2.QtMultimedia.QMediaPlayer. audioAvailableChanged ( available )
参数

available bool

PySide2.QtMultimedia.QMediaPlayer. audioRole ( )
返回类型

Role

另请参阅

setAudioRole()

PySide2.QtMultimedia.QMediaPlayer. audioRoleChanged ( role )
参数

role Role

PySide2.QtMultimedia.QMediaPlayer. bufferStatus ( )
返回类型

int

PySide2.QtMultimedia.QMediaPlayer. bufferStatusChanged ( percentFilled )
参数

percentFilled int

PySide2.QtMultimedia.QMediaPlayer. currentMedia ( )
返回类型

QMediaContent

PySide2.QtMultimedia.QMediaPlayer. currentMediaChanged ( media )
参数

media QMediaContent

PySide2.QtMultimedia.QMediaPlayer. currentNetworkConfiguration ( )
返回类型

QNetworkConfiguration

注意

此函数被弃用。

Returns the current network access point in use. If a default contructed QNetworkConfiguration is returned this feature is not available or that none of the current supplied configurations are in use.

PySide2.QtMultimedia.QMediaPlayer. customAudioRole ( )
返回类型

unicode

PySide2.QtMultimedia.QMediaPlayer. customAudioRoleChanged ( role )
参数

role – unicode

PySide2.QtMultimedia.QMediaPlayer. duration ( )
返回类型

qint64

PySide2.QtMultimedia.QMediaPlayer. durationChanged ( duration )
参数

duration qint64

PySide2.QtMultimedia.QMediaPlayer. error ( error )
参数

error Error

PySide2.QtMultimedia.QMediaPlayer. error ( )
返回类型

Error

返回当前错误状态。

PySide2.QtMultimedia.QMediaPlayer. errorString ( )
返回类型

unicode

static PySide2.QtMultimedia.QMediaPlayer. hasSupport ( mimeType [ , codecs=list() [ , flags=QMediaPlayer.Flags() ] ] )
参数
  • mimeType – unicode

  • codecs – 字符串列表

  • flags 标志

返回类型

SupportEstimate

Returns the level of support a media player has for a mimeType and a set of codecs .

flags argument allows additional requirements such as performance indicators to be specified.

PySide2.QtMultimedia.QMediaPlayer. isAudioAvailable ( )
返回类型

bool

PySide2.QtMultimedia.QMediaPlayer. isMuted ( )
返回类型

bool

PySide2.QtMultimedia.QMediaPlayer. isSeekable ( )
返回类型

bool

PySide2.QtMultimedia.QMediaPlayer. isVideoAvailable ( )
返回类型

bool

PySide2.QtMultimedia.QMediaPlayer. media ( )
返回类型

QMediaContent

另请参阅

setMedia()

PySide2.QtMultimedia.QMediaPlayer. mediaChanged ( media )
参数

media QMediaContent

PySide2.QtMultimedia.QMediaPlayer. mediaStatus ( )
返回类型

MediaStatus

PySide2.QtMultimedia.QMediaPlayer. mediaStatusChanged ( status )
参数

status MediaStatus

PySide2.QtMultimedia.QMediaPlayer. mediaStream ( )
返回类型

QIODevice

Returns the stream source of media data.

This is only valid if a stream was passed to setMedia() .

另请参阅

setMedia()

PySide2.QtMultimedia.QMediaPlayer. mutedChanged ( muted )
参数

muted bool

PySide2.QtMultimedia.QMediaPlayer. networkConfigurationChanged ( configuration )
参数

configuration QNetworkConfiguration

注意

此函数被弃用。

PySide2.QtMultimedia.QMediaPlayer. pause ( )

暂停播放当前源。

PySide2.QtMultimedia.QMediaPlayer. play ( )

开始 (或再继续) 播放当前源。

PySide2.QtMultimedia.QMediaPlayer. playbackRate ( )
返回类型

qreal

另请参阅

setPlaybackRate()

PySide2.QtMultimedia.QMediaPlayer. playbackRateChanged ( rate )
参数

rate qreal

PySide2.QtMultimedia.QMediaPlayer. playlist ( )
返回类型

QMediaPlaylist

另请参阅

setPlaylist()

PySide2.QtMultimedia.QMediaPlayer. position ( )
返回类型

qint64

另请参阅

setPosition()

PySide2.QtMultimedia.QMediaPlayer. positionChanged ( position )
参数

position qint64

PySide2.QtMultimedia.QMediaPlayer. seekableChanged ( seekable )
参数

seekable bool

PySide2.QtMultimedia.QMediaPlayer. setAudioRole ( audioRole )
参数

audioRole Role

另请参阅

audioRole()

PySide2.QtMultimedia.QMediaPlayer. setCustomAudioRole ( audioRole )
参数

audioRole – unicode

另请参阅

customAudioRole()

PySide2.QtMultimedia.QMediaPlayer. setMedia ( media [ , stream=None ] )
参数

设置当前 media 源。

stream is supplied; media data will be read from it instead of resolving the media source. In this case the url should be provided to resolve additional information about the media such as mime type. The stream must be open and readable. For macOS the stream should be also seekable.

Setting the media to a null QMediaContent will cause the player to discard all information relating to the current media source and to cease all I/O operations related to that media.

注意

This function returns immediately after recording the specified source of the media. It does not wait for the media to finish loading and does not check for errors. Listen for the mediaStatusChanged() and error() signals to be notified when the media is loaded and when an error occurs during loading.

从 Qt 5.12.2 起,URL 方案 gst-pipeline 提供自定义管道为 GStreamer 后端。

player = new QMediaPlayer;
player->setMedia(QUrl("gst-pipeline: videotestsrc ! autovideosink"));
player->play();
											

QAbstractVideoSurface is used as the video output, qtvideosink can be used as a video sink element directly in the pipeline. After that the surface will receive the video frames in present() .

class Surface : public QAbstractVideoSurface
{
public:
    Surface(QObject *p) : QAbstractVideoSurface(p) { }
    QList<QVideoFrame::PixelFormat> supportedPixelFormats(QAbstractVideoBuffer::HandleType) const override
    {
        // Make sure that the driver supports this pixel format.
        return QList<QVideoFrame::PixelFormat>() << QVideoFrame::Format_YUYV;
    }
    // Video frames are handled here.
    bool present(const QVideoFrame &) override { return true; }
};
player = new QMediaPlayer;
player->setVideoOutput(new Surface(player));
player->setMedia(QUrl("gst-pipeline: videotestsrc ! qtvideosink"));
player->play();
											

QVideoWidget is used as the video output and the pipeline contains a video sink element named qtvideosink , current QVideoWidget will be used to render the video.

player = new QMediaPlayer;
videoWidget = new QVideoWidget;
videoWidget->show();
player->setVideoOutput(videoWidget);
player->setMedia(QUrl("gst-pipeline: videotestsrc ! xvimagesink name=\"qtvideosink\""));
player->play();
											

If the pipeline contains appsrc element, it will be used to push data from stream .

QImage img("images/qt-logo.png");
img = img.convertToFormat(QImage::Format_ARGB32);
QByteArray ba(reinterpret_cast<const char *>(img.bits()), img.sizeInBytes());
QBuffer buffer(&ba);
buffer.open(QIODevice::ReadOnly);
player = new QMediaPlayer;
player->setMedia(QUrl("gst-pipeline: appsrc blocksize=4294967295 ! \
    video/x-raw,format=BGRx,framerate=30/1,width=200,height=147 ! \
    coloreffects preset=heat ! videoconvert ! video/x-raw,format=I420 ! jpegenc ! rtpjpegpay ! \
    udpsink host=127.0.0.1 port=5000"), &buffer);
player->play();
QMediaPlayer *receiver = new QMediaPlayer;
videoWidget = new QVideoWidget;
receiver->setVideoOutput(videoWidget);
receiver->setMedia(QUrl("gst-pipeline: udpsrc port=5000 ! \
    application/x-rtp,encoding-name=JPEG,payload=26 ! rtpjpegdepay ! jpegdec ! \
    xvimagesink name=qtvideosink"));
receiver->play();
// Content will be shown in this widget.
videoWidget->show();
											

另请参阅

media()

PySide2.QtMultimedia.QMediaPlayer. setMuted ( muted )
参数

muted bool

另请参阅

isMuted()

PySide2.QtMultimedia.QMediaPlayer. setNetworkConfigurations ( configurations )
参数

configurations

注意

此函数被弃用。

Sets the network access points for remote media playback. configurations contains, in ascending preferential order, a list of configuration that can be used for network access.

This will invalidate the choice of previous configurations.

PySide2.QtMultimedia.QMediaPlayer. setPlaybackRate ( rate )
参数

rate qreal

另请参阅

playbackRate()

PySide2.QtMultimedia.QMediaPlayer. setPlaylist ( playlist )
参数

playlist QMediaPlaylist

另请参阅

playlist()

PySide2.QtMultimedia.QMediaPlayer. setPosition ( position )
参数

position qint64

另请参阅

position()

PySide2.QtMultimedia.QMediaPlayer. setVideoOutput ( surface )
参数

surface QAbstractVideoSurface

Sets a video surface as the video output of a media player.

If a video output has already been set on the media player the new surface will replace it.

PySide2.QtMultimedia.QMediaPlayer. setVideoOutput ( surfaces )
参数

surfaces

PySide2.QtMultimedia.QMediaPlayer. setVolume ( volume )
参数

volume int

另请参阅

volume()

PySide2.QtMultimedia.QMediaPlayer. state ( )
返回类型

State

PySide2.QtMultimedia.QMediaPlayer. stateChanged ( newState )
参数

newState State

PySide2.QtMultimedia.QMediaPlayer. stop ( )

停止播放,然后将播放位置重置到起始。

PySide2.QtMultimedia.QMediaPlayer. supportedAudioRoles ( )
返回类型

返回支持的音频角色列表。

If setting the audio role is not supported, an empty list is returned.

另请参阅

audioRole

PySide2.QtMultimedia.QMediaPlayer. supportedCustomAudioRoles ( )
返回类型

字符串列表

Returns a list of supported custom audio roles. An empty list may indicate that the supported custom audio roles aren’t known. The list may not be complete.

另请参阅

customAudioRole

static PySide2.QtMultimedia.QMediaPlayer. supportedMimeTypes ( [ flags=QMediaPlayer.Flags() ] )
参数

flags 标志

返回类型

字符串列表

Returns a list of MIME types supported by the media player.

flags argument causes the resultant list to be restricted to MIME types which can be supported given additional requirements, such as performance indicators.

This function may not return useful results on some platforms, and support for a specific file of a given mime type is not guaranteed even if the mime type is in general supported. In addition, in some cases this function will need to load all available media plugins and query them for their support, which may take some time.

PySide2.QtMultimedia.QMediaPlayer. videoAvailableChanged ( videoAvailable )
参数

videoAvailable bool

PySide2.QtMultimedia.QMediaPlayer. volume ( )
返回类型

int

另请参阅

setVolume()

PySide2.QtMultimedia.QMediaPlayer. volumeChanged ( volume )
参数

volume int