Skip to content
This repository has been archived by the owner on Jun 28, 2021. It is now read-only.

Commit

Permalink
Implement stop button in audio player
Browse files Browse the repository at this point in the history
  • Loading branch information
azeezolaniran2016 committed Jul 27, 2017
1 parent 7789bf5 commit 0a1790a
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 9 deletions.
40 changes: 32 additions & 8 deletions src/components/Audioplayer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ export class Audioplayer extends Component {
// NOTE: if no file, just wait.
if (!file) return false;

const { update } = this.props; // eslint-disable-line no-shadow
const { update, stop } = this.props; // eslint-disable-line no-shadow
debug('component:Audioplayer', `Attaching listeners to ${file.src}`);

// Preload file
Expand Down Expand Up @@ -360,11 +360,16 @@ export class Audioplayer extends Component {
file.ontimeupdate = null; // eslint-disable-line no-param-reassign
};

const stopAudio = () => {
file.currentTime = 0; // eslint-disable-line no-param-reassign
stop();
};

file.onloadeddata = onLoadeddata; // eslint-disable-line no-param-reassign
file.onpause = onPause; // eslint-disable-line no-param-reassign
file.onplay = onPlay; // eslint-disable-line no-param-reassign
file.onended = onEnded; // eslint-disable-line no-param-reassign

file.stopAudio = stopAudio; // eslint-disable-line no-param-reassign
return file;
}

Expand All @@ -377,6 +382,7 @@ export class Audioplayer extends Component {
file.onPause = null; // eslint-disable-line no-param-reassign
file.onended = null; // eslint-disable-line no-param-reassign
file.onprogress = null; // eslint-disable-line no-param-reassign
delete file.stopAudio; // eslint-disable-line no-param-reassign
};

handleTrackChange = (fraction) => {
Expand All @@ -389,7 +395,7 @@ export class Audioplayer extends Component {
currentFile.currentTime = fraction * currentFile.duration;
};

renderPlayStopButtons() {
renderPlayPauseButtons() {
const { isPlaying, pause } = this.props; // eslint-disable-line no-shadow

return (
Expand Down Expand Up @@ -438,6 +444,21 @@ export class Audioplayer extends Component {
);
}

renderStopButton() {
const { currentFile } = this.props;
return (
<a
tabIndex="-1"
className={`pointer ${style.buttons} ${currentFile.currentTime > 0
? ''
: style.disabled}`}
onClick={currentFile.stopAudio}
>
<i className="ss-icon ss-stop" />
</a>
);
}

render() {
debug('component:Audioplayer', 'render');

Expand Down Expand Up @@ -471,7 +492,8 @@ export class Audioplayer extends Component {

return (
<div
className={`${isPlaying && style.isPlaying} ${style.container} ${className}`}
className={`${isPlaying &&
style.isPlaying} ${style.container} ${className}`}
>
<Wrapper>
{currentFile &&
Expand All @@ -493,15 +515,16 @@ export class Audioplayer extends Component {
id="player.currentVerse"
defaultMessage="Ayah"
/>
:
{' '}
{currentVerse.verseKey.split(':')[1]}
: {currentVerse.verseKey.split(':')[1]}
</ControlItem>
<ControlItem>
{this.renderPreviousButton()}
</ControlItem>
<ControlItem>
{this.renderPlayStopButtons()}
{this.renderPlayPauseButtons()}
</ControlItem>
<ControlItem>
{this.renderStopButton()}
</ControlItem>
<ControlItem>
{this.renderNextButton()}
Expand Down Expand Up @@ -557,6 +580,7 @@ Audioplayer.propTypes = {
isLoading: PropTypes.bool.isRequired,
play: PropTypes.func.isRequired,
pause: PropTypes.func.isRequired,
stop: PropTypes.func.isRequired,
next: PropTypes.func.isRequired, // eslint-disable-line
previous: PropTypes.func.isRequired, // eslint-disable-line
update: PropTypes.func.isRequired,
Expand Down
9 changes: 8 additions & 1 deletion src/redux/actions/audioplayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import {
LOAD,
LOAD_SUCCESS,
LOAD_FAIL,
UPDATE
UPDATE,
STOP
} from 'redux/constants/audioplayer.js';

export function setCurrentFile(file) {
Expand Down Expand Up @@ -43,6 +44,12 @@ export function play() {
};
}

export function stop() {
return {
type: STOP
};
}

export function pause() {
return {
type: PAUSE
Expand Down
1 change: 1 addition & 0 deletions src/redux/constants/audioplayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ export const UPDATE = '@@quran/audioplayer/UPDATE';
export const LOAD = '@@quran/audioplayer/LOAD';
export const LOAD_SUCCESS = '@@quran/audioplayer/LOAD_SUCCESS';
export const LOAD_FAIL = '@@quran/audioplayer/LOAD_FAIL';
export const STOP = '@@quran/audioplayer/STOP';
7 changes: 7 additions & 0 deletions src/redux/modules/audioplayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
PLAY,
PAUSE,
NEXT,
STOP,
SET_AYAH,
PREVIOUS,
SET_REPEAT,
Expand Down Expand Up @@ -101,6 +102,12 @@ export default function reducer(state = initialState, action = {}) {
isPlaying: true
};
}
case STOP: {
return {
...state,
isPlaying: false
};
}
case PAUSE: {
return {
...state,
Expand Down

0 comments on commit 0a1790a

Please sign in to comment.