Pencil2D Animation
Download Community News Docs Contribute
  • Overview
  • Articles
  • Code
  •  
  • Class List
  • Class Index
  • Class Hierarchy
  • Class Members
  • File List
Loading...
Searching...
No Matches
  • core_lib
  • src
soundplayer.cpp
1/*
2
3Pencil2D - Traditional Animation Software
4Copyright (C) 2012-2020 Matthew Chiawen Chang
5
6This program is free software; you can redistribute it and/or
7modify it under the terms of the GNU General Public License
8as published by the Free Software Foundation; version 2 of the License.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15*/
16
17#include "soundplayer.h"
18#include <QAudioOutput>
19#include <QMediaPlayer>
20#include <QFile>
21#include <QDebug>
22#include "soundclip.h"
23#include "util.h"
24
25SoundPlayer::SoundPlayer()
26{
27}
28
29SoundPlayer::~SoundPlayer()
30{
31#ifdef Q_OS_WIN
32 // Qt Multimedia's DirectShow backend segfaults when it is destroyed while paused
33 stop();
34#endif
35}
36
37void SoundPlayer::init(SoundClip* clip)
38{
39 Q_ASSERT(clip != nullptr);
40 mSoundClip = clip;
41
42 mMediaPlayer = new QMediaPlayer(this);
43
44 QFile file(clip->fileName());
45 file.open(QIODevice::ReadOnly);
46
47 mBuffer.setData(file.readAll());
48 mBuffer.open(QBuffer::ReadOnly);
49
50#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
51 mMediaPlayer->setAudioOutput(new QAudioOutput(this));
52 mMediaPlayer->setSourceDevice(&mBuffer, QUrl::fromLocalFile(clip->fileName()));
53#else
54 mMediaPlayer->setMedia(QUrl::fromLocalFile(clip->fileName()), &mBuffer);
55#endif
56 makeConnections();
57
58 clip->attachPlayer(this);
59}
60
61void SoundPlayer::onKeyFrameDestroy(KeyFrame* keyFrame)
62{
63 Q_UNUSED(keyFrame)
64}
65
66bool SoundPlayer::isValid()
67{
68 if (mMediaPlayer)
69 {
70 return (mMediaPlayer->error() == QMediaPlayer::NoError);
71 }
72 return false;
73}
74
75void SoundPlayer::play()
76{
77 if (mMediaPlayer)
78 {
79 mMediaPlayer->play();
80 }
81}
82
83void SoundPlayer::pause()
84{
85 if (mMediaPlayer)
86 {
87 mMediaPlayer->pause();
88 }
89}
90
91void SoundPlayer::stop()
92{
93 if (mMediaPlayer)
94 {
95 mMediaPlayer->stop();
96 }
97}
98
99int64_t SoundPlayer::duration()
100{
101 if (mMediaPlayer)
102 {
103 return mMediaPlayer->duration();
104 }
105 return 0;
106}
107
108void SoundPlayer::setMediaPlayerPosition(qint64 pos)
109{
110 if (mMediaPlayer)
111 {
112 mMediaPlayer->setPosition(pos);
113 }
114}
115
116void SoundPlayer::makeConnections()
117{
118#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
119 connect(mMediaPlayer, &QMediaPlayer::errorOccurred, this, [](QMediaPlayer::Error err, const QString&)
120#else
121 auto errorSignal = static_cast<void (QMediaPlayer::*)(QMediaPlayer::Error)>(&QMediaPlayer::error);
122 connect(mMediaPlayer, errorSignal, this, [](QMediaPlayer::Error err)
123#endif
124 {
125 qDebug() << "MediaPlayer Error: " << err;
126 });
127
128 connect(mMediaPlayer, &QMediaPlayer::durationChanged, [this](qint64 duration)
129 {
130 qDebug() << "MediaPlayer durationChanged :" << duration;
131 emit durationChanged(this, duration);
132 });
133}
KeyFrame
Definition: keyframe.h:30
SoundClip
Definition: soundclip.h:27
QBuffer::open
virtual bool open(QIODevice::OpenMode flags) override
QBuffer::setData
void setData(const QByteArray &data)
QFile
QIODevice::ReadOnly
ReadOnly
QObject::connect
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QString
QUrl::fromLocalFile
QUrl fromLocalFile(const QString &localFile)
Generated on Thu May 8 2025 04:47:53 for Pencil2D by doxygen 1.9.6 based on revision 4513250b1d5b1a3676ec0e67b06b7a885ceaae39