Skip to content

Commit

Permalink
Merge pull request #83 from elimu-ai/21-stop-audio-on-page-change
Browse files Browse the repository at this point in the history
#21 stop audio on page change
  • Loading branch information
gscdev authored May 6, 2021
2 parents 7ca9210 + 5dfe933 commit 7396ee0
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;
Expand Down Expand Up @@ -63,6 +64,8 @@ public class ChapterFragment extends Fragment implements AudioListener {

private TextToSpeech tts;

private MediaPlayer mediaPlayer;

protected int readingLevelPosition;

public static ChapterFragment newInstance(int chapterIndex, ReadingLevel readingLevel) {
Expand Down Expand Up @@ -176,17 +179,23 @@ public void onItemClick(WordGson wordWithAudio, View view, int position) {
fab.setVisibility(View.GONE);
}

return root;
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);

// Add button for initializing Text-to-Speech (TTS)
final String finalChapterText = chapterText;
FloatingActionButton fab = view.findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.i(getClass().getName(), "onClick");
playAudio(finalChapterText, ChapterFragment.this);
}
});

return root;
}

public void playAudio(final String chapterText, final AudioListener audioListener) {
Expand All @@ -207,14 +216,25 @@ public void playAudio(final String chapterText, final AudioListener audioListene
}
}

public UtteranceProgressListener getUtteranceProgressListener(AudioListener _audioListener) {
@Override
public void onPause() {
super.onPause();
tts.stop();
if (mediaPlayer != null) {
mediaPlayer.stop();
}
}

public UtteranceProgressListener getUtteranceProgressListener(AudioListener audioListener) {

final int[] wordPosition = {-1};

return new UtteranceProgressListener() {

final FlexboxLayoutManager layoutManager = (FlexboxLayoutManager) chapterRecyclerView.getLayoutManager();

View highlightedTextView;

@Override
public void onStart(String utteranceId) {
Log.i(getClass().getName(), "onStart");
Expand Down Expand Up @@ -248,6 +268,7 @@ public void onRangeStart(String utteranceId, int start, int end, int frame) {
itemView = layoutManager.findViewByPosition(wordPosition[0]);
if (itemView != null) {
itemView.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.colorAccent));
highlightedTextView = itemView;
}
}

Expand All @@ -267,6 +288,14 @@ public void onError(String utteranceId) {
Log.i(getClass().getName(), "onError");
}

@Override
public void onStop(String utteranceId, boolean interrupted) {
super.onStop(utteranceId, interrupted);
if (highlightedTextView != null) {
highlightedTextView.setBackground(ContextCompat.getDrawable(getContext(), R.drawable.bg_word_selector));
}
}

private void scrollToWordIfNotVisible(final int position) {
int firstWordVisible = layoutManager.findFirstCompletelyVisibleItemPosition();
int lastWordVisible = layoutManager.findLastCompletelyVisibleItemPosition();
Expand All @@ -288,7 +317,7 @@ private void playAudioFile(AudioGson audioGson) {
audioGson.getId() + "_r" + audioGson.getRevisionNumber() + "." + audioGson.getAudioFormat().toString().toLowerCase());
Log.i(getClass().getName(), "audioFile: " + audioFile);
Log.i(getClass().getName(), "audioFile.exists(): " + audioFile.exists());
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource(audioFile.getPath());
mediaPlayer.prepare();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class ChapterPagerAdapter extends FragmentStatePagerAdapter {
private String description;

public ChapterPagerAdapter(FragmentManager fm, List<StoryBookChapterGson> storyBookChapters, ReadingLevel readingLevel, String description) {
super(fm);
super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
ChapterPagerAdapter.storyBookChapters = storyBookChapters;
this.readingLevel = readingLevel;
this.description = description;
Expand Down
27 changes: 21 additions & 6 deletions app/src/main/java/ai/elimu/vitabu/ui/storybook/CoverFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,

titleTextView = root.findViewById(R.id.storybook_title);
titleTextView.setText(chapterText);

setTextSizeByLevel(titleTextView, titleFontSize);

// Initialize audio parameters with the storybook title
Expand Down Expand Up @@ -112,12 +113,14 @@ public void onRangeStart(String utteranceId, int start, int end, int frame) {
super.onRangeStart(utteranceId, start, end, frame);

Log.i(getClass().getName(), "utteranceId: " + utteranceId + ", start: " + start + ", end: " + end);

// Highlight the word being spoken
Spannable spannable = new SpannableString(audioText);
BackgroundColorSpan backgroundColorSpan = new BackgroundColorSpan(getResources().getColor(R.color.colorAccent));
spannable.setSpan(backgroundColorSpan, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
audioTextView.setText(spannable);

if (start >= 0) {
// Highlight the word being spoken
Spannable spannable = new SpannableString(audioText);
BackgroundColorSpan backgroundColorSpan = new BackgroundColorSpan(getResources().getColor(R.color.colorAccent));
spannable.setSpan(backgroundColorSpan, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
audioTextView.setText(spannable);
}
}

@Override
Expand All @@ -129,13 +132,25 @@ public void onDone(String utteranceId) {

if (audioListener != null) {
audioListener.onAudioDone();
} else {
onStop(utteranceId, false);
}
}

@Override
public void onError(String utteranceId) {
Log.i(getClass().getName(), "onError");
}

@Override
public void onStop(String utteranceId, boolean interrupted) {
super.onStop(utteranceId, interrupted);
titleTextView.setText(chapterText);
descriptionTextView.setText(description);

audioText = chapterText;
audioTextView = titleTextView;
}
};
}
}

0 comments on commit 7396ee0

Please sign in to comment.