pymatio 0 Light Poster

I'm creating a media player with Qt & the phonon module:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <fstream>
#include <string.h>
#include <stdlib.h>
#include <QtGui>

void MainWindow::slotFinished(){
    int index = sources.indexOf(mediaObject->currentSource()) + 1;
    mediaObject->enqueue(sources.at(index));
    setLabelNowPlaying();
}

std::string ExtractFilename( const std::string& path )
{
  return path.substr( path.find_last_of( '/' ) +1 );
}


void MainWindow::setLabel(const QString &s){
    ui->label->setText(s);
    //qDebug() << "s = " << s;

}

void MainWindow::setLabelNowPlaying(){
    std::string stds;
    stds = "Now playing: ";
    std::string filename;
    filename = mediaObject->currentSource().fileName().toStdString();

    std::string file;

    file = ExtractFilename(filename);

    stds += file;
    setLabel( QString::fromStdString( stds ) );
}

void MainWindow::loadfromfile(std::string filename){
    try{
         std::ifstream file(filename.c_str());
         if (file.is_open()){
             char str[2000];
             bool firsttime = true;
             int index;
             while(!file.eof()){
                file.getline(str,2000);
                if (firsttime){
                    index = atoi(str);
                    if (index == -1){
                        std::cout << "ahh\n";
                        return;
                    }
                    firsttime = false;
                }else{
                    Phonon::MediaSource source(str);
                    sources.append(source);
                    std::cout << str << "\n";
                }
             }
             if (!firsttime){
                mediaObject->setCurrentSource(sources.at(index));
            }
        }
     }catch (int a){}
}

void MainWindow::saveToFile(std::string filename){
    std::ofstream file(filename.c_str());
    file << sources.indexOf(mediaObject->currentSource()) << "\n";
    foreach(Phonon::MediaSource s, sources){
        file << s.fileName().toStdString() << "\n";
    }
}

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindow)
{
     audioOutput = new Phonon::AudioOutput(Phonon::MusicCategory, this);
     mediaObject = new Phonon::MediaObject(this);
     metaInformationResolver = new Phonon::MediaObject(this);
     Phonon::createPath(mediaObject, audioOutput);
     connect(mediaObject, SIGNAL(aboutToFinish()), SLOT(slotFinished()));

     std::string home = getenv("HOME");
     home += "/.mped/default.session";
     std::cout << home << "\n";

     try{
        loadfromfile(home);
     }catch (int a){
     }

     ui->setupUi(this);
     statusBar()->showMessage("Imported default.session",7000);

}

void MainWindow::playPause()
{
    switch (mediaObject->state()){
        case Phonon::PlayingState:
            mediaObject->pause();
            ui->pushButtonPlay->setChecked(false);
            setLabel("Paused");
            break;
        case Phonon::PausedState:
            mediaObject->play();
            setLabelNowPlaying();
            break;
        case Phonon::StoppedState:
            mediaObject->play();
            setLabelNowPlaying();
            break;
        case Phonon::LoadingState:
            ui->pushButtonPlay->setChecked(false);
            setLabel("Paused");
            break;
    }
}

void MainWindow::addFiles()
{
    QStringList files = QFileDialog::getOpenFileNames(this, tr("Select Music Files"),
        QDesktopServices::storageLocation(QDesktopServices::MusicLocation));

    ui->pushButtonPlay->setChecked(false);
    if (files.isEmpty())
        return;
    int index = sources.size();
    foreach (QString string, files) {
            Phonon::MediaSource source(string);
            sources.append(source);
    }
    if (!sources.isEmpty()){
        metaInformationResolver->setCurrentSource(sources.at(0));
        mediaObject->setCurrentSource(metaInformationResolver->currentSource());

    }
}


void MainWindow::nextFile()
{

    int index = sources.indexOf(mediaObject->currentSource()) + 1;

    if (sources.size() > index) {
         mediaObject->stop();
         mediaObject->setCurrentSource(sources.at(index));
         mediaObject->play();
         setLabelNowPlaying();
     }
}

void MainWindow::lastFile(){
    int index = sources.indexOf(mediaObject->currentSource()) - 1;

    mediaObject->stop();
    try{
    mediaObject->setCurrentSource(sources.at(index));
    }catch(int e){}
    mediaObject->play();
    setLabelNowPlaying();
}

void MainWindow::shuffle(){
    mediaObject->stop();
    std::random_shuffle(sources.begin(), sources.end());
    mediaObject->setCurrentSource(sources.at(0));
    setLabel("Paused");
 }

void MainWindow::clear(){
    mediaObject->stop();
    sources.clear();
    setLabel("Paused");
}

void MainWindow::first(){
    mediaObject->stop();
    mediaObject->setCurrentSource(sources.at(0));
    mediaObject->play();
    setLabelNowPlaying();
}

void MainWindow::open(){
    QString filename = QFileDialog::getOpenFileName(this, tr("Select session file"),
        QDesktopServices::storageLocation(QDesktopServices::MusicLocation));
    if (filename == "" || filename == NULL){
        return;
    }
     loadfromfile(filename.toStdString());
     mediaObject->setCurrentSource(sources.at(0));
}

void MainWindow::save(){
    QString filename = QFileDialog::getOpenFileName(this, tr("Select session file"),
        QDesktopServices::storageLocation(QDesktopServices::MusicLocation));
    if (filename == "" || filename == NULL){
        return;
    }
    saveToFile(filename.toStdString());
}

void MainWindow::about(){
    QMessageBox::about(this,"About MPED",
                "MPED 0.1\n\n"
                "MPED was written by Matthew Hall <pymatio@googlemail.com>\n\n"
                "MPED is release under the GPL.\n");

}

void MainWindow::aboutToFinish()
{

    int index = sources.indexOf(mediaObject->currentSource()) + 1;

     if (sources.size() > index) {
         mediaObject->enqueue(sources.at(index));
     } else {
         ui->pushButtonPlay->setChecked(false);
     }

}

void MainWindow::finished()
{

         ui->pushButtonPlay->setChecked(false);
         setLabel("Paused");

}



MainWindow::~MainWindow()
{
    std::string home = getenv("HOME");
    home += "/.mped/default.session";
    std::ofstream file(home.c_str());
    file << sources.indexOf(mediaObject->currentSource()) << "\n";
    foreach(Phonon::MediaSource s, sources){
        file << s.fileName().toStdString() << "\n";
    }
    delete ui;
}

when run if you press the next button twice it stays on the same track, also it sometimes jumps to tracks along, why?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.