Qt game development necessary! Play QtMultimedia format music with Ogg

 

QtGame development essential! UseQtMultimediaPlayOggFormat music

 

QtMultimediaIsQt4.6A new layer of audio and video is proposed. The aim is to provide a more complete video and audio control for developers, while not losing the advantages of platform independence. WhileOggIs a good open source multimedia container, can accommodate a variety of encoding format content, and we are most familiar withVorbisEncoding format. This time I will tryQtMultimediaNext playOggVorbisMusic, while playing music in the game is essential.

Download the source code and demo program:Here

My development environment:

Gcc4.4 QtSDK + 4.8.1 QtCreator2.6 + Ubuntu+

MinGW4.4 QtSDK + 4.8.3 QtCreator2.6 + Windows8+

BecauseOggVorbisThe nature of the open source, leading us to be personally decoded. OnlineOggVery few decoding of the content, I am also a combination of game audio program design-Beginning.Game.Audio.Programming"AndOggThe document is rightOggVorbisFormat has some understanding. First need to download"Libogg-1.3.0"And"Libvorbis-1.3.2"These two software development kits. Can beXiph.org(onHttp://www.xiph.org/Get these two software development kit (also can download my source code, that comes with theOggAndVorbisSource code and project files). And then, the software development kit comes with it.VisualStudioProject files, and I use theQtCreator2.6So I was readingVisualStudioAfter the project file to write a.proFile to generateOggAndVorbisStatic library. You can download the relevant project files to my resources. (blog is authorized by Intel, the original blog address: http://prog3.com/sbdm/blog/jiangcaiyang123)

Build upLibOgg,LibVorbisAndLibVorbisFileThree static libraries, we can use the existing library functions to build our experiments.

Here is what I defineQOggVorbisClass declaration:

#ifndef _QOGGVORBIS_H_
#define _QOGGVORBIS_H_
#include <QObject>
#include <QString>
#include <QStringList>
#define DECLRARE_PROPERTY_WITH_GETTERS( AType, AProperty )
 Private:\
 AType M_ ## AProperty; Public:\
 AType AProperty( Void ) { Return M_ ## AProperty; }
 
Struct OggVorbis_File;/ / forward declaration
 
Class QOggVorbis: Public QObject
{
 Q_OBJECT
 Q_PROPERTY( Quint16 AudioFormat READ AudioFormat )
 Q_PROPERTY( Quint16 Channels READ Channels )
 Q_PROPERTY( Quint32 SampleRate READ SampleRate )
 Q_PROPERTY( Quint32 ByteRate READ ByteRate )
 Q_PROPERTY( Quint16 BlockAlign READ BlockAlign )
 Q_PROPERTY( Quint16 BitsPerSample READ BitsPerSample )
 Q_PROPERTY( QStringList UserComments READ UserComments )
 Q_PROPERTY( QString Vendor READ Vendor )
 Q_PROPERTY( Qint8* Data READ Data )
 Q_PROPERTY( Quint32 DataSize READ DataSize )
Public:
 QOggVorbis( Void );
 QOggVorbis( Const QString& FileName );
 ~QOggVorbis( Void );
 Bool Load( Const QString& FileName );
 Void Clear( Void );
Private:
 Bool GetComment( OggVorbis_File* VF );
 Bool Decode( OggVorbis_File* VF );
 DECLRARE_PROPERTY_WITH_GETTERS( Quint16, audioformat 
 declrare_property_with_getters quint16 渠道 
 declrare_property_with_getters quint32 采样率 / /也就是频率频率
 declrare_property_with_getters quint32 byterate 
 declrare_property_with_getters quint16 blockalign 
 declrare_property_with_getters quint16 频率 / /也就是采样率
 declrare_property_with_getters QStringList 用户的意见 
 declrare_property_with_getters QString 供应商 
 declrare_property_with_getters qint8*, 数据 
 declrare_property_with_getters quint32 数据大小 
};
# endif / / _qoggvorbis_h_

这里使用q_property宏来对这个类进行商务部,可以通过setproperty()函数和getproperty()函数来获得成员的值,而declrare_property_with_getters是一个自定义的宏,用来定义一个数据成员和一个吸气剂由于。QT中有些类(如QString是隐式共享()implicitsharing的,返回变量还是它的引用都没有关系一些私有的成员函数由于传值需要用到)。oggvorbis_file结构,而又不想破坏它的封装性,只有先前向声明oggvorbis_file结构,再将结构的指针作为参数进行传递。

进行QtMultimedia的编程,需要使用qaudioformatqaudiodeviceinfoqaudiooutput这三个类首先用。qaudioformat设置音频的格式,然后用这种格式来匹配qaudiodeviceinfo,随后利用qaudioformatqaudiodeviceinfo的信息来创建qaudiooutput的对象最后利用。qaudiooutput的对象(或对象指针进行播放下面是相关的代码):

/ / main.cpp主函数所在的空间
1 / 2013年月21日19:33:17通过jiangcaiyang
#包括 qcoreapplication > <
#包括 qbuffer > <
#包括 < QtMultimedia >
#包括 qtdebug > <
#包括 “qoggvorbis。”
等级 testaudio
{
公共
 testaudio 无效 
 {
 }
 ~testaudio 无效 
 {
 释放 );
 }
 布尔 loadoggfile const QString& 文件名 
 {
 如果  m_oggvorbis负荷 文件名   返回 
 quint32 采样率 = m_oggvorbis采样率 );
 quint16 渠道 = m_oggvorbis渠道 );
 quint16 样本 = m_oggvorbis频率 );
 QStringList 评论 = m_oggvorbis用户的意见 );
 qlistiterator<QString> itercmts 评论 );
 快速诊断调试程序  < “Ogg文件的信息:“ <
 “[采样率:” < 采样率 <
 [频道] < 渠道 <
 “[样本]:” < 样本 <
 ]”
 / /显示OGG文件额外信息
 快速诊断调试程序  < “Ogg的评论:“
   itercmtshasNext  
 {
 快速诊断调试程序  < itercmts下一个 );
 }
 / /设置音频格式
 m_formatsetsamplerate 采样率 );
 m_formatsetchannelcount 渠道 );
 m_formatsetsamplesize 样本 );
 m_formatsetcodec “音频PCM” );
 m_formatsetbyteorder qaudioformat::littleendian );
 m_formatsetsampletype qaudioformat::signedint );
 / /初始化音频设备
 m_deviceinfo = qaudiodeviceinfo::defaultoutputdevice );
 如果  m_deviceinfoisformatsupported m_format  
 {
 快速诊断调试程序  < “无法支持这种格式,尝试相应的格式。”
 m_format = m_deviceinfonearestformat m_format );
 }
 m_bufferSetData const 字符*)m_oggvorbis数据 ),
 m_oggvorbis数据大小  );
 m_poutput = 新的 qaudiooutput m_deviceinfo m_format 0 );
 返回 真的
 }
 无效  无效 
 {
 m_buffer正常开放 qiodevice::只读 );
 m_poutput•>起点 &m_buffer );
 }
 无效 释放 无效 
 {
 m_buffer关闭 );
 删除 m_poutput
 m_poutput = 0
 }
私人
 qaudioformat m_format
 qaudiodeviceinfo m_deviceinfo
 qoggvorbis m_oggvorbis
 qaudiooutput* m_poutput
 qbuffer m_buffer
};
国际的 主要国际的 argc 字符* argv[ ] 
{
 qcoreapplication 一个 argc argv );
 / /读取并且播放
 testaudio testaudio
 如果  testaudioloadoggfile “/ testsound OGG格式。”   返回 1
 快速诊断调试程序  < “读测试成功!\ '
 testaudio );
 快速诊断调试程序  < “现在播放音频!\ '
 返回 一个exec();
}
有关编译器优化的更完整信息,请参阅优化通知