c++ - 有什么方法可以从任意点开始声子媒体对象中的流吗?

标签 c++ qt phonon

我正在编写一个应用程序来在 Mac OS X 上的 Qt 4.8.1 中收听播客。现在,我正在设置我的 MediaObject 以使用来自播客的 RSS 项目的附件 URL,像这样:

mediaObj->setCurrentSource(Phonon::MediaSource(QUrl(idx.data(Metadata::Enclosure).toString())));

注意:idx 是一个 QModelIndex,对应于用户在 QTreeView< 中单击的模型中的 QStandardItem/ 小部件。

不过,我现在有两个问题。

  1. 每当我暂停并再次启动流时,它都会从头开始。不是很有帮助。 :-(
  2. 如果我使用我的 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 是最后一个 tick() 信号 mediaObj 发出的时间。我尝试使用 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 以一种它不想要的方式运行。 我担心这似乎是声子模块的一个长期存在的问题,我可能不得不以其他方式实现流。帮忙?

最佳答案

好吧,看来我太花哨了。我正在使用单个按钮进行播放/暂停。我只需要改变这个:

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;
    }
}

当然,我最终也会想要处理那些其他状态。但这解决了我的第一个问题。另一个问题可能要等到另一天。

关于c++ - 有什么方法可以从任意点开始声子媒体对象中的流吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10400731/

相关文章:

c++ - WX : attempting to select value when clicking a text field

c++ - 在 BB10 应用程序开发的 cpp 文件中包含 <QNetworkAccessManager>、<QNetworkReply> 时出错

qt - 如何从YouTube网址(如VLC)播放视频?

ffmpeg - 多媒体框架的差异

qt - 直接显示 9 声子错误 "Pins cannot connect"

c++ - Unicode 工具提示未显示

c++ - 可以在 mfc 中为 ado 禁用 Schema.ini 中的 TextDelimiter 吗?

c++ - 谷歌测试 (gtest) : ASSERT_PREDx and class member functions

c++ - 如何为子类 QComboBox 编写 paintEvent()

python - 单击按钮时如何调用两个函数?