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 
#include 

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 
#include 

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



 MainWindow
 
  
   
    0
    0
    400
    300
   
  
  
   MainWindow
  
  
   
    
     
      60
      150
      80
      25
     
    
    
     播放
    
   
   
    
     
      230
      150
      80
      25
     
    
    
     停止
    
   
   
    
     
      280
      50
      55
      24
     
    
    
     -1
    
    
     1
    
   
  
  
   
    
     0
     0
     400
     22
    
   
  
  
   
    TopToolBarArea
   
   
    false
   
  
  
 
 
 
 


QT开发之音频_第1张图片


                                                                                                                                     文章出自:Linux_Google

你可能感兴趣的:(QT)