summaryrefslogtreecommitdiff
path: root/public/js/mx/mx.soundcloud.js
blob: fecb2f4a18e59e185f8c2bafa34f9cfe2091c4ca (plain)
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
MX.Soundcloud = MX.Object3D.extend({
  init: function (ops) {
  
  	this.type = "Soundcloud"
		this.media = ops.media
    this.width = 0
    this.height = 0
		this.x = ops.x || 0
		this.y = ops.y || 0
		this.z = ops.z || 0
		this.scale = ops.scale || 1
		this.backface = ops.backface || false
			
		ops.className && this.el.classList.add(ops.className)
		this.backface && this.el.classList.add("backface-visible")
		this.el.classList.add("audio")
		this.el.classList.add("mx-scenery")

		this.el.style.backgroundRepeat = 'no-repeat'
		this.paused = true

  	this.ops = ops
  },

  load: function(ops){
    if (ops) {
      ops = this.ops = defaults(ops, this.ops)
    }
    else {
      ops = this.ops
    }

  	this.width = ops.media.width
  	this.height = ops.media.height

  	var tag = Parser.lookup.soundcloud.tag(ops.media)
  	var $iframe = $(tag)
  	var iframe = $iframe[0]
  	$iframe.css('z-index', '-1')
  	this.el.appendChild( iframe )

  	var overlay = this.overlay = document.createElement("div")
  	overlay.style.width = "100%"
  	overlay.style.height = "100%"
  	overlay.style.position = "absolute"
  	overlay.style.top = "0"
  	overlay.style.left = "0"
  	overlay.style.zIndex = "2"
  	overlay.className = "overlay"
  	this.el.appendChild(overlay)
  	
  	this.player = SC.Widget( iframe )
  	this.player.setVolume(80)
  	
  	this.duration = 0
  	
  	this.player.bind(SC.Widget.Events.READY, this.ready.bind(this))
//   	this.player.bind(SC.Widget.Events.LOAD_PROGRESS, this.loadProgress.bind(this))
//   	this.player.bind(SC.Widget.Events.PLAY_PROGRESS, this.playProgress.bind(this))
  	this.player.bind(SC.Widget.Events.PLAY, this.didPlay.bind(this))
  	this.player.bind(SC.Widget.Events.PAUSE, this.didPause.bind(this))
  	this.player.bind(SC.Widget.Events.FINISH, this.finished.bind(this))
  },
  
  ready: function(){
		this.seek( this.media.keyframe || 0 )

  	if (this.media.autoplay) {
  	  this.play()
  	}

  	this.player.getDuration(function(duration){
  	  this.duration = duration
  	}.bind(this))
  },
  
  play: function(){
    this.player.play()
  },
  
  pause: function(){
    this.player.pause()
  },

  toggle: function(state){
    if (typeof state === "boolean") {
      if (state) this.play()
      else this.pause()
    }
    else {
      this.player.toggle()
    }
  },
  
  seek: function(n){
		if (n < 1) {
			n = n * this.duration
		}
		this.player.seekTo(n)
  },

  setLoop: function(state){
		this.media.loop = state
  },

  setVolume: function(n){
    if (this.muted || ! this.player) return
  	this.player.setVolume(floor( n * 100 ))
  },
  
  didPlay: function(){
    this.paused = false
  },
  
  didPause: function(){
    this.paused = true
  },
  
  finished: function(){
  	console.log("soundcloud finished")
  	if (this.media.loop) {
  		this.seek(0)
  		this.play()
  	}
  	else if (this.bound) {
  		$(".playButton").removeClass('playing')
  	}
  },
  
})