Skip to content

Commit

Permalink
audiobook: allow different file extensions for audio files vs wordtiming
Browse files Browse the repository at this point in the history
  • Loading branch information
teleshoes committed Apr 25, 2023
1 parent 7bacec4 commit c701b7a
Showing 1 changed file with 39 additions and 2 deletions.
41 changes: 39 additions & 2 deletions android/src/org/coolreader/tts/TTSControlService.java
Original file line number Diff line number Diff line change
Expand Up @@ -1383,7 +1383,12 @@ public void ensureAudioBookPlaying() {
if(mMediaPlayer != null && mMediaPlayer.isPlaying()){
return;
}
if(audioFile == null){
File fileToPlay = audioFile;
if(fileToPlay != null && !fileToPlay.exists()){
fileToPlay = getAlternativeAudioFile(fileToPlay);
}

if(fileToPlay == null || !fileToPlay.exists()){
return;
}

Expand All @@ -1394,7 +1399,7 @@ public void ensureAudioBookPlaying() {
mMediaPlayer = null;
}
mMediaPlayer = MediaPlayer.create(
getApplicationContext(), Uri.parse("file://" + audioFile.toString()));
getApplicationContext(), Uri.parse("file://" + fileToPlay.toString()));
}catch(Exception e){
log.d("ERROR: " + e.getMessage());
}
Expand Down Expand Up @@ -1527,6 +1532,38 @@ else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
return notification;
}

private File getAlternativeAudioFile(File origAudioFile) {
if(origAudioFile == null) {
return null;
}
String fileNoExt = origAudioFile.toString().replaceAll("\\.\\w+$", "");
File dir = origAudioFile.getParentFile();
if(dir.exists() && dir.isDirectory()) {
Map<String, List<File>> filesByExt = new HashMap<>();
File firstFile = null;
for(File file : dir.listFiles()) {
if(!file.toString().startsWith(fileNoExt + ".")){
continue;
}
String ext = file.toString().toLowerCase().replaceAll(".*\\.", "");
if(filesByExt.get(ext) == null) {
filesByExt.put(ext, new ArrayList<>());
}
filesByExt.get(ext).add(file);
if(firstFile == null) {
firstFile = file;
}
}
for(String ext : new String[]{"flac", "wav", "m4a", "ogg", "mp3"}) {
if(filesByExt.get(ext) != null){
return filesByExt.get(ext).get(0);
}
}
return firstFile;
}
return null;
}

private void setupTTSHandlers() {
if(useAudioBook){
return;
Expand Down

0 comments on commit c701b7a

Please sign in to comment.