@@ -1383,7 +1383,12 @@ public void ensureAudioBookPlaying() {
13831383 if (mMediaPlayer != null && mMediaPlayer .isPlaying ()){
13841384 return ;
13851385 }
1386- if (audioFile == null ){
1386+ File fileToPlay = audioFile ;
1387+ if (fileToPlay != null && !fileToPlay .exists ()){
1388+ fileToPlay = getAlternativeAudioFile (fileToPlay );
1389+ }
1390+
1391+ if (fileToPlay == null || !fileToPlay .exists ()){
13871392 return ;
13881393 }
13891394
@@ -1394,7 +1399,7 @@ public void ensureAudioBookPlaying() {
13941399 mMediaPlayer = null ;
13951400 }
13961401 mMediaPlayer = MediaPlayer .create (
1397- getApplicationContext (), Uri .parse ("file://" + audioFile .toString ()));
1402+ getApplicationContext (), Uri .parse ("file://" + fileToPlay .toString ()));
13981403 }catch (Exception e ){
13991404 log .d ("ERROR: " + e .getMessage ());
14001405 }
@@ -1527,6 +1532,38 @@ else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
15271532 return notification ;
15281533 }
15291534
1535+ private File getAlternativeAudioFile (File origAudioFile ) {
1536+ if (origAudioFile == null ) {
1537+ return null ;
1538+ }
1539+ String fileNoExt = origAudioFile .toString ().replaceAll ("\\ .\\ w+$" , "" );
1540+ File dir = origAudioFile .getParentFile ();
1541+ if (dir .exists () && dir .isDirectory ()) {
1542+ Map <String , List <File >> filesByExt = new HashMap <>();
1543+ File firstFile = null ;
1544+ for (File file : dir .listFiles ()) {
1545+ if (!file .toString ().startsWith (fileNoExt + "." )){
1546+ continue ;
1547+ }
1548+ String ext = file .toString ().toLowerCase ().replaceAll (".*\\ ." , "" );
1549+ if (filesByExt .get (ext ) == null ) {
1550+ filesByExt .put (ext , new ArrayList <>());
1551+ }
1552+ filesByExt .get (ext ).add (file );
1553+ if (firstFile == null ) {
1554+ firstFile = file ;
1555+ }
1556+ }
1557+ for (String ext : new String []{"flac" , "wav" , "m4a" , "ogg" , "mp3" }) {
1558+ if (filesByExt .get (ext ) != null ){
1559+ return filesByExt .get (ext ).get (0 );
1560+ }
1561+ }
1562+ return firstFile ;
1563+ }
1564+ return null ;
1565+ }
1566+
15301567 private void setupTTSHandlers () {
15311568 if (useAudioBook ){
15321569 return ;
0 commit comments