The QMediaPlayer class allows the playing of a media source. 更多...
头: | #include <QMediaPlayer> |
qmake: | QT += multimedia |
继承: | QMediaObject |
enum | Error { NoError, ResourceError, FormatError, NetworkError, AccessDeniedError, ServiceMissingError } |
enum | Flag { LowLatency, StreamPlayback, VideoSurface } |
flags | Flags |
enum | MediaStatus { UnknownMediaStatus, NoMedia, LoadingMedia, LoadedMedia, ..., InvalidMedia } |
enum | State { StoppedState, PlayingState, PausedState } |
|
|
QMediaPlayer (QObject * parent = nullptr, QMediaPlayer::Flags flags = Flags()) | |
virtual | ~QMediaPlayer () |
QAudio::Role | audioRole () const |
int | bufferStatus () const |
QMediaContent | currentMedia () const |
QNetworkConfiguration | currentNetworkConfiguration () const |
QString | customAudioRole () const |
qint64 | duration () const |
QMediaPlayer::Error | error () const |
QString | errorString () const |
bool | isAudioAvailable () const |
bool | isMuted () const |
bool | isSeekable () const |
bool | isVideoAvailable () const |
QMediaContent | media () const |
QMediaPlayer::MediaStatus | mediaStatus () const |
const QIODevice * | mediaStream () const |
qreal | playbackRate () const |
QMediaPlaylist * | playlist () const |
qint64 | position () const |
void | setAudioRole (QAudio::Role audioRole ) |
void | setCustomAudioRole (const QString & audioRole ) |
void | setVideoOutput (QVideoWidget * output ) |
void | setVideoOutput (QGraphicsVideoItem * output ) |
void | setVideoOutput (QAbstractVideoSurface * surface ) |
QMediaPlayer::State | state () const |
QList<QAudio::Role> | supportedAudioRoles () const |
QStringList | supportedCustomAudioRoles () const |
int | volume () const |
virtual QMultimedia::AvailabilityStatus | availability () const override |
void | pause () |
void | play () |
void | setMedia (const QMediaContent & media , QIODevice * stream = nullptr) |
void | setMuted (bool muted ) |
void | setNetworkConfigurations (const QList<QNetworkConfiguration> & configurations ) |
void | setPlaybackRate (qreal rate ) |
void | setPlaylist (QMediaPlaylist * playlist ) |
void | setPosition (qint64 position ) |
void | setVolume (int volume ) |
void | stop () |
void | audioAvailableChanged (bool available ) |
void | audioRoleChanged (QAudio::Role role ) |
void | bufferStatusChanged (int percentFilled ) |
void | currentMediaChanged (const QMediaContent & media ) |
void | customAudioRoleChanged (const QString & role ) |
void | durationChanged (qint64 duration ) |
void | error (QMediaPlayer::Error error ) |
void | mediaChanged (const QMediaContent & media ) |
void | mediaStatusChanged (QMediaPlayer::MediaStatus status ) |
void | mutedChanged (bool muted ) |
void | networkConfigurationChanged (const QNetworkConfiguration & configuration ) |
void | playbackRateChanged (qreal rate ) |
void | positionChanged (qint64 position ) |
void | seekableChanged (bool seekable ) |
void | stateChanged (QMediaPlayer::State state ) |
void | videoAvailableChanged (bool videoAvailable ) |
void | volumeChanged (int volume ) |
QMultimedia::SupportEstimate | hasSupport (const QString & mimeType , const QStringList & codecs = QStringList(), QMediaPlayer::Flags flags = Flags()) |
const QMetaObject | staticMetaObject |
The QMediaPlayer class allows the playing of a media source.
The 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 可以使用 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();
由于 QMediaPlayer 是 QMediaObject ,可以使用几个 QMediaObject 函数对于像这样的事情:
另请参阅 QMediaObject , QMediaService , QVideoWidget ,和 QMediaPlaylist .
定义媒体播放器错误条件。
常量 | 值 | 描述 |
---|---|---|
QMediaPlayer::NoError
|
0
|
没有出现错误。 |
QMediaPlayer::ResourceError
|
1
|
无法解析媒体资源。 |
QMediaPlayer::FormatError
|
2
|
不 (完全) 支持媒体资源格式。仍可能可以回放,但没有音频或视频组件。 |
QMediaPlayer::NetworkError
|
3
|
发生网络错误。 |
QMediaPlayer::AccessDeniedError
|
4
|
没有适当权限来播放媒体资源。 |
QMediaPlayer::ServiceMissingError
|
5
|
找不到有效回放服务,无法继续进行播放。开头 |
常量 | 值 | 描述 |
---|---|---|
QMediaPlayer::LowLatency
|
0x01
|
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
|
0x02
|
播放器期望播放 QIODevice 基流。若传递给 QMediaPlayer constructor, the service supporting streams playback will be chosen. |
QMediaPlayer::VideoSurface
|
0x04
|
The player is expected to be able to render to a QAbstractVideoSurface output . |
Flags 类型是 typedef 对于 QFlags <Flag>。它存储 Flag 值的 OR 组合。
定义媒体播放器当前媒体的状态。
常量 | 值 | 描述 |
---|---|---|
QMediaPlayer::UnknownMediaStatus
|
0
|
无法确定媒体状态。 |
QMediaPlayer::NoMedia
|
1
|
当前没有媒体。播放器处于 StoppedState . |
QMediaPlayer::LoadingMedia
|
2
|
正加载当前媒体。播放器可能处于任何状态。 |
QMediaPlayer::LoadedMedia
|
3
|
当前媒体已加载。播放器处于 StoppedState . |
QMediaPlayer::StalledMedia
|
4
|
当前媒体的回放已失速,由于缓冲不足或某些其它临时中断。播放器在 PlayingState or PausedState . |
QMediaPlayer::BufferingMedia
|
5
|
播放器正在缓冲数据,但已缓冲足够数据,以便在不久的将来能继续回放。播放器在 PlayingState or PausedState . |
QMediaPlayer::BufferedMedia
|
6
|
播放器已完全缓冲当前媒体。播放器处于 PlayingState or PausedState . |
QMediaPlayer::EndOfMedia
|
7
|
回放已到达当前媒体结尾。播放器处于 StoppedState . |
QMediaPlayer::InvalidMedia
|
8
|
当前媒体无法播放。播放器处于 StoppedState . |
定义媒体播放器的当前状态。
常量 | 值 | 描述 |
---|---|---|
QMediaPlayer::StoppedState
|
0
|
媒体播放器未播放内容,回放将从当前轨道的起始开始。 |
QMediaPlayer::PlayingState
|
1
|
媒体播放器目前正在播放内容。 |
QMediaPlayer::PausedState
|
2
|
媒体播放器已暂停回放,当前轨道的回放将从播放器暂停位置处再继续。 |
此特性保持当前媒体的音频可用性状态。
As the life time of QMediaPlayer can be longer than the playback of one QMediaContent , this property may change over time, the audioAvailableChanged signal can be used to monitor it's status.
访问函数:
bool | isAudioAvailable () const |
通知程序信号:
void | audioAvailableChanged (bool available ) |
This property holds the role of the audio stream played by the media player.
It can be set to specify the type of audio being played, allowing the system to make appropriate decisions when it comes to volume, routing or post-processing.
必须设置音频角色,先于调用 setMedia ().
customAudioRole is cleared when this property is set to anything other than QAudio::CustomRole .
该特性在 Qt 5.6 引入。
访问函数:
QAudio::Role | audioRole () const |
void | setAudioRole (QAudio::Role audioRole ) |
通知程序信号:
void | audioRoleChanged (QAudio::Role role ) |
另请参阅 supportedAudioRoles ().
This property holds the percentage of the temporary buffer filled before playback begins or resumes, from
0
(空) 到
100
(完整)。
When the player object is buffering; this property holds the percentage of the temporary buffer that is filled. The buffer will need to reach 100% filled before playback can start or resume, at which time
mediaStatus
() 会返回
BufferedMedia
or
BufferingMedia
. If the value is anything lower than
100
,
mediaStatus
() 会返回
StalledMedia
.
访问函数:
int | bufferStatus () const |
通知程序信号:
void | bufferStatusChanged (int percentFilled ) |
另请参阅 mediaStatus ().
This property holds the current active media content being played by the player object. This value could be different from QMediaPlayer::media property if a playlist is used. In this case currentMedia indicates the current media content being processed by the player, while QMediaPlayer::media property contains the original playlist.
访问函数:
QMediaContent | currentMedia () const |
通知程序信号:
void | currentMediaChanged (const QMediaContent & media ) |
另请参阅 QMediaContent and media ().
This property holds the role of the audio stream played by the media player.
It can be set to specify the type of audio being played when the backend supports audio roles unknown to Qt. Specifying a role allows the system to make appropriate decisions when it comes to volume, routing or post-processing.
必须设置音频角色,先于调用 setMedia ().
audioRole 被设为 QAudio::CustomRole 当此特性有设置。
该特性在 Qt 5.11 引入。
访问函数:
QString | customAudioRole () const |
void | setCustomAudioRole (const QString & audioRole ) |
通知程序信号:
void | customAudioRoleChanged (const QString & role ) |
另请参阅 supportedCustomAudioRoles ().
此特性保持当前媒体的持续时间。
The value is the total playback time in milliseconds of the current media. The value may change across the life time of the QMediaPlayer object and may not be available when initial playback begins, connect to the durationChanged () signal to receive status notifications.
访问函数:
qint64 | duration () const |
通知程序信号:
void | durationChanged (qint64 duration ) |
此特性保持描述最后一个错误条件的字符串。
访问函数:
QString | errorString () const |
另请参阅 error ().
此特性保持播放器对象正在使用的活动媒体源。
播放器对象将使用 QMediaContent for selection of the content to be played.
默认情况下,此特性拥有 null QMediaContent .
将此特性设为 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.
访问函数:
QMediaContent | media () const |
void | setMedia (const QMediaContent & media , QIODevice * stream = nullptr) |
通知程序信号:
void | mediaChanged (const QMediaContent & media ) |
另请参阅 QMediaContent and currentMedia ().
此特性保持当前媒体流的状态。
流状态描述当前流的回放进展。
默认情况下此特性为 QMediaPlayer::NoMedia
访问函数:
QMediaPlayer::MediaStatus | mediaStatus () const |
通知程序信号:
void | mediaStatusChanged (QMediaPlayer::MediaStatus status ) |
另请参阅 state .
This property holds the muted state of the current media.
The value will be true if the playback volume is muted; otherwise false.
访问函数:
bool | isMuted () const |
void | setMuted (bool muted ) |
通知程序信号:
void | mutedChanged (bool muted ) |
此特性保持当前媒体的回放速率。
This value is a multiplier applied to the media's standard play rate. By default this value is 1.0, indicating that the media is playing at the standard pace. Values higher than 1.0 will increase the rate of play. Values less than zero can be set and indicate the media will rewind at the multiplier of the standard pace.
Not all playback services support change of the playback rate. It is framework defined as to the status and quality of audio and video while fast forwarding or rewinding.
访问函数:
qreal | playbackRate () const |
void | setPlaybackRate (qreal rate ) |
通知程序信号:
void | playbackRateChanged (qreal rate ) |
This property holds the media playlist being used by the player object.
The player object will use the current playlist item for selection of the content to be played.
By default this property is set to null.
If the media playlist is used as a source, QMediaPlayer::currentMedia is updated with a current playlist item. The current source should be selected with QMediaPlaylist::setCurrentIndex (int) instead of QMediaPlayer::setMedia (), otherwise the current playlist will be discarded.
访问函数:
QMediaPlaylist * | playlist () const |
void | setPlaylist (QMediaPlaylist * playlist ) |
另请参阅 QMediaContent .
此特性保持当前媒体的回放位置。
值是当前回放位置,以从媒体开始起的毫秒数表达。将指示位置的周期性变化采用信号 positionChanged (),可以设置更新间隔采用 QMediaObject 的方法 setNotifyInterval ().
访问函数:
qint64 | 位置 () const |
void | setPosition (qint64 position ) |
通知程序信号:
void | positionChanged (qint64 position ) |
此特性保持当前媒体的可寻址状态
若支持寻址,此特性为 true;否则为 false。此特性的状态可能改变,当跨存活时间在 QMediaPlayer 对象,使用 seekableChanged 信号以监视改变。
访问函数:
bool | isSeekable () const |
通知程序信号:
void | seekableChanged (bool seekable ) |
此特性保持媒体播放器的回放状态。
默认情况下此特性为 QMediaPlayer::Stopped
访问函数:
QMediaPlayer::State | state () const |
通知程序信号:
void | stateChanged (QMediaPlayer::State state ) |
另请参阅 mediaStatus (), play (), pause (),和 stop ().
This property holds the video availability status for the current media.
若可用, QVideoWidget class can be used to view the video. As the life time of QMediaPlayer can be longer than the playback of one QMediaContent , this property may change over time, the videoAvailableChanged signal can be used to monitor it's status.
访问函数:
bool | isVideoAvailable () const |
通知程序信号:
void | videoAvailableChanged (bool videoAvailable ) |
另请参阅 QVideoWidget and QMediaContent .
此特性保持当前回放音量。
The playback volume is scaled linearly, ranging from
0
(无声) 到
100
(full volume). Values outside this range will be clamped.
默认情况下,音量为
100
.
UI volume controls should usually be scaled nonlinearly. For example, using a logarithmic scale will produce linear changes in perceived loudness, which is what a user would normally expect from a volume control. See QAudio::convertVolume () 了解更多细节。
访问函数:
int | volume () const |
void | setVolume (int volume ) |
通知程序信号:
void | volumeChanged (int volume ) |
构造 QMediaPlayer instance parented to parent 和采用 flags .
[虚拟]
QMediaPlayer::
~QMediaPlayer
()
销毁播放器对象。
[signal]
void
QMediaPlayer::
audioAvailableChanged
(
bool
available
)
信号音频内容的可用性已改变成 available .
注意: 通知程序信号对于特性 audioAvailable .
[signal]
void
QMediaPlayer::
audioRoleChanged
(
QAudio::Role
role
)
信号音频 role 为媒体播放器已改变。
该函数在 Qt 5.6 引入。
注意: 通知程序信号对于特性 audioRole .
[override virtual]
QMultimedia::AvailabilityStatus
QMediaPlayer::
availability
() const
重实现自 QMediaObject::availability ().
[signal]
void
QMediaPlayer::
bufferStatusChanged
(
int
percentFilled
)
Signal the amount of the local buffer filled as a percentage by percentFilled .
注意: 通知程序信号对于特性 bufferStatus .
[signal]
void
QMediaPlayer::
currentMediaChanged
(const
QMediaContent
&
media
)
Signals that the current playing content has been changed to media .
注意: 通知程序信号对于特性 currentMedia .
另请参阅 currentMedia () 和 mediaChanged ().
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.
[signal]
void
QMediaPlayer::
customAudioRoleChanged
(const
QString
&
role
)
信号音频 role 为媒体播放器已改变。
该函数在 Qt 5.11 引入。
注意: 通知程序信号对于特性 customAudioRole .
[signal]
void
QMediaPlayer::
durationChanged
(
qint64
duration
)
Signal the duration of the content has changed to duration ,表达 (以毫秒为单位)。
注意: 通知程序信号对于特性 duration .
返回当前错误状态。
[signal]
void
QMediaPlayer::
error
(
QMediaPlayer::Error
error
)
Signals that an error 条件已发生。
注意: 信号 error 在此类中被重载。通过使用函数指针句法连接到此信号,Qt 提供用于获得如此范例展示的函数指针的方便帮助程序:
connect(mediaPlayer, QOverload<QMediaPlayer::Error>::of(&QMediaPlayer::error), [=](QMediaPlayer::Error error){ /* ... */ });
另请参阅 errorString ().
[static]
QMultimedia::SupportEstimate
QMediaPlayer::
hasSupport
(const
QString
&
mimeType
, const
QStringList
&
codecs
= QStringList(),
QMediaPlayer::Flags
flags
= Flags())
Returns the level of support a media player has for a mimeType and a set of codecs .
The flags argument allows additional requirements such as performance indicators to be specified.
[signal]
void
QMediaPlayer::
mediaChanged
(const
QMediaContent
&
media
)
发射信号当媒体源已变为 media .
注意: 通知程序信号对于特性 media .
另请参阅 media () 和 currentMediaChanged ().
[signal]
void
QMediaPlayer::
mediaStatusChanged
(
QMediaPlayer::MediaStatus
status
)
信号 status 若当前媒体已改变。
注意: 通知程序信号对于特性 mediaStatus .
另请参阅 mediaStatus ().
返回媒体数据的流源。
This is only valid if a stream was passed to setMedia ().
另请参阅 setMedia ().
[signal]
void
QMediaPlayer::
mutedChanged
(
bool
muted
)
Signal the mute state has changed to muted .
注意: 通知程序信号对于特性 muted .
[signal]
void
QMediaPlayer::
networkConfigurationChanged
(const
QNetworkConfiguration
&
configuration
)
Signal that the active in use network access point has been changed to configuration and all subsequent network access will use this configuration .
[slot]
void
QMediaPlayer::
pause
()
暂停播放当前源。
[slot]
void
QMediaPlayer::
play
()
开始 (或再继续) 播放当前源。
[signal]
void
QMediaPlayer::
playbackRateChanged
(
qreal
rate
)
信号 playbackRate 已改变成 rate .
注意: 通知程序信号对于特性 playbackRate .
[signal]
void
QMediaPlayer::
positionChanged
(
qint64
position
)
Signal the position of the content has changed to position ,表达 (以毫秒为单位)。
注意: 通知程序信号对于特性 position .
[signal]
void
QMediaPlayer::
seekableChanged
(
bool
seekable
)
信号 seekable 播放器对象状态已改变。
注意: 通知程序信号对于特性 seekable .
[slot]
void
QMediaPlayer::
setMedia
(const
QMediaContent
&
media
,
QIODevice
*
stream
= nullptr)
设置当前 media 源。
若 stream is supplied; media data will be read from it instead of resolving the media source. In this case the media source may still be used to resolve additional information about the media such as mime type. The stream must be open and readable.
将媒体设为 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 () 和 error () signals to be notified when the media is loaded and when an error occurs during loading.
从 Qt 5.12.2 起,URL 方案
gst-pipeline
provides custom pipelines for the GStreamer backend.
player = new QMediaPlayer; player->setMedia(QUrl("gst-pipeline: videotestsrc ! autovideosink")); player->play();
If the pipeline contains a video sink element named
qtvideosink
, current
QVideoWidget
can be used to render the video.
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();
注意: setter 函数对于特性 media .
另请参阅 media ().
[slot]
void
QMediaPlayer::
setNetworkConfigurations
(const
QList
<
QNetworkConfiguration
> &
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.
Attach a QVideoWidget video output to the media player.
If the media player has already video output attached, it will be replaced with a new one.
Attach a QGraphicsVideoItem video output to the media player.
If the media player has already video output attached, it will be replaced with a new one.
设置视频 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.
[signal]
void
QMediaPlayer::
stateChanged
(
QMediaPlayer::State
state
)
信号 state 对于播放器对象已改变。
注意: 通知程序信号对于特性 state .
[slot]
void
QMediaPlayer::
stop
()
停止播放,然后将播放位置重置到起始。
返回支持的音频角色列表。
If setting the audio role is not supported, an empty list is returned.
该函数在 Qt 5.6 引入。
另请参阅 audioRole .
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.
该函数在 Qt 5.11 引入。
另请参阅 customAudioRole .
[signal]
void
QMediaPlayer::
videoAvailableChanged
(
bool
videoAvailable
)
Signal the availability of visual content has changed to videoAvailable .
注意: 通知程序信号对于特性 videoAvailable .
[signal]
void
QMediaPlayer::
volumeChanged
(
int
volume
)
回放音量信号已改变成 volume .
注意: 通知程序信号对于特性 volume .