1 /* This file is (c) 2018 Igor Kushnir <igorkuo@gmail.com>
2  * Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
3 
4 #ifdef MAKE_QTMULTIMEDIA_PLAYER
5 
6 #include <QByteArray>
7 #include <QMediaContent>
8 #include "multimediaaudioplayer.hh"
9 
MultimediaAudioPlayer()10 MultimediaAudioPlayer::MultimediaAudioPlayer() :
11   player( 0, QMediaPlayer::StreamPlayback )
12 {
13   typedef void( QMediaPlayer::* ErrorSignal )( QMediaPlayer::Error );
14   connect( &player, static_cast< ErrorSignal >( &QMediaPlayer::error ),
15            this, &MultimediaAudioPlayer::onMediaPlayerError );
16 }
17 
play(const char * data,int size)18 QString MultimediaAudioPlayer::play( const char * data, int size )
19 {
20   stop();
21 
22   audioBuffer.setData( data, size );
23   if( !audioBuffer.open( QIODevice::ReadOnly ) )
24     return tr( "Couldn't open audio buffer for reading." );
25 
26   player.setMedia( QMediaContent(), &audioBuffer );
27   player.play();
28   return QString();
29 }
30 
stop()31 void MultimediaAudioPlayer::stop()
32 {
33   player.setMedia( QMediaContent() ); // Forget about audioBuffer.
34   audioBuffer.close();
35   audioBuffer.setData( QByteArray() ); // Free memory.
36 }
37 
onMediaPlayerError()38 void MultimediaAudioPlayer::onMediaPlayerError()
39 {
40   emit error( player.errorString() );
41 }
42 
43 #endif // MAKE_QTMULTIMEDIA_PLAYER
44