Pencil2D  ff90c0872e88be3bf81c548cd60f01983012ec49
Pencil2D is an animation software for both bitmap and vector graphics. It is free, multi-platform, and open source.
 All Classes Functions
soundplayer.cpp
1 /*
2 
3 Pencil - Traditional Animation Software
4 Copyright (C) 2012-2017 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 "soundclip.h"
20 
21 SoundPlayer::SoundPlayer( )
22 {
23 
24 }
25 
26 SoundPlayer::~SoundPlayer()
27 {
28 }
29 
30 void SoundPlayer::init( SoundClip* clip )
31 {
32  Q_ASSERT( clip != nullptr );
33  mSoundClip = clip;
34 
35  mMediaPlayer = new QMediaPlayer( this );
36  mMediaPlayer->setMedia( QUrl::fromLocalFile( clip->fileName() ) );
37  makeConnections();
38 
39  clip->attachPlayer( this );
40  //mMediaPlayer->play();
41 
42  qDebug() << "Seekable = " << mMediaPlayer->isSeekable();
43 }
44 
45 void SoundPlayer::onKeyFrameDestroy( KeyFrame* keyFrame )
46 {
47  Q_UNUSED(keyFrame)
48 }
49 
50 bool SoundPlayer::isValid()
51 {
52  if ( mMediaPlayer )
53  {
54  return ( mMediaPlayer->error() == QMediaPlayer::NoError );
55  }
56  return false;
57 }
58 
59 void SoundPlayer::play()
60 {
61  if ( mMediaPlayer )
62  {
63  mMediaPlayer->play();
64  }
65 }
66 
67 void SoundPlayer::stop()
68 {
69  if ( mMediaPlayer )
70  {
71  mMediaPlayer->stop();
72  }
73 }
74 
75 int64_t SoundPlayer::duration()
76 {
77  if ( mMediaPlayer )
78  {
79  return mMediaPlayer->duration();
80  }
81  return 0;
82 }
83 
84 void SoundPlayer::setMediaPlayerPosition(qint64 pos)
85 {
86  if( mMediaPlayer )
87  {
88  mMediaPlayer->setPosition(pos);
89  }
90 }
91 
92 void SoundPlayer::makeConnections()
93 {
94  auto errorSignal = static_cast< void ( QMediaPlayer::* )( QMediaPlayer::Error ) >( &QMediaPlayer::error );
95  connect( mMediaPlayer, errorSignal, this, [ this ]( QMediaPlayer::Error err )
96  {
97  qDebug() << "MediaPlayer Error: " << err;
98  } );
99 
100  connect( mMediaPlayer, &QMediaPlayer::durationChanged, [ this ]( qint64 duration )
101  {
102  qDebug() << "MediaPlayer durationChanged :" << duration;
103  emit durationChanged( this, duration );
104  } );
105 }