Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#58 Add a pause between each paragraph #89

Merged
merged 3 commits into from
Sep 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions app/src/main/java/ai/elimu/vitabu/ui/storybook/ChapterFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import android.os.Environment;
import android.speech.tts.TextToSpeech;
import android.speech.tts.UtteranceProgressListener;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
Expand Down Expand Up @@ -58,9 +57,11 @@ public class ChapterFragment extends Fragment implements AudioListener {
final static String PICTURES_PATH = FILES_PATH + Environment.DIRECTORY_PICTURES + "/";
final static String MUSIC_PATH = FILES_PATH + Environment.DIRECTORY_MUSIC + "/";

private final static long PARAGRAPH_PAUSE = 1000;

private StoryBookChapterGson storyBookChapter;

protected String chapterText = "";
protected String[] chapterParagraphs = {};

private RecyclerView chapterRecyclerView;

Expand Down Expand Up @@ -159,6 +160,7 @@ public void onItemClick(WordGson wordWithAudio, View view, int position) {
chapterRecyclerView.setAdapter(wordViewAdapter);
}

chapterParagraphs = new String[storyBookParagraphGsons.size()];
for (int paragraphIndex = 0; paragraphIndex < storyBookParagraphGsons.size(); paragraphIndex++) {
Log.i(getClass().getName(), "storyBookParagraphGson.getOriginalText(): \"" + storyBookParagraphGsons.get(paragraphIndex).getOriginalText() + "\"");

Expand All @@ -167,10 +169,7 @@ public void onItemClick(WordGson wordWithAudio, View view, int position) {
Log.i(getClass().getName(), "wordsInOriginalText.length: " + wordsInOriginalText.length);
Log.i(getClass().getName(), "Arrays.toString(wordsInOriginalText): " + Arrays.toString(wordsInOriginalText));

if (!TextUtils.isEmpty(chapterText)) {
chapterText += "\n\n";
}
chapterText += originalText;
chapterParagraphs[paragraphIndex] = originalText;

List<WordGson> wordAudios = storyBookParagraphGsons.get(paragraphIndex).getWords();
Log.i(getClass().getName(), "words: " + wordAudios);
Expand All @@ -189,18 +188,18 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
super.onViewCreated(view, savedInstanceState);

// Add button for initializing Text-to-Speech (TTS)
final String finalChapterText = chapterText;
final String[] chapterText = chapterParagraphs;
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);
playAudio(chapterText, ChapterFragment.this);
}
});
}

public void playAudio(final String chapterText, final AudioListener audioListener) {
public void playAudio(final String[] chapterText, final AudioListener audioListener) {
List<StoryBookParagraphGson> storyBookParagraphs = storyBookChapter.getStoryBookParagraphs();
StoryBookParagraphGson storyBookParagraphGson = storyBookParagraphs.get(0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like we have hardcoded a selection of only the first paragraph, which is probably the audio is not being played correctly for chapters containing more than 1 paragraph.

I created a separate issue for this here: #90

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll take that issue

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll take that issue

@gscdev Great, thank you! 🙂

String transcription = storyBookParagraphGson.getOriginalText();
Expand All @@ -213,8 +212,11 @@ public void playAudio(final String chapterText, final AudioListener audioListene
// Fall back to TTS
tts.setOnUtteranceProgressListener(getUtteranceProgressListener(audioListener));

Log.i(getClass().getName(), "chapterText: \"" + chapterText + "\"");
tts.speak(chapterText.replaceAll("[-*]", ""), TextToSpeech.QUEUE_FLUSH, null, "0");
Log.i(getClass().getName(), "chapterText: \"" + Arrays.toString(chapterText) + "\"");
for (String paragraph : chapterText) {
tts.speak(paragraph.replaceAll("[-*]", ""), TextToSpeech.QUEUE_ADD, null, "0");
tts.playSilentUtterance(PARAGRAPH_PAUSE, TextToSpeech.QUEUE_ADD, null);
}
}
}

Expand Down
22 changes: 12 additions & 10 deletions app/src/main/java/ai/elimu/vitabu/ui/storybook/CoverFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class CoverFragment extends ChapterFragment {
protected TextView titleTextView;

private TextView descriptionTextView;
private String description;
private final String[] description = new String[1];

public static CoverFragment newInstance(ReadingLevel readingLevel, String description) {
CoverFragment fragment = new CoverFragment();
Expand All @@ -54,20 +54,22 @@ public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
int[] titleFontSize = getResources().getIntArray(R.array.cover_title_font_size);
int[] descriptionFontSize = getResources().getIntArray(R.array.chapter_text_font_size);

chapterText = setWordSpacing(chapterText);
for (int i = 0; i < chapterParagraphs.length; i++) {
chapterParagraphs[i] = setWordSpacing(chapterParagraphs[i]);
}

titleTextView = root.findViewById(R.id.storybook_title);
titleTextView.setText(chapterText);
titleTextView.setText(chapterParagraphs[0]);

setTextSizeByLevel(titleTextView, titleFontSize);

// Initialize audio parameters with the storybook title
audioTextView = titleTextView;
audioText = chapterText;

description = setWordSpacing((String) getArguments().get(ARG_DESCRIPTION));
audioText = TextUtils.join("", chapterParagraphs);
description[0] = setWordSpacing((String) getArguments().get(ARG_DESCRIPTION));
descriptionTextView = root.findViewById(R.id.storybook_description);
descriptionTextView.setText(description);
descriptionTextView.setText(description[0]);

setTextSizeByLevel(descriptionTextView, descriptionFontSize);

Expand All @@ -93,7 +95,7 @@ private void setTextSizeByLevel(TextView textView, int[] fontSize) {
public void onAudioDone() {
// Update audio parameters with the storybook description
audioTextView = descriptionTextView;
audioText = description;
audioText = description[0];

playAudio(description, null);
}
Expand Down Expand Up @@ -148,10 +150,10 @@ public void onError(String utteranceId) {
public void onStop(String utteranceId, boolean interrupted) {
super.onStop(utteranceId, interrupted);
requireActivity().runOnUiThread(() -> {
titleTextView.setText(chapterText);
descriptionTextView.setText(description);
titleTextView.setText(TextUtils.join("", chapterParagraphs));
descriptionTextView.setText(description[0]);
});
audioText = chapterText;
audioText = TextUtils.join("", chapterParagraphs);
audioTextView = titleTextView;
}
};
Expand Down