0

I am creating media player with qt5. Code below works fine ( it open file dialog, load one song and play it after loading) but it can't play some MP3 files. These files are not damaged or something like this, I can open them with other media players. I noticed that the most of them are larger than 20 mB. Please help me if you find the solution.

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFile>
#include <QFileDialog>
#include <QDebug>
#include <iostream>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect(&player, &QMediaPlayer::mediaStatusChanged,
            this, [&](QMediaPlayer::MediaStatus status){

        if(status == QMediaPlayer::LoadedMedia)    //play if loaded
            player.play();
    });
    connect(&player , QOverload<QMediaPlayer::Error>::of(&QMediaPlayer::error),   // notify about errors
        [=](QMediaPlayer::Error error){

        if (error==QMediaPlayer::ResourceError)
           qDebug("resource");
        if (error==QMediaPlayer::FormatError)

           qDebug("format");
        if (error==QMediaPlayer::AccessDeniedError)
            qDebug("acces");
        if (error==QMediaPlayer::ServiceMissingError)
            qDebug("service");

    });

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()

{   QString file= QFileDialog::getOpenFileName();  //select file
    player.setMedia(QUrl::fromLocalFile(file)); // load


}
Dushess
  • 1
  • 1
  • Maybe your "other players" are just more forgiving about semi-broken files.. – Jesper Juhl May 15 '19 at 20:17
  • Are all of the files that can't be played of the same extension? For example, does it play any OGG successfully, or none? None, some, or all of the MP3? The `mediaStatusChanged` and `error` signals can give you more information about failures, but the code above is ignoring all possible results except `QMediaPlayer::LoadedMedia`. –  May 15 '19 at 20:55
  • Player can play some mp3 files. I converted mp3 files that player cant play to wav and it played all of them correctly. – Dushess May 15 '19 at 21:15
  • I also didn't get any qt errors (FormatError,AccesDeniedError, ResourceError, ServiceMissingError) when opening those files. – Dushess May 15 '19 at 21:30

0 Answers0