Skip to content

Commit

Permalink
Play/Pause toggle
Browse files Browse the repository at this point in the history
- Play and pause buttons now merged into one button and will toggle depending on state of the player.
  • Loading branch information
evanlouie committed Oct 29, 2018
1 parent 1892964 commit 14e7184
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 25 deletions.
48 changes: 24 additions & 24 deletions src/components/AudioPlayer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ interface IAudioPlayerState {
classifications: List<Classification>
currentlyPlayingLabelIds: Set<number>
isLoading: boolean
isPlaying: boolean
labels: List<Label>
wavesurfer?: WaveSurferInstance & WaveSurferRegions
wavesurferRegionIdToLabelIdMap: Map<string | number, number>
Expand Down Expand Up @@ -90,6 +91,7 @@ export class AudioPlayer extends React.PureComponent<IAudioPlayerProps, IAudioPl
classifications: List(),
currentlyPlayingLabelIds: Set(),
isLoading: true,
isPlaying: false,
labels: List(),
wavesurferRegionIdToLabelIdMap: Map(),
zoom: 50,
Expand Down Expand Up @@ -281,10 +283,9 @@ export class AudioPlayer extends React.PureComponent<IAudioPlayerProps, IAudioPl
}

public render() {
const { wavesurfer, classifications, audioUrl } = this.state
const { wavesurfer, classifications, audioUrl, isPlaying } = this.state
const maxWidthRefStyles = { width: "100%", minWidth: "20vw" }
const isVideo = this.isVideoFile(this.props.filepath)

const classificationOption = ({ id, name }: Classification) => (
<option key={id} value={name}>
{name}
Expand Down Expand Up @@ -325,16 +326,19 @@ export class AudioPlayer extends React.PureComponent<IAudioPlayerProps, IAudioPl

{wavesurfer && (
<div className="toolbar">
<Tooltip title="Play">
<Button mini={true} color="primary" onClick={this.playAudio}>
<PlayArrowRounded />
</Button>
</Tooltip>
<Tooltip title="Pause">
<Button mini={true} color="secondary" onClick={this.pauseAudio}>
<Pause />
</Button>
</Tooltip>
{isPlaying ? (
<Tooltip title="Pause">
<Button mini={true} color="secondary" onClick={this.pauseAudio}>
<Pause />
</Button>
</Tooltip>
) : (
<Tooltip title="Play">
<Button mini={true} color="primary" onClick={this.playAudio}>
<PlayArrowRounded />
</Button>
</Tooltip>
)}
<NotificationContext.Consumer>
{({ notify }) => {
const notifiedHandler = async () =>
Expand Down Expand Up @@ -431,21 +435,17 @@ export class AudioPlayer extends React.PureComponent<IAudioPlayerProps, IAudioPl
// Helpers
////////////////////////////////////////////////////////////////////////////////
private playAudio = async () => {
const { wavesurfer } = this.state
return wavesurfer
? wavesurfer.play()
: Promise.reject(
new Error(`Failed to call play() on wavesurfer instance; instance: ${wavesurfer}`),
)
this.wavesurfer().then((w) => {
this.setState({ isPlaying: true })
w.play()
})
}

private pauseAudio = async () => {
const { wavesurfer } = this.state
return wavesurfer
? wavesurfer.pause()
: Promise.reject(
new Error(`Failed to call pause() on wavesurfer instance; instance: ${wavesurfer}`),
)
this.wavesurfer().then((w) => {
this.setState({ isPlaying: false })
w.pause()
})
}

private addLabel = async (label: {
Expand Down
2 changes: 1 addition & 1 deletion src/entities/AudioFile.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { writeFile } from "fs"
import { readFile, writeFile } from "fs"
import { ensureDir } from "fs-extra"
import path from "path"
import {
Expand Down

0 comments on commit 14e7184

Please sign in to comment.