播放使用了QIODevice(Qt4.6用VC ++)音频数据 [英] Play audio data using QIODevice (Qt4.6 with VC++)

查看:375
本文介绍了播放使用了QIODevice(Qt4.6用VC ++)音频数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用VC ++与QtMultimedia库中的音频流播放音频。由于我不是太与Qt的图书馆经历,我开始通过阅读.wav文件,并将其写入缓冲区:

I'm working on playing audio from an audio stream using VC++ with the QtMultimedia library. Since I'm not too experienced with Qt's libraries I started by reading in a .wav file and writing it to a buffer:

ifstream wavFile;
char* file = "error_ex.wav";
wavFile.open( file, ios::binary );

在这之后,我用的ifstream的.read()函数,所有的数据写入到缓冲区中。在缓冲区被写入后,它的被罚下到音频笔者,prepares它的Qt:

After that, I used ifstream's .read() function and write all the data into a buffer. After the buffer is written it's sent off to the audio writer that prepares it for Qt:

QByteArray fData;

for( int i = 0; i < (int)data.size(); ++i )
{
    fData.push_back(data.at(i));
}

m_pBuffer->open(QIODevice::ReadWrite);
m_pBuffer->write( fData );

m_pBuffer->close();

(m_pBuffer的类型是ΣQ缓冲的)

(m_pBuffer is of type QBuffer)

一旦ΣQ缓冲准备好我尝试播放缓冲:

Once the QBuffer is ready I attempt to play the buffer:

QIODevice* ioDevice = m_pAudioOut->start();
ioDevice->write( m_pBuffer->buffer() );

(m_pAudioOut的类型是QAudioOutput的)

(m_pAudioOut is of type QAudioOutput)

这导致从扬声器小流行,然后停止播放。任何想法,为什么?

This results in a small pop from the speakers and then it stops playing. Any ideas why?

使用Qt库4.6.3在Windows XP SP2上运行的视觉工作室2008年。

Running Visual Studios 2008 on Windows XP SP2 using Qt library 4.6.3.

推荐答案

弗兰克指出,如果你的要求是简单地从一个文件播放音频数据,更高级别的API会做的作业,可以简化您的应用程序code。 声子将是一个选项;此外,该项目将QtMobility提供 QMediaPlayer API,用于高层次的用例。

As Frank pointed out, if your requirement is simply to play audio data from a file, a higher-level API would do the job, and would simplify your application code. Phonon would be one option; alternatively, the QtMobility project provides the QMediaPlayer API for high-level use cases.

由于这个问题是专门关于使用的QIODevice 然而,那你提到阅读从WAV文件只是你的初使的方法,我会假设你确实需要一个流API,即其中一个允许客户端来控制缓冲,而不是交给这个控件到更高层次的抽象,如声子。

Given that the question is specifically about using QIODevice however, and that you mentioned that reading from a WAV file was just your intitial approach, I'll assume that you actually need a streaming API, i.e. one which allows the client to control the buffering, rather than handing over this control to a higher-level abstraction such as Phonon.

QAudioOutput 的可以在两种不同模式下使用,这取决于<$的c超载$ C>开始()正所谓:

QAudioOutput can be used in two different modes, depending on which overload of start() is called:

在此模式下,QAudioOutput将拉动从提供的QIODevice数据,而不从客户端进一步干预。这是一个很好的选择,如果正在使用的QIODevice是其中之一是由Qt的(如: QFile时提供 QAbstractSocket 等)。

In this mode, QAudioOutput will pull data from the supplied QIODevice without further intervention from the client. It is a good choice if the QIODevice being used is one which is provided by Qt (e.g. QFile, QAbstractSocket etc).

推模式: 的QIODevice * QAudioOutput ::开始()

在这种模式下,客户端QAudioOutput必须通过调用推送模式,音频设备的QIODevice ::写()。这将需要在一个循环做,是这样的:

In this mode, the QAudioOutput client must push mode to the audio device by calling QIODevice::write(). This will need to be done in a loop, something like:

qint64 dataRemaining = ... // assign correct value here
while (dataRemaining) {
    qint64 bytesWritten = audioOutput->write(buffer, dataRemaining);
    dataRemaining -= bytesWritten;
    buffer += bytesWritten;
    // Then wait for a short time
}

如何等待实现将取决于应用程序的情况下 - 如果声音是从一个专门的线程编写的,它可以简单的睡眠()。另外,如果声音是从主线程写的,你可能会想在写一个 QTimer被触发

How the wait is implemented will depend on the context of your application - if audio is being written from a dedicated thread, it could simply sleep(). Alternatively, if audio is being written from the main thread, you will probably want the write to be triggered by a QTimer.

既然你不提关于使用周围的写入(一个循环任何东西)在你的应用程序调用,它看起来像正在发生的事情是,你写数据的短段(其中扮演一个弹出),然后穿上未写了。

Since you don't mention anything about using a loop around the write() calls in your app, it looks like what is happening is that you write a short segment of data (which plays as a pop), then don't write any more.

您可以使用它使用Qt提供的例子/多媒体/ audiooutput应用这两种模式见code。

You can see code using both modes in the examples/multimedia/audiooutput app which is delivered with Qt.

这篇关于播放使用了QIODevice(Qt4.6用VC ++)音频数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆