@@ -1383,7 +1383,12 @@ public void ensureAudioBookPlaying() {
1383
1383
if (mMediaPlayer != null && mMediaPlayer .isPlaying ()){
1384
1384
return ;
1385
1385
}
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 ()){
1387
1392
return ;
1388
1393
}
1389
1394
@@ -1394,7 +1399,7 @@ public void ensureAudioBookPlaying() {
1394
1399
mMediaPlayer = null ;
1395
1400
}
1396
1401
mMediaPlayer = MediaPlayer .create (
1397
- getApplicationContext (), Uri .parse ("file://" + audioFile .toString ()));
1402
+ getApplicationContext (), Uri .parse ("file://" + fileToPlay .toString ()));
1398
1403
}catch (Exception e ){
1399
1404
log .d ("ERROR: " + e .getMessage ());
1400
1405
}
@@ -1527,6 +1532,38 @@ else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
1527
1532
return notification ;
1528
1533
}
1529
1534
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
+
1530
1567
private void setupTTSHandlers () {
1531
1568
if (useAudioBook ){
1532
1569
return ;
0 commit comments