Created
July 7, 2019 14:25
-
-
Save Charpell/5fb361962f2f8f5f2100438bba823fc0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Alert } from "react-native"; | |
import { Audio } from "expo"; | |
export default class MusicPlayer { | |
// Current item index | |
index = 0; | |
constructor(list, initialState = { speed: 1, autoPlay: true }) { | |
// Create new object from Expo.Audio.Sound | |
this.soundObject = new Audio.Sound(); | |
// Set speed value | |
this.speed = initialState.speed; | |
this.list = list; | |
} | |
/** | |
* Start or stop audio. | |
* @param {number} index - Index of playing item. | |
* @param {boolean} playing - Is audio playing now or no. | |
* @return {Promise} | |
*/ | |
startPlay = async (index = this.index, playing = false) => { | |
const path = this.list[index].path; | |
this.index = index; | |
// Checking if now playing music, if yes stop that | |
if(playing) { | |
await this.soundObject.stopAsync(); | |
} else { | |
// Checking if item already loaded, if yes just play, else load music before play | |
if(this.soundObject._loaded) { | |
await this.soundObject.playAsync(); | |
} else { | |
await this.soundObject.loadAsync(path); | |
await this.soundObject.playAsync(); | |
} | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment