mirror of
https://github.com/xiaoyifang/goldendict-ng.git
synced 2024-11-23 20:14:05 +00:00
opt: optimize the audio auto pronounciation logic (#1846)
* opt: optimize the audio play logic
This commit is contained in:
parent
b39397a544
commit
426950fc10
|
@ -1141,6 +1141,53 @@ void ArticleView::openLink( QUrl const & url, QUrl const & ref, QString const &
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
void ArticleView::playAudio( QUrl const & url )
|
||||
{
|
||||
audioPlayer->stop();
|
||||
qDebug() << "play audio,the link url:" << url;
|
||||
|
||||
if ( url.scheme() == "bres" || url.scheme() == "gdau" || url.scheme() == "gdvideo"
|
||||
|| Utils::Url::isAudioUrl( url ) ) {
|
||||
// Download it
|
||||
|
||||
if ( Utils::Url::isWebAudioUrl( url ) ) {
|
||||
sptr< Dictionary::DataRequest > req = std::make_shared< Dictionary::WebMultimediaDownload >( url, articleNetMgr );
|
||||
|
||||
connect( req.get(), &Dictionary::Request::finished, this, [ req, this ]() {
|
||||
audioDownloadFinished( req );
|
||||
} );
|
||||
}
|
||||
else if ( url.scheme() == "gdau" ) {
|
||||
// Since searches should be limited to current group, we just do them
|
||||
// here ourselves since otherwise we'd need to pass group id to netmgr
|
||||
// and it should've been having knowledge of the current groups, too.
|
||||
|
||||
sptr< Dictionary::Class > dict = dictionaryGroup->getDictionaryById( url.host().toStdString() );
|
||||
|
||||
if ( dict ) {
|
||||
try {
|
||||
sptr< Dictionary::DataRequest > req = dict->getResource( url.path().mid( 1 ).toUtf8().data() );
|
||||
|
||||
if ( !req->isFinished() ) {
|
||||
// Queued loading
|
||||
connect( req.get(), &Dictionary::Request::finished, this, [ req, this ]() {
|
||||
audioDownloadFinished( req );
|
||||
} );
|
||||
}
|
||||
else {
|
||||
// Immediate loading
|
||||
audioDownloadFinished( req );
|
||||
}
|
||||
}
|
||||
catch ( std::exception & e ) {
|
||||
emit statusBarMessage( tr( "ERROR: %1" ).arg( e.what() ), 10000, QPixmap( ":/icons/error.svg" ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ResourceToSaveHandler * ArticleView::saveResource( const QUrl & url, const QString & fileName )
|
||||
{
|
||||
ResourceToSaveHandler * handler = new ResourceToSaveHandler( this, fileName );
|
||||
|
@ -1787,6 +1834,28 @@ void ArticleView::resourceDownloadFinished()
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
void ArticleView::audioDownloadFinished( const sptr< Dictionary::DataRequest > & req )
|
||||
{
|
||||
if ( req->dataSize() >= 0 ) {
|
||||
// Ok, got one finished, all others are irrelevant now
|
||||
qDebug() << "audio download finished. Playing...";
|
||||
vector< char > const & data = req->getFullData();
|
||||
|
||||
// Audio data
|
||||
audioPlayer->stop();
|
||||
connect( audioPlayer.data(),
|
||||
&AudioPlayerInterface::error,
|
||||
this,
|
||||
&ArticleView::audioPlayerError,
|
||||
Qt::UniqueConnection );
|
||||
QString errorMessage = audioPlayer->play( data.data(), data.size() );
|
||||
if ( !errorMessage.isEmpty() ) {
|
||||
QMessageBox::critical( this, "GoldenDict", tr( "Failed to play sound file: %1" ).arg( errorMessage ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ArticleView::audioPlayerError( QString const & message )
|
||||
{
|
||||
emit statusBarMessage( tr( "WARNING: Audio Player: %1" ).arg( message ), 10000, QPixmap( ":/icons/error.svg" ) );
|
||||
|
|
|
@ -152,6 +152,8 @@ public:
|
|||
QUrl const & referrer,
|
||||
QString const & scrollTo = QString(),
|
||||
Contexts const & contexts = Contexts() );
|
||||
void playAudio( QUrl const & url );
|
||||
void audioDownloadFinished( const sptr< Dictionary::DataRequest > & req );
|
||||
|
||||
/// Called when the state of dictionary bar changes and the view is active.
|
||||
/// The function reloads content if the change affects it.
|
||||
|
|
|
@ -723,7 +723,7 @@ MainWindow::MainWindow( Config::Class & cfg_ ):
|
|||
auto view = getCurrentArticleView();
|
||||
if ( ( cfg.preferences.pronounceOnLoadMain ) && view != nullptr ) {
|
||||
|
||||
view->openLink( QUrl::fromEncoded( audioUrl.toUtf8() ), {} );
|
||||
view->playAudio( QUrl::fromEncoded( audioUrl.toUtf8() ) );
|
||||
}
|
||||
} );
|
||||
applyProxySettings();
|
||||
|
|
|
@ -169,8 +169,7 @@ ScanPopup::ScanPopup( QWidget * parent,
|
|||
return;
|
||||
}
|
||||
if ( cfg.preferences.pronounceOnLoadPopup ) {
|
||||
|
||||
definition->openLink( QUrl::fromEncoded( audioUrl.toUtf8() ), {} );
|
||||
definition->playAudio( QUrl::fromEncoded( audioUrl.toUtf8() ) );
|
||||
}
|
||||
} );
|
||||
pinnedGeometry = cfg.popupWindowGeometry;
|
||||
|
|
Loading…
Reference in a new issue