Skip to main content
Version: v1.0

MUSIC

Control the watch to play music using the speaker

Creating Sensors

const music = hmSensor.createSensor(hmSensor.id.MUSIC)

MUSIC instance

music: object

PropertiesDescriptionType
artistartist namestring
titlemusic namestring
isPlayingPlay status true: playing, false: not playingboolean
caution

Some devices do not have speakers, and if an error is reported for creating a sensor, the device does not support

music.audInit()

Initialize music controls

Type

() => void

music.audPlay()

Play

Type

() => void

music.audPause()

Pause

Type

() => void

music.audPrev()

Previous

Type

() => void

music.audNext()

Next

Type

() => void

Registering sensor instance callback events

music.addEventListener(event, callback: Callback)

The events of CHANGE

The value of event

hmSensor.event.CHANGE

Callback

() => void

The example of events

music.addEventListener(hmSensor.event.CHANGE, function () {
console.log("The current song's name: " + music.title + '\r\n')
})

Full Example

class TextByLine {
constructor(params) {
const { text = '', y = undefined, line = 0 } = params

this.text = text
this.y = y
this.line = line
this.y_computed = Number.isInteger(this.y) ? this.y : px(this.line * 60 + 120)
}

render() {
return hmUI.createWidget(hmUI.widget.TEXT, {
x: px(0),
y: this.y_computed,
w: px(480),
h: px(46),
color: 0xffffff,
text_size: px(20),
align_h: hmUI.align.CENTER_H,
align_v: hmUI.align.CENTER_V,
text_style: hmUI.text_style.NONE,
text: this.text
})
}
}

Page({
build() {
const music = hmSensor.createSensor(hmSensor.id.MUSIC)

music.audInit()

const { title, artist, isPlaying } = music

const infoText = new TextByLine({
text: `title:${title};artist:${artist};isPlaying:${isPlaying}`,
line: 0
}).render()

music.addEventListener(hmSensor.event.CHANGE, function () {
const { title, artist, isPlaying } = music

infoText.setProperty(hmUI.prop.MORE, {
text: `title:${title};artist:${artist};isPlaying:${isPlaying}`,
})
})

hmUI.createWidget(hmUI.widget.BUTTON, {
x: px(80),
y: px(240),
w: px(300),
h: px(60),
radius: px(12),
normal_color: 0xfc6950,
press_color: 0xfeb4a8,
text: 'PLAY',
click_func: () => {
music.audPlay()
}
})

hmUI.createWidget(hmUI.widget.BUTTON, {
x: px(80),
y: px(320),
w: px(300),
h: px(60),
radius: px(12),
normal_color: 0xfc6950,
press_color: 0xfeb4a8,
text: 'PAUSE',
click_func: () => {
music.audPause()
}
})

hmUI.createWidget(hmUI.widget.BUTTON, {
x: px(80),
y: px(400),
w: px(300),
h: px(60),
radius: px(12),
normal_color: 0xfc6950,
press_color: 0xfeb4a8,
text: 'PREV',
click_func: () => {
music.audPrev()
}
})

hmUI.createWidget(hmUI.widget.BUTTON, {
x: px(80),
y: px(480),
w: px(300),
h: px(60),
radius: px(12),
normal_color: 0xfc6950,
press_color: 0xfeb4a8,
text: 'NEXT',
click_func: () => {
music.audNext()
}
})
}
})