opt: optimize the audio auto pronounciation logic (#1846)

* opt: optimize the audio play logic
This commit is contained in:
xiaoyifang 2024-10-23 20:14:11 +08:00 committed by GitHub
parent b39397a544
commit 426950fc10
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 73 additions and 3 deletions

View file

@ -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 * ArticleView::saveResource( const QUrl & url, const QString & fileName )
{ {
ResourceToSaveHandler * handler = new ResourceToSaveHandler( this, 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 ) void ArticleView::audioPlayerError( QString const & message )
{ {
emit statusBarMessage( tr( "WARNING: Audio Player: %1" ).arg( message ), 10000, QPixmap( ":/icons/error.svg" ) ); emit statusBarMessage( tr( "WARNING: Audio Player: %1" ).arg( message ), 10000, QPixmap( ":/icons/error.svg" ) );

View file

@ -152,6 +152,8 @@ public:
QUrl const & referrer, QUrl const & referrer,
QString const & scrollTo = QString(), QString const & scrollTo = QString(),
Contexts const & contexts = Contexts() ); 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. /// Called when the state of dictionary bar changes and the view is active.
/// The function reloads content if the change affects it. /// The function reloads content if the change affects it.

View file

@ -723,7 +723,7 @@ MainWindow::MainWindow( Config::Class & cfg_ ):
auto view = getCurrentArticleView(); auto view = getCurrentArticleView();
if ( ( cfg.preferences.pronounceOnLoadMain ) && view != nullptr ) { if ( ( cfg.preferences.pronounceOnLoadMain ) && view != nullptr ) {
view->openLink( QUrl::fromEncoded( audioUrl.toUtf8() ), {} ); view->playAudio( QUrl::fromEncoded( audioUrl.toUtf8() ) );
} }
} ); } );
applyProxySettings(); applyProxySettings();

View file

@ -169,8 +169,7 @@ ScanPopup::ScanPopup( QWidget * parent,
return; return;
} }
if ( cfg.preferences.pronounceOnLoadPopup ) { if ( cfg.preferences.pronounceOnLoadPopup ) {
definition->playAudio( QUrl::fromEncoded( audioUrl.toUtf8() ) );
definition->openLink( QUrl::fromEncoded( audioUrl.toUtf8() ), {} );
} }
} ); } );
pinnedGeometry = cfg.popupWindowGeometry; pinnedGeometry = cfg.popupWindowGeometry;