-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmedia-manager.js
140 lines (131 loc) · 4.06 KB
/
media-manager.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
138
139
140
'use strict'
var AudioFocus = require('@yodaos/application').AudioFocus
var MediaPlayer = require('@yoda/multimedia').MediaPlayer
var AudioManager = require('@yoda/audio').AudioManager
var tts = require('@yodaos/speech-synthesis').speechSynthesis
var logger = require('logger')('alarm-MediaManager')
/**
* @class MediaManager
*/
function MediaManager (alarmCore) {
this.alarmCore = alarmCore
this.audioFocus = new AudioFocus(AudioFocus.Type.TRANSIENT)
this.audioFocus.player = new MediaPlayer(this.streamType || AudioManager.STREAM_ALARM)
this.audioFocus.onGain = () => {
this.alarmCore._preventEventsDefaults()
this.audioFocus.isFocus = true
this.audioFocus.resumeOnGain = false
var player = this.audioFocus.player
if (!player) {
player = this.audioFocus.player = new MediaPlayer(this.streamType || AudioManager.STREAM_ALARM)
}
this.setListener(player)
if (this.audioFocus.playUrl) {
player.start(this.audioFocus.playUrl)
}
}
this.audioFocus.onLoss = (transient, mayDuck) => {
logger.info('audioFocus---->onLoss; transient: ', transient)
logger.info('audioFocus---->onLoss; mayDuck: ', mayDuck)
this.alarmCore.restoreEventsDefaults()
this.audioFocus.isFocus = false
var player = this.audioFocus.player
if (!player) {
return
}
this.stopMedia()
this.audioFocus.playUrl = null
this.alarmCore.clearAll()
}
}
MediaManager.prototype.playAudio = function (url, option) {
logger.info('playAudio------>url', url)
logger.info('playAudio------>isFocus', this.audioFocus.isFocus)
this.streamType = (option && option.streamType) || AudioManager.STREAM_ALARM
this.loop = (option && option.loop) || false
this.audioFocus.playUrl = url
if (this.audioFocus.isFocus) {
this.stopMedia()
this.audioFocus.player.start(url)
logger.info('playAudio------>start ', url)
} else {
this.audioFocus.request()
}
return Promise.resolve()
}
MediaManager.prototype.release = function () {
if (this.audioFocus) {
this.audioFocus.abandon()
}
}
MediaManager.prototype.stopMedia = function stopMedia () {
var player = this.audioFocus.player
if (player) {
player.stop()
player.reset()
}
player = this.audioFocus.player = new MediaPlayer(this.streamType || AudioManager.STREAM_ALARM)
this.setListener(player)
}
MediaManager.prototype.setListener = function setListener (player) {
player.on('prepared', (id, duration, position) => {
this.isPlaying = true
logger.info(`player prepared----> id:${id}, duration:${duration}, position:${position}`)
})
player.on('start', () => {
logger.info('player start----> is called')
})
player.on('seekcomplete', (id) => {
logger.info(`player seekcomplete----> id:${id}`)
})
player.on('error', (id) => {
this.isPlaying = false
logger.info(`player error----> id:${id}`)
})
player.on('cancel', (id) => {
this.isPlaying = false
logger.info(`player cancel----> id:${id}`)
})
player.on('playbackcomplete', (id) => {
this.isPlaying = false
logger.info(`player playbackcomplete----> id:${id}`)
if (this.loop) {
this.stopMedia()
this.audioFocus.player.start(this.audioFocus.playUrl)
}
})
player.on('paused', (id) => {
logger.info(`player paused----> id:${id}`)
})
player.on('resumed', (id) => {
this.isPlaying = true
logger.info(`player resumed----> id:${id}`)
})
}
MediaManager.prototype.stopMediaAndTts = function stopMediaAndTts () {
logger.info('stopMediaAndTts----> this.audioFocus.isFocus is ', this.audioFocus.isFocus)
if (this.audioFocus.isFocus) {
this.stopMedia()
tts.cancel()
this.release()
}
}
MediaManager.prototype.speakTts = function speakTts (content, callback) {
var _callback = callback
return new Promise((resolve, reject) => {
if (!tts) {
_callback('tts not inited')
}
tts.speak(content)
.once('end', () => {
_callback('end')
})
.once('cancel', () => {
_callback('cancel')
})
.once('error', () => {
_callback('error')
})
})
}
exports.MediaManager = MediaManager