All Classes Namespaces Functions Variables Enumerations Properties Pages
soundplayer.cpp
1 /*
2 
3 Pencil2D - Traditional Animation Software
4 Copyright (C) 2012-2020 Matthew Chiawen Chang
5 
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; version 2 of the License.
9 
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14 
15 */
16 
17 #include "soundplayer.h"
18 #include <QMediaPlayer>
19 #include <QBuffer>
20 #include <QFile>
21 #include "soundclip.h"
22 
23 SoundPlayer::SoundPlayer( )
24 {
25 
26 }
27 
28 SoundPlayer::~SoundPlayer()
29 {
30 }
31 
32 void SoundPlayer::init( SoundClip* clip )
33 {
34  Q_ASSERT( clip != nullptr );
35  mSoundClip = clip;
36 
37  mMediaPlayer = new QMediaPlayer( this );
38 
39  QFile file(clip->fileName());
41  QBuffer *buffer = new QBuffer();
42  buffer->setData(file.readAll());
43  buffer->open(QBuffer::ReadOnly);
44 
45  mMediaPlayer->setMedia( QUrl::fromLocalFile( clip->fileName() ), buffer );
46  makeConnections();
47 
48  clip->attachPlayer( this );
49  //mMediaPlayer->play();
50 
51  qDebug() << "Seekable = " << mMediaPlayer->isSeekable();
52 }
53 
54 void SoundPlayer::onKeyFrameDestroy( KeyFrame* keyFrame )
55 {
56  Q_UNUSED(keyFrame)
57 }
58 
59 bool SoundPlayer::isValid()
60 {
61  if ( mMediaPlayer )
62  {
63  return ( mMediaPlayer->error() == QMediaPlayer::NoError );
64  }
65  return false;
66 }
67 
68 void SoundPlayer::play()
69 {
70  if ( mMediaPlayer )
71  {
72  mMediaPlayer->play();
73  }
74 }
75 
76 void SoundPlayer::pause()
77 {
78  if ( mMediaPlayer )
79  {
80  mMediaPlayer->pause();
81  }
82 }
83 
84 void SoundPlayer::stop()
85 {
86  if ( mMediaPlayer )
87  {
88  mMediaPlayer->stop();
89  }
90 }
91 
92 int64_t SoundPlayer::duration()
93 {
94  if ( mMediaPlayer )
95  {
96  return mMediaPlayer->duration();
97  }
98  return 0;
99 }
100 
101 void SoundPlayer::setMediaPlayerPosition(qint64 pos)
102 {
103  if( mMediaPlayer )
104  {
105  mMediaPlayer->setPosition(pos);
106  }
107 }
108 
109 void SoundPlayer::makeConnections()
110 {
111  auto errorSignal = static_cast< void ( QMediaPlayer::* )( QMediaPlayer::Error ) >( &QMediaPlayer::error );
112  connect( mMediaPlayer, errorSignal, this, []( QMediaPlayer::Error err )
113  {
114  qDebug() << "MediaPlayer Error: " << err;
115  } );
116 
117  connect( mMediaPlayer, &QMediaPlayer::durationChanged, [ this ]( qint64 duration )
118  {
119  qDebug() << "MediaPlayer durationChanged :" << duration;
120  emit durationChanged( this, duration );
121  } );
122 }
void setData(const QByteArray &data)
virtual bool open(QIODevice::OpenMode flags) override
virtual bool open(QIODevice::OpenMode mode) override
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QUrl fromLocalFile(const QString &localFile)