项目开发过程中 , 设备需要不断循环的播放同一个视频广告,网上很多例子是调用QMediaPlaylist+QMediaPlayer来实现循环的,但是这样会导致系统cpu占用率很高,容易导致视频卡主,黑屏的原因,
解决方法:
不使用QMediaPlayerlist 来进行循环 ;
QMediaPlayer new的时候不绑定父类;
V_widget=new QVideoWidget();
MC_player=new QMediaPlayer();
MC_player->setMedia(QMediaContent(QUrl::fromLocalFile(“本地视频路径”)));
MC_player->setVideoOutput(V_widget);
this->setCentralWidget(V_widget);
V_widget->show();
MC_player->play();
connect(MC_player,SIGNAL(mediaStatusChanged(QMediaPlayer::MediaStatus)),this,SLOT(Paly_InitStatus(QMediaPlayer::MediaStatus)));
void Paly_InitStatus(QMediaPlayer::MediaStatus status){
if(status == QMediaPlayer::EndOfMedia)
{ MC_player->setPosition(0);
MC_player->play(); }
}
当视频播放完成后 QMediaplayer会发送一个信号,捕抓这个信号然后把标志移到初始位置就可以实现重头播放,免去了预加载占用cpu过高的问题。
参考以下可捕抓到很多类似的信号:
Constant |
Value |
Description |
QMediaPlayer::UnknownMediaStatus |
0 |
The status of the media cannot be determined. |
QMediaPlayer::NoMedia |
1 |
The is no current media. The player is in the StoppedState. |
QMediaPlayer::LoadingMedia |
2 |
The current media is being loaded. The player may be in any state. |
QMediaPlayer::LoadedMedia |
3 |
The current media has been loaded. The player is in the StoppedState. |
QMediaPlayer::StalledMedia |
4 |
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 |
5 |
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 |
6 |
The player has fully buffered the current media. The player is in the PlayingState or PausedState. |
QMediaPlayer::EndOfMedia |
7 |
Playback has reached the end of the current media. The player is in the StoppedState. |
QMediaPlayer::InvalidMedia |
8 |
The current media cannot be played. The player is in the StoppedState. |
-------------------------------------------QTimer 问题
再同一个类函数里面构造两个QTimer 时间 如果 设置的开始时间相同 会导致其中一个不能发送timeout() 信号, 我通过设置不一样的时间,来解决了这问题。