我正在编写一个应用程序,用于在Mac OS X上收听Qt 4.8.1中的播客。现在,我正在将MediaObject
设置为使用播客 RSS 项目中的附件 URL,如下所示:
mediaObj->setCurrentSource(Phonon::MediaSource(QUrl(idx.data(Metadata::Enclosure).toString())));
注意:idx
是一个QModelIndex
,对应于用户在QTreeView
小部件中单击的模型中的QStandardItem
。
不过,我现在有两个问题。
- 每当我暂停流并重新启动它时,它都会从头开始。没那么有帮助。:-(
- 如果我使用
Phonon::SeekSlider
小部件跳到尚未缓冲的流的一部分,我会被反弹回流的开头。
我(可能有缺陷的(想法是,如果我能找到一种方法让mediaObj
从流中的特定时间开始播放,我就可以解决这两个问题。所以,我试过这个:
void BrodCastMainWin::pauseStream()
{
//save the "left off at" point in the stream
qDebug() << "current time is:" << currentTime;
qDebug() << "Saving place at" << currentTimeString
<< "in item" << nowPlaying.data(Qt::DisplayRole).toString();
//need to use QModelIndex for setData()
QModelIndex nowPlayingHandle = nowPlaying;
feed.setData(nowPlayingHandle, QVariant(currentTime), Metadata::LeftOffAt);
//pause the stream
mediaObj->pause();
}
void BrodCastMainWin::playStream()
{
//seek to the "left off at" point in the stream
qDebug() << "Trying to seek to" << convertTime(nowPlaying.data(Metadata::LeftOffAt).toLongLong())
<< "in item" << nowPlaying.data(Qt::DisplayRole).toString();
mediaObj->seek(nowPlaying.data(Metadata::LeftOffAt).toLongLong());
//play the file
mediaObj->play();
}
注意:currentTime
是截至mediaObj
发出最后一个tick()
信号的时间。我试图在mediaObj
中使用currentTime()
方法获取这些数据,但没有奏效。但这是另一天的战斗。同样,currentTimeString
是人类可读格式的相同信息。
唉,它不起作用:-(。这是我播放流时该代码发生的情况:
current time is: 32153
Saving place at "00:32" in item "Leo Laporte - The Tech Guy 867"
switching button to 'play'
槽的。但是当我尝试再次播放时:
Trying to seek to "00:32" in item "Leo Laporte - The Tech Guy 867"
Playing/Loading/Buffering; switching button to 'pause'
current time is: 0
Saving place at "00:00" in item "Leo Laporte - The Tech Guy 867"
switching button to 'play'
Trying to seek to "00:00" in item "Leo Laporte - The Tech Guy 867"
Playing/Loading/Buffering; switching button to 'pause'
我完全糊涂了。似乎我试图让Phonon以一种它不想的方式行事。我担心这似乎是 Phonon 模块长期存在的问题,我可能不得不以其他方式实现流。帮助?
好吧,看来我太花哨了。我使用一个按钮进行播放/暂停。我只需要改变这个:
void BrodCastMainWin::handleMediaState(Phonon::State state, Phonon::State)
{
switch (state)
{
case Phonon::PlayingState:
case Phonon::LoadingState:
case Phonon::BufferingState:
qDebug() << "Playing/Loading/Buffering; switching button to 'pause'";
//If we're playing, the button should pause
ui->playPauseButton->setIcon(QIcon(":/assets/stock_media-pause.svg"));
connect(ui->playPauseButton, SIGNAL(clicked()),
this, SLOT(pauseStream()));
break;
case Phonon::PausedState:
case Phonon::StoppedState:
qDebug() << "switching button to 'play'";
//if we're paused, the button should play
ui->playPauseButton->setIcon(QIcon(":/assets/stock_media-play.svg"));
connect(ui->playPauseButton, SIGNAL(clicked()),
this, SLOT(playStream()));
case Phonon::ErrorState:
//additionally, if there's an error, do error handling.
break;
default:
break;
}
}
。对此:
void BrodCastMainWin::handleMediaState(Phonon::State state, Phonon::State)
{
switch (state)
{
case Phonon::PlayingState:
qDebug() << "Playing/Loading/Buffering; switching button to 'pause'";
//If we're playing, the button should pause
ui->playPauseButton->setIcon(QIcon(":/assets/stock_media-pause.svg"));
connect(ui->playPauseButton, SIGNAL(clicked()),
this, SLOT(pauseStream()));
break;
case Phonon::PausedState:
qDebug() << "switching button to 'play'";
//if we're paused, the button should play
ui->playPauseButton->setIcon(QIcon(":/assets/stock_media-play.svg"));
connect(ui->playPauseButton, SIGNAL(clicked()),
this, SLOT(playStream()));
break;
case Phonon::ErrorState:
//additionally, if there's an error, do error handling.
break;
case Phonon::LoadingState:
case Phonon::BufferingState:
case Phonon::StoppedState:
default:
break;
}
}
当然,我最终也会想处理其他状态。但这解决了我的第一个问题。另一个问题可能要等到另一天。