Информация об изменениях

Сообщение Не воспроизводится звук от 16.06.2020 6:52

Изменено 16.06.2020 6:53 arfaa

Не воспроизводится звук
Добрый день, пытаюсь проиграть с помощью Qt файл формата .mp3, но по какой-то причине он не проигрывается, не могу понять в чем проблема:
.pro
#-------------------------------------------------
#
# Project created by QtCreator 2020-03-13T13:49:57
#
#-------------------------------------------------

QT       += core
QT       += multimedia
QT -=gui
CONFIG += c++11
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = untitled
TEMPLATE = app

include(qtservice.pri)

# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your codehttps://rsdn.org/Forum/NewMsg.aspx?gid=100# away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.

# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0


SOURCES +=         main.cpp         mainwindow.cpp

HEADERS +=         mainwindow.h

FORMS +=         mainwindow.ui


main.cpp
#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{

    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}


mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QMediaPlayer>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
    QMediaPlayer * _player;

};

#endif // MAINWINDOW_H


mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    _player = new QMediaPlayer;
    QUrl file = QUrl::fromLocalFile(QFileDialog::getOpenFileName(this, tr("Open Music"), "", tr("")));
    if (file.url() == "")
        return ;
    _player->setMedia(file);
    _player->setVolume(50);
    _player->play();


}

MainWindow::~MainWindow()
{
    delete ui;
}
Не воспроизводится звук
Добрый день, пытаюсь проиграть с помощью Qt файл формата .mp3, но по какой-то причине он не проигрывается, не могу понять в чем проблема, выбираю в диалоговом окне файл формата .mp3:
.pro
#-------------------------------------------------
#
# Project created by QtCreator 2020-03-13T13:49:57
#
#-------------------------------------------------

QT       += core
QT       += multimedia
QT -=gui
CONFIG += c++11
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = untitled
TEMPLATE = app

include(qtservice.pri)

# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your codehttps://rsdn.org/Forum/NewMsg.aspx?gid=100# away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.

# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0


SOURCES += \
        main.cpp \
        mainwindow.cpp

HEADERS += \
        mainwindow.h

FORMS += \
        mainwindow.ui


main.cpp
#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{

    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}


mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QMediaPlayer>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
    QMediaPlayer * _player;

};

#endif // MAINWINDOW_H


mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    _player = new QMediaPlayer;
    QUrl file = QUrl::fromLocalFile(QFileDialog::getOpenFileName(this, tr("Open Music"), "", tr("")));
    if (file.url() == "")
        return ;
    _player->setMedia(file);
    _player->setVolume(50);
    _player->play();


}

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