goldendict-ng/src/multimediaaudioplayer.cc

65 lines
1.7 KiB
C++
Raw Normal View History

/* 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))
#include <QMediaContent>
2022-02-27 05:17:37 +00:00
#endif
#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
{
typedef void( QMediaPlayer::* ErrorSignal )( QMediaPlayer::Error );
2022-02-27 05:17:37 +00:00
#if (QT_VERSION < QT_VERSION_CHECK(6,0,0))
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
}
QString MultimediaAudioPlayer::play( const char * data, int size )
{
stop();
audioBuffer = new QBuffer();
audioBuffer->setData( data, size );
if( !audioBuffer->open( QIODevice::ReadOnly ) )
return tr( "Couldn't open audio buffer for reading." );
#if( QT_VERSION >= QT_VERSION_CHECK( 6, 0, 0 ) )
player.setSourceDevice( audioBuffer );
2022-02-27 05:17:37 +00:00
#else
player.setMedia( QMediaContent(), audioBuffer );
2022-02-27 05:17:37 +00:00
#endif
player.play();
return QString();
}
void MultimediaAudioPlayer::stop()
{
player.stop();
2022-02-27 05:17:37 +00:00
#if (QT_VERSION < QT_VERSION_CHECK(6,0,0))
player.setMedia( QMediaContent() ); // Forget about audioBuffer.
2022-02-27 05:17:37 +00:00
#endif
if( audioBuffer )
{
audioBuffer->close();
audioBuffer->setData( QByteArray() ); // Free memory.
audioBuffer.clear();
}
}
void MultimediaAudioPlayer::onMediaPlayerError()
{
emit error( player.errorString() );
}
#endif // MAKE_QTMULTIMEDIA_PLAYER