-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathSpotify.js
137 lines (114 loc) · 2.82 KB
/
Spotify.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import React, { Component } from 'react'
import { getSDK, callPlayer } from '../utils'
import { canPlay } from '../patterns'
const SDK_URL = 'https://open.spotify.com/embed-podcast/iframe-api/v1'
const SDK_GLOBAL = 'SpotifyIframeApi'
const SDK_GLOBAL_READY = 'SpotifyIframeApi'
export default class Spotify extends Component {
static displayName = 'Spotify'
static loopOnEnded = true
static canPlay = canPlay.spotify
callPlayer = callPlayer
duration = null
currentTime = null
totalTime = null
player = null
componentDidMount () {
this.props.onMount && this.props.onMount(this)
}
load (url) {
if (window[SDK_GLOBAL] && !this.player) {
this.initializePlayer(window[SDK_GLOBAL], url)
return
} else if (this.player) {
this.callPlayer('loadUri', this.props.url)
return
}
window.onSpotifyIframeApiReady = (IFrameAPI) => this.initializePlayer(IFrameAPI, url)
getSDK(SDK_URL, SDK_GLOBAL, SDK_GLOBAL_READY)
}
initializePlayer = (IFrameAPI, url) => {
if (!this.container) return
const options = {
width: '100%',
height: '100%',
uri: url
}
const callback = (EmbedController) => {
this.player = EmbedController
this.player.addListener('playback_update', this.onStateChange)
this.player.addListener('ready', this.props.onReady)
}
IFrameAPI.createController(this.container, options, callback)
}
onStateChange = (event) => {
const { data } = event
const { onPlay, onPause, onBuffer, onBufferEnd, onEnded } = this.props
if (data.position >= data.duration && data.position && data.duration) {
onEnded()
}
if (data.isPaused === true) onPause()
if (data.isPaused === false && data.isBuffering === false) {
this.currentTime = data.position
this.totalTime = data.duration
onPlay()
onBufferEnd()
}
if (data.isBuffering === true) onBuffer()
}
play () {
this.callPlayer('resume')
}
pause () {
this.callPlayer('pause')
}
stop () {
this.callPlayer('destroy')
}
seekTo (amount) {
this.callPlayer('seek', amount)
if (!this.props.playing) {
this.pause()
} else {
this.play()
}
}
setVolume (fraction) {
// No volume support
}
mute () {
// No volume support
}
unmute () {
// No volume support
}
setPlaybackRate (rate) {
// No playback rate support
}
setLoop (loop) {
// No loop support
}
getDuration () {
return this.totalTime / 1000
}
getCurrentTime () {
return this.currentTime / 1000
}
getSecondsLoaded () {
// No seconds loaded support
}
ref = container => {
this.container = container
}
render () {
const style = {
width: '100%',
height: '100%'
}
return (
<div style={style}>
<div ref={this.ref} />
</div>
)
}
}