[SOLVED] Media Player Start/Pause/Stop



  • Hello, I am playing around with Qt and trying to create Media Player. So my problem is when I start playing a song, I can't stop it. I don't know how to stop it nor pause it. Any suggestions how to do that??? I guess threads??? Or some other way?

    Regards

    This is how my source for play/stop looks like... So far it only disable a button, but not also stop playing a song...

    @void MainWindow::on_play_clicked()
    {

    QMediaPlayer *player = new QMediaPlayer(this);
    player->setMedia(QUrl::fromLocalFile("Made in the USA.mp3"));
    player->setVolume(100);
    player->play();
    ui->Play->setEnabled(false);
    ui->Stop->setEnabled(true);
    

    }

    void MainWindow::on_Stop_clicked()
    {

    ui->Stop->setEnabled(false);
    ui->Play->setEnabled(true);
    

    }
    @


  • Lifetime Qt Champion

    Hi,

    Have a look at the "Media Player Example":http://qt-project.org/doc/qt-5.0/qtmultimedia/multimediawidgets-player.html it shows what you're looking for



  • Thanks ! Fixed.



  • Hey, another problem... I am trying to add PlayList and It add list in my listWidget, but it won't play it... I know problem is in last two comment lines but don't know how to solve that... If I uncomment that, it will say "playlist not defined"

    @MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    QMediaPlayer *player = new QMediaPlayer(this);
    QMediaPlaylist *playlist = new QMediaPlaylist(player);

    player->setPlaylist(playlist);
    
    player->setVolume(35);
    playlist->addMedia(playlist);
    
    
    
    connect(ui->Play, SIGNAL(clicked()), player, SLOT(play()));
    connect(ui->Pause, SIGNAL(clicked()), player, SLOT(pause()));
    connect(ui->Stop, SIGNAL(clicked()), player, SLOT(stop()));
    connect(ui->Volume, SIGNAL(sliderMoved(int)), player, SLOT(setVolume(int)));
    

    }@

    @void MainWindow::on_Playlist_clicked()
    {
    QString directory = QFileDialog::getExistingDirectory(this,tr("Select dir for files to import"));
    if(directory.isEmpty())
    return;
    QDir dir(directory);
    QStringList files = dir.entryList(QStringList() << "*.mp3",QDir::Files);
    QList<QMediaContent> content;
    for(const QString& f:files)
    {
    content.push_back(QUrl::fromLocalFile(dir.path()+"/" + f));
    QFileInfo fi(f);
    ui->listWidget->addItem(fi.fileName());
    }
    //ui->listWidget->setCurrentRow(playlist->currentIndex() != -1? playlist->currentIndex():0);
    // playlist->addMedia(content);
    }
    @



  • Sloved !


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.