diff --git a/src/components/Audioplayer/index.js b/src/components/Audioplayer/index.js
index 44620c0f4..6c91a66e3 100644
--- a/src/components/Audioplayer/index.js
+++ b/src/components/Audioplayer/index.js
@@ -28,8 +28,13 @@ const RepeatDropdown = Loadable({
});
export class Audioplayer extends Component {
+ state = {
+ loadingFile: false
+ };
+
componentDidMount() {
- const { currentFile } = this.props; // eslint-disable-line no-shadow, max-len
+ const { currentFile, currentVerse, audio, verses, load } = this.props; // eslint-disable-line no-shadow, max-len
+ const nextVerse = verses[this.getNext()];
debug('component:Audioplayer', 'componentDidMount');
@@ -37,7 +42,19 @@ export class Audioplayer extends Component {
return this.handleAddFileListeners(currentFile);
}
- console.error('Audioplayer mounted but no file available');
+ load({
+ chapterId: currentVerse.chapterId,
+ verseId: currentVerse.id,
+ verseKey: currentVerse.verseKey,
+ audio
+ });
+
+ load({
+ chapterId: nextVerse.chapterId,
+ verseId: nextVerse.id,
+ verseKey: nextVerse.verseKey,
+ audio
+ });
return false;
}
@@ -58,7 +75,7 @@ export class Audioplayer extends Component {
}
// Change verse
- if (this.props.currentVerse !== nextProps.currentVerse) {
+ if (this.props.currentVerse.verseKey !== nextProps.currentVerse.verseKey) {
if (this.props.currentFile) {
this.handleRemoveFileListeners(this.props.currentFile);
}
@@ -66,11 +83,42 @@ export class Audioplayer extends Component {
return this.handleAddFileListeners(nextProps.currentFile);
}
+ if (this.props.audio !== nextProps.audio) {
+ Object.keys(this.props.files).forEach(key =>
+ this.props.load({
+ chapterId: this.props.verses[key].chapterId,
+ verseId: this.props.verses[key].id,
+ verseKey: this.props.verses[key].verseKey,
+ audio: nextProps.audio
+ })
+ );
+ }
+
return false;
}
- componentDidUpdate() {
- const { currentFile, isPlaying } = this.props;
+ componentDidUpdate(previousProps) {
+ const {
+ currentFile,
+ isPlaying,
+ verses,
+ audio,
+ currentVerse,
+ load
+ } = this.props;
+
+ if (
+ currentVerse.verseKey !== previousProps.currentVerse.verseKey &&
+ verses[this.getNext()]
+ ) {
+ const verse = verses[this.getNext()];
+ load({
+ chapterId: verse.chapterId,
+ verseId: verse.id,
+ verseKey: verse.verseKey,
+ audio
+ });
+ }
if (!currentFile) return false;
@@ -95,30 +143,28 @@ export class Audioplayer extends Component {
}
getPrevious() {
- const { currentVerse, files } = this.props;
- const ayahIds = Object.keys(files);
- const index = ayahIds.findIndex(id => id === currentVerse);
+ const { currentVerse, verseIds } = this.props;
+ const index = verseIds.findIndex(id => id === currentVerse.verseKey);
- return ayahIds[index - 1];
+ return verseIds[index - 1];
}
getNext() {
- const { currentVerse, chapter, files, onLoadAyahs } = this.props;
- const ayahIds = Object.keys(files);
- const ayahNum = currentVerse.split(':')[1];
- const index = ayahIds.findIndex(id => id === currentVerse);
+ const { currentVerse, chapter, onLoadAyahs, verseIds } = this.props;
+ const ayahNum = currentVerse.verseKey.split(':')[1];
+ const index = verseIds.findIndex(id => id === currentVerse.verseKey);
if (chapter.versesCount === ayahNum + 1) {
// We are at the end of the chapter!
return false;
}
- if (ayahIds.length - 3 <= index + 1) {
+ if (verseIds.length - 3 <= index + 1) {
// Need to load the next set of ayahs!
onLoadAyahs();
}
- return ayahIds[index + 1];
+ return verseIds[index + 1];
}
handleAyahChange = (direction = 'next') => {
@@ -130,7 +176,7 @@ export class Audioplayer extends Component {
const nextVerse = this[camelize(`get_${direction}`)]();
if (!nextVerse) return pause();
- this.props[direction](currentVerse);
+ this.props[direction](currentVerse.verseKey);
this.handleScrollTo(nextVerse);
@@ -141,7 +187,7 @@ export class Audioplayer extends Component {
return false;
};
- scrollToVerse = (ayahNum = this.props.currentVerse) => {
+ scrollToVerse = (ayahNum = this.props.currentVerse.verseKey) => {
scroller.scrollTo(`verse:${ayahNum}`, -45);
};
@@ -163,7 +209,7 @@ export class Audioplayer extends Component {
preloadNext() {
const { currentVerse, files } = this.props;
const ayahIds = Object.keys(files);
- const index = ayahIds.findIndex(id => id === currentVerse) + 1;
+ const index = ayahIds.findIndex(id => id === currentVerse.verseKey) + 1;
for (let id = index; id <= index + 2; id += 1) {
if (ayahIds[id]) {
@@ -183,7 +229,7 @@ export class Audioplayer extends Component {
setRepeat, // eslint-disable-line no-shadow
setAyah // eslint-disable-line no-shadow
} = this.props;
- const [chapter, ayah] = currentVerse
+ const [chapter, ayah] = currentVerse.verseKey
.split(':')
.map(val => parseInt(val, 10));
@@ -244,7 +290,7 @@ export class Audioplayer extends Component {
if (!shouldScroll) {
// we use the inverse (!) here because we're toggling, so false is true
- this.scrollToVerse(currentVerse);
+ this.scrollToVerse(currentVerse.verseKey);
}
this.props.toggleScroll();
@@ -254,7 +300,7 @@ export class Audioplayer extends Component {
// NOTE: if no file, just wait.
if (!file) return false;
- const { update, currentTime } = this.props; // eslint-disable-line no-shadow
+ const { update } = this.props; // eslint-disable-line no-shadow
debug('component:Audioplayer', `Attaching listeners to ${file.src}`);
// Preload file
@@ -262,8 +308,8 @@ export class Audioplayer extends Component {
const onLoadeddata = () => {
// Default current time to zero. This will change
- file.currentTime = // eslint-disable-line no-param-reassign
- file.currentTime || currentTime || 0;
+ file.currentTime = 0; // eslint-disable-line
+ // file.currentTime || currentTime || 0;
return update({
duration: file.duration,
@@ -345,7 +391,9 @@ export class Audioplayer extends Component {
renderPreviousButton() {
const { currentVerse, files } = this.props;
if (!files) return false;
- const index = Object.keys(files).findIndex(id => id === currentVerse);
+ const index = Object.keys(files).findIndex(
+ id => id === currentVerse.verseKey
+ );
return (
}
{segments &&
- segments[currentVerse] &&
+ segments[currentVerse.verseKey] &&