QT开发之音频

我们先使用QSound和QMovie类来分别做音频和动画学习,在下篇文章我们将采用Phonon多媒体框架.


先做好前期准备好,在Windos下开发的就可以跳过这一步了,在Linux环境下开发的就的执行下面操作.需要安装nas类.不然运行程序QSound::play()会没有声音的.

sudo apt-get install nas nas-bin

新建QT Gui应用,工程名为mySound,类默认.main.cpp默认就行,不做修改.

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QSound>

namespace Ui {
class MainWindow;
}

class QSound;

class MainWindow : public QMainWindow
{
    Q_OBJECT
    
public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    
private slots:
    void on_pushButton_clicked();

    void on_pushButton_2_clicked();

    void on_spinBox_valueChanged(int arg1);

private:
    Ui::MainWindow *ui;
    QSound *sound;
};

#endif // MAINWINDOW_H

上面代码看出我们设计界面添加了两个按钮分别是开始和停止,一个设置播放次数的.三个信号槽函数分别有这个三个按钮触发,定义了一个QSound类的私有对象指针sound


mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QSound>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    sound = new QSound("/home/linux/QT/project/mySound/sound.wav",this);
    qDebug() << sound->isAvailable();
}

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

void MainWindow::on_pushButton_clicked()
{
    sound->play();
    qDebug() << "loops:" << sound->loops() ;
}

void MainWindow::on_pushButton_2_clicked()
{
    sound->stop();
}

void MainWindow::on_spinBox_valueChanged(int value)
{
    sound->setLoops(value);
}

mainwndow.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>400</width>
    <height>300</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralWidget">
   <widget class="QPushButton" name="pushButton">
    <property name="geometry">
     <rect>
      <x>60</x>
      <y>150</y>
      <width>80</width>
      <height>25</height>
     </rect>
    </property>
    <property name="text">
     <string>播放</string>
    </property>
   </widget>
   <widget class="QPushButton" name="pushButton_2">
    <property name="geometry">
     <rect>
      <x>230</x>
      <y>150</y>
      <width>80</width>
      <height>25</height>
     </rect>
    </property>
    <property name="text">
     <string>停止</string>
    </property>
   </widget>
   <widget class="QSpinBox" name="spinBox">
    <property name="geometry">
     <rect>
      <x>280</x>
      <y>50</y>
      <width>55</width>
      <height>24</height>
     </rect>
    </property>
    <property name="minimum">
     <number>-1</number>
    </property>
    <property name="value">
     <number>1</number>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menuBar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>400</width>
     <height>22</height>
    </rect>
   </property>
  </widget>
  <widget class="QToolBar" name="mainToolBar">
   <attribute name="toolBarArea">
    <enum>TopToolBarArea</enum>
   </attribute>
   <attribute name="toolBarBreak">
    <bool>false</bool>
   </attribute>
  </widget>
  <widget class="QStatusBar" name="statusBar"/>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources/>
 <connections/>
</ui>



                                                                                                                                     文章出自:Linux_Google

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页