2018-03-29 17:00:53 +00:00
|
|
|
/* This file is (c) 2018 Igor Kushnir <igorkuo@gmail.com>
|
|
|
|
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
|
|
|
|
|
|
|
|
#ifdef MAKE_QTMULTIMEDIA_PLAYER
|
|
|
|
|
|
|
|
#include <QByteArray>
|
2022-02-27 05:17:37 +00:00
|
|
|
#if (QT_VERSION < QT_VERSION_CHECK(6,0,0))
|
2018-03-29 17:00:53 +00:00
|
|
|
#include <QMediaContent>
|
2022-02-27 05:17:37 +00:00
|
|
|
#endif
|
2018-03-29 17:00:53 +00:00
|
|
|
#include "multimediaaudioplayer.hh"
|
|
|
|
|
2022-02-27 05:17:37 +00:00
|
|
|
MultimediaAudioPlayer::MultimediaAudioPlayer()
|
|
|
|
#if (QT_VERSION < QT_VERSION_CHECK(6,0,0))
|
|
|
|
: player( 0, QMediaPlayer::StreamPlayback )
|
|
|
|
#endif
|
2018-03-29 17:00:53 +00:00
|
|
|
{
|
|
|
|
typedef void( QMediaPlayer::* ErrorSignal )( QMediaPlayer::Error );
|
2022-02-27 05:17:37 +00:00
|
|
|
#if (QT_VERSION < QT_VERSION_CHECK(6,0,0))
|
2018-03-29 17:00:53 +00:00
|
|
|
connect( &player, static_cast< ErrorSignal >( &QMediaPlayer::error ),
|
|
|
|
this, &MultimediaAudioPlayer::onMediaPlayerError );
|
2022-02-27 05:17:37 +00:00
|
|
|
#else
|
|
|
|
player.setAudioOutput(&audioOutput);
|
|
|
|
|
|
|
|
connect( &player, &QMediaPlayer::errorChanged ,
|
|
|
|
this, &MultimediaAudioPlayer::onMediaPlayerError );
|
|
|
|
#endif
|
2018-03-29 17:00:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QString MultimediaAudioPlayer::play( const char * data, int size )
|
|
|
|
{
|
|
|
|
stop();
|
|
|
|
|
|
|
|
audioBuffer.setData( data, size );
|
|
|
|
if( !audioBuffer.open( QIODevice::ReadOnly ) )
|
|
|
|
return tr( "Couldn't open audio buffer for reading." );
|
2022-02-27 05:17:37 +00:00
|
|
|
#if (QT_VERSION >= QT_VERSION_CHECK(6,0,0))
|
|
|
|
player.setSourceDevice (&audioBuffer );
|
|
|
|
#else
|
2018-03-29 17:00:53 +00:00
|
|
|
player.setMedia( QMediaContent(), &audioBuffer );
|
2022-02-27 05:17:37 +00:00
|
|
|
#endif
|
2018-03-29 17:00:53 +00:00
|
|
|
player.play();
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MultimediaAudioPlayer::stop()
|
|
|
|
{
|
2022-02-27 05:17:37 +00:00
|
|
|
#if (QT_VERSION < QT_VERSION_CHECK(6,0,0))
|
2018-03-29 17:00:53 +00:00
|
|
|
player.setMedia( QMediaContent() ); // Forget about audioBuffer.
|
2022-02-27 05:17:37 +00:00
|
|
|
#endif
|
2018-03-29 17:00:53 +00:00
|
|
|
audioBuffer.close();
|
|
|
|
audioBuffer.setData( QByteArray() ); // Free memory.
|
|
|
|
}
|
|
|
|
|
|
|
|
void MultimediaAudioPlayer::onMediaPlayerError()
|
|
|
|
{
|
|
|
|
emit error( player.errorString() );
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // MAKE_QTMULTIMEDIA_PLAYER
|