Skip to content

Commit

Permalink
Add playAfter and cleanups. Bump 0.0.26
Browse files Browse the repository at this point in the history
  • Loading branch information
mrf345 committed Jun 15, 2020
1 parent 447b91e commit f362d57
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 14 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ List of typical music player like methods:
| Method | Input | Output | Description|
|---------|--------|--------|----------|
| `.play(file = '')` | `file`: audio file's link. | `boolean` | to start playing the added audio files. |
| `.playAfter(files = [])` | `files`: array of files to play. | N/A | play `files` after the ongoing playlist sequence is done. |
| `.replay()` | N/A | `boolean` | restart playing the current audio file. |
| `.stop()` | N/A | `boolean` | stop playing all added audio files. |
| `.pause()` | N/A | `boolean` | pause the currently playing audio. |
Expand Down
2 changes: 1 addition & 1 deletion bin/AudioSequence.min.js

Large diffs are not rendered by default.

8 changes: 1 addition & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "audio_sequence",
"version": "0.0.25",
"version": "0.0.26",
"description": "simple module to control, monitor and play multiple audio files in sequences at a time.",
"main": "lib/index.js",
"scripts": {
Expand Down Expand Up @@ -42,8 +42,7 @@
"jest-cli": "^24.9.0",
"standard": "^14.3.4",
"webpack": "^4.43.0",
"webpack-cli": "^3.3.11",
"webpack-node-externals": "^1.7.2"
"webpack-cli": "^3.3.11"
},
"standard": {
"globals": [
Expand Down
11 changes: 10 additions & 1 deletion src/main.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { thisExpression } from "@babel/types"

export class AudioSequence {
/**
* Utility to ease the process of plying audio elements in sequences.
Expand Down Expand Up @@ -27,6 +29,7 @@ export class AudioSequence {
this.repeatCounter = 0 // whole repeats index counter
this.prePromises = [] // to resolve prior to ending transition
this.postPromises = [] // to resolve after ending transition
this.waiting = [] // array of files array to play after ending
this.logger = undefined // store the logging interval

this.hasFiles = () => !!this.playlist.length
Expand All @@ -35,11 +38,17 @@ export class AudioSequence {
this.getNext = p => this.keepWithin(p ? this.current - 1 : this.current + 1, this.playlist)
this.isMuted = () => this.hasFiles() && this.getCurrent().item.volume === 0
this.isPaused = () => this.hasFiles() && this.getCurrent().item.paused
this.isLast = () => (this.playlist.length - 1) === this.current
this.notStarted = () => !this.isPaused() && !this.isActive()
this.getPlace = ele => this.files
.map(f => f.replace(window.origin, ''))
.indexOf(ele.src.replace(window.origin, ''))
this.isLast = () => (this.playlist.length - 1) === this.current
this.isFinal = () => {
const lastTrack = (this.playlist.length - 1) === this.current
const lastRepeat = this.getCurrent().item.repeats >= this.repeats

return lastTrack && (this.repeatEach ? lastRepeat : this.repeatCounter >= this.repeats)
}
this.isActive = item => {
if (this.hasFiles()) {
item = item || this.getCurrent().item
Expand Down
8 changes: 8 additions & 0 deletions src/mixins/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ module.exports = {
}
},

playAfter (files = []) {
if (this.isActive()) this.waiting.push(files)
else {
this.files = files
this.load().then(() => !this.autoStart && this.play())
}
},

replay () {
const canReplay = this.isActive() || this.isPaused()

Expand Down
9 changes: 9 additions & 0 deletions src/mixins/fetcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ module.exports = {
this.prePromises = []
this.postPromises = []
this.loading = false

if (!this.isActive() && this.isFinal() && element.src === this.playlist.slice(-1)[0].src) {
const waitingFiles = this.waiting.shift()

if (waitingFiles) {
this.files = waitingFiles
this.load().then(() => !this.autoStart && this.play())
}
}
}).catch(e => console.warn(e))
}

Expand Down
4 changes: 2 additions & 2 deletions webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { resolve } = require('path')
const nodeExternals = require('webpack-node-externals')


module.exports = [
{
Expand All @@ -19,5 +19,5 @@ module.exports = [
use: { loader: 'babel-loader' }
}]
}
},
}
]

0 comments on commit f362d57

Please sign in to comment.