Qt wiki will be updated on October 12th 2023 starting at 11:30 AM (EEST) and the maintenance will last around 2-3 hours. During the maintenance the site will be unavailable.

Seek in Sound File: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
[[Category:Snippets]]<br />[[Category:HowTo]]
[[Category:Snippets]]
[[Category:HowTo]]


= How to Seek in Sound File =
= How to Seek in Sound File =
Line 5: Line 6:
I tried to play a sound file not at the beginning but with an offset of 20 seconds.
I tried to play a sound file not at the beginning but with an offset of 20 seconds.


The following way of setting an offset '''fails''':<br /><code>player = new QMediaPlayer(this);<br />player->setMedia(QUrl::fromLocalFile("myCoolSong.mp3"));<br />player->setVolume(50);<br />player->play(); // music can be heard<br />qDebug() << player->isSeekable(); // is always false<br />player->setPosition ( 20*1000 ); //does not work</code>
The following way of setting an offset '''fails''':
<code>player = new QMediaPlayer(this);
player->setMedia(QUrl::fromLocalFile("myCoolSong.mp3"));
player->setVolume(50);
player->play(); // music can be heard
qDebug() << player->isSeekable(); // is always false
player->setPosition ( 20*1000 ); //does not work</code>


'''''It does not work because QMediaPlayer works asynchronously.'''''
'''''It does not work because QMediaPlayer works asynchronously.'''''
Line 11: Line 18:
As soon as the seek operations are delayed everything works as intended. For example:
As soon as the seek operations are delayed everything works as intended. For example:


<code>CoolPlayer::on_Button1_clicked()<br />{<br /> qDebug() << player->isSeekable(); // true<br /> player->setPosition ( 20*1000 );<br />}</code>
<code>CoolPlayer::on_Button1_clicked()
{
qDebug() << player->isSeekable(); // true
player->setPosition ( 20*1000 );
}</code>


To make my life easier I have derived a class MediaPlayer from QMediaPlayer and added a blocking load method:
To make my life easier I have derived a class MediaPlayer from QMediaPlayer and added a blocking load method:


<code>void MediaPlayer::loadBlocking(const QString &amp;file;)<br />{<br /> setMedia(QUrl::fromLocalFile(file));<br /> if(!isSeekable())<br /> {<br /> QEventLoop loop;<br /> QTimer timer;<br /> timer.setSingleShot(true);<br /> timer.setInterval(2000);<br /> loop.connect(&amp;timer;, SIGNAL (timeout()), &amp;loop;, SLOT (quit()) );<br /> loop.connect(this, SIGNAL (seekableChanged(bool)), &amp;loop;, SLOT (quit()));<br /> loop.exec();<br /> }<br />}</code>
<code>void MediaPlayer::loadBlocking(const QString &amp;file;)
{
setMedia(QUrl::fromLocalFile(file));
if(!isSeekable())
{
QEventLoop loop;
QTimer timer;
timer.setSingleShot(true);
timer.setInterval(2000);
loop.connect(&amp;timer;, SIGNAL (timeout()), &amp;loop;, SLOT (quit()) );
loop.connect(this, SIGNAL (seekableChanged(bool)), &amp;loop;, SLOT (quit()));
loop.exec();
}
}</code>


When files are loaded with this blocking load method <code>setPosition()</code> can be called right in the next source line.<br /><code>loadBlocking</code> blocks until signal <code>seekableChanged()</code> is emitted. To avoid a deadlock, maximum waiting time is 2 seconds.
When files are loaded with this blocking load method <code>setPosition()</code> can be called right in the next source line.
<code>loadBlocking</code> blocks until signal <code>seekableChanged()</code> is emitted. To avoid a deadlock, maximum waiting time is 2 seconds.

Revision as of 11:42, 25 February 2015


How to Seek in Sound File

I tried to play a sound file not at the beginning but with an offset of 20 seconds.

The following way of setting an offset fails:

player = new QMediaPlayer(this);
player->setMedia(QUrl::fromLocalFile("myCoolSong.mp3"));
player->setVolume(50);
player->play(); // music can be heard
qDebug() << player->isSeekable(); // is always false
player->setPosition ( 20*1000 ); //does not work

It does not work because QMediaPlayer works asynchronously.

As soon as the seek operations are delayed everything works as intended. For example:

CoolPlayer::on_Button1_clicked()
{
 qDebug() << player->isSeekable(); // true
 player->setPosition ( 20*1000 );
}

To make my life easier I have derived a class MediaPlayer from QMediaPlayer and added a blocking load method:

void MediaPlayer::loadBlocking(const QString &amp;file;)
{
 setMedia(QUrl::fromLocalFile(file));
 if(!isSeekable())
 {
 QEventLoop loop;
 QTimer timer;
 timer.setSingleShot(true);
 timer.setInterval(2000);
 loop.connect(&amp;timer;, SIGNAL (timeout()), &amp;loop;, SLOT (quit()) );
 loop.connect(this, SIGNAL (seekableChanged(bool)), &amp;loop;, SLOT (quit()));
 loop.exec();
 }
}

When files are loaded with this blocking load method

setPosition()

can be called right in the next source line.

loadBlocking

blocks until signal

seekableChanged()

is emitted. To avoid a deadlock, maximum waiting time is 2 seconds.