Qt 3D开发案例:音频可视化器示例(附源码)
Qt自带集成开发环境(IDE),名为Qt Creator。它可以在Linux、OS X和Windows上运行,并提供智能代码完成、语法高亮、集成帮助系统、调试器和剖析器集成,还集成了所有主要的版本控制系统(如git、Bazaar)。除了Qt Creator外,Windows上的开发人员还可以使用Qt的Visual Studio插件。也可以使用其他的IDE(如KDE上的KDevelop)。但当然绝不是必须使用任何IDE。
前往慧都网下载Qt6最新试用版
演示结合了Qt 3D渲染和Qt Quick 2元素。

Audio Visualizer演示了如何实现将Qt 3D渲染与Qt Quick 2D元素结合使用的应用程序。该示例使用媒体播放器播放音乐,并将音乐的大小可视化为动画条。
运行示例
要从Qt Creator运行示例,请打开“Welcome”模式,然后从“demo”中选择demo。
Qt Quick 2D实施
该audio-visualizer-qml/main.qml示例中的Qt快速实现MediaPlayer用于播放音频内容。
-
MediaPlayer {
-
id: mediaPlayer
-
autoPlay: true
-
volume: 0.5
-
source: "qrc:/music/tiltshifted_lost_neon_sun.mp3"
播放器由playButton和c {stopButton}控制。基于点击的按钮state的mainview变化。
使用该Scene3D类型渲染3D内容。音频可视化器的状态保留在中mainview。它会传递给visualizerbar动画所需的。
Scene3D { anchors.fill: parent Visualizer { id: visualizer animationState: mainview.state numberOfBars: 120 barRotationTimeMs: 8160 // 68 ms per bar } }
Qt 3D实施
该示例的3D元素在中创建audio-visualizer-qml/Visualizer.qml。
-
Camera {
-
id: camera
-
projectionType: CameraLens.PerspectiveProjection
-
fieldOfView: 45
-
aspectRatio: 1820 / 1080
-
nearPlane: 0.1
-
farPlane: 1000.0
-
position: Qt.vector3d(0.014, 0.956, 2.178)
-
upVector: Qt.vector3d(0.0, 1.0, 0.0)
-
viewCenter: Qt.vector3d(0.0, 0.7, 0.0)
-
}
ANodeInstantiator用于创建使音乐大小可视化的小节。
-
// Bars
-
CuboidMesh {
-
id: barMesh
-
xExtent: 0.1
-
yExtent: 0.1
-
zExtent: 0.1
-
}
-
-
NodeInstantiator {
-
id: collection
-
property int maxCount: parent.numberOfBars
-
model: maxCount
-
-
delegate: BarEntity {
-
id: cubicEntity
-
entityMesh: barMesh
-
rotationTimeMs: sceneRoot.barRotationTimeMs
-
entityIndex: index
-
entityCount: sceneRoot.numberOfBars
-
entityAnimationsState: animationState
-
magnitude: 0
-
}
-
}
该visualizer还包含一个Entity显示进度。该元素具有曲线形状的网格,并根据播放曲目的持续时间在一个级别上旋转以显示进度。
-
-
Mesh {
-
id: progressMesh
-
source: "qrc:/meshes/progressbar.obj"
-
}
-
-
Transform {
-
id: progressTransform
-
property real defaultStartAngle: -90
-
property real progressAngle: defaultStartAngle
-
rotationY: progressAngle
-
}
-
-
Entity {
-
property Material progressMaterial: PhongMaterial {
-
ambient: "#80C342"
-
diffuse: "black"
-
}
-
-
components: [progressMesh, progressMaterial, progressTransform]
-
}
在audio-visualizer-qml/BarEntity.qml其中有用于旋转条形和更改条形颜色的动画。这些条在遵循环形的水平上旋转。同时,条的颜色被设置为动画。
-
QQ2.NumberAnimation {
-
id: angleAnimation
-
target: angleTransform
-
property: "barAngle"
-
duration: rotationTimeMs
-
loops: QQ2.Animation.Infinite
-
running: true
-
from: startAngle
-
to: 360 + startAngle
-
}
-
QQ2.SequentialAnimation on barColor {
-
id: barColorAnimations
-
running: false
-
-
QQ2.ColorAnimation {
-
from: lowColor
-
to: highColor
-
duration: animationDuration
-
}
-
-
QQ2.PauseAnimation {
-
duration: animationDuration
-
}
-
-
QQ2.ColorAnimation {
-
from: highColor
-
to: lowColor
-
duration: animationDuration
-
}
-
}
每个小节的大小都从一个单独的.raw文件中读取,该文件基于正在播放的曲目。当小节围绕环旋转时,高度将缩放以突出显示当前播放的位置。旋转一整圈后,将获取一个新值。
Qt相关组件:
- QtitanRibbon: 遵循Microsoft Ribbon UI Paradigm for Qt技术的Ribbon UI组件,致力于为Windows、Linux和Mac OS X提供功能完整的Ribbon组件。
- QtitanChart :是一个C ++库,代表一组控件,这些控件使您可以快速地为应用程序提供漂亮而丰富的图表。并且支持所有主要的桌面操作系统。
- QtitanDataGrid :这个Qt数据网格组件使用纯C++创建,运行速度极快,处理大数据和超大数据集的效果突出。QtitanDataGrid完全集成了QtDesigner,因而极易适应其他相似的开发环境,保证100%兼容Qt GUI。
浙公网安备 33010602011771号