聲音#
介紹#
VEX AIR 無人機控制器可讓使用者播放內建音效、自訂音訊檔案和音符。它還包含用於停止聲音和偵測音訊是否正在播放的控制功能。
以下是所有可用方法的清單:
操作 - 播放和停止聲音
獲取器 - 取得聲音狀態
is_active— 檢查目前是否正在播放聲音。
行動#
play#
play 以指定的音量百分比播放控制器內建的某個聲音。由於這是一種非等待方法,控制器會播放內建聲音後立即執行下一個命令,而不會等待其播放完畢。
用法:
controller.sound.play(sound, volume)
範圍 |
描述 |
|---|---|
|
下面列出的內建聲音選項之一。 |
|
可選。音量大小,取值範圍為 0% 到 100% 的整數。預設值為 50%。 |
內建聲音 |
播放聲音 |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Play the looping sound.
controller.sound.play(LOOPING)
# Play the looping sound at full volume.
controller.sound.play(LOOPING, 100)
# Play the looping sound.
# Wait until the sound is finished before taking off.
controller.sound.play(LOOPING)
while controller.sound.is_active():
wait(50, MSEC)
drone.take_off(climb_to=500)
wait(1, SECONDS)
drone.land()
play_file#
play_file 以指定的音量百分比播放使用者載入的自訂聲音。由於這是一個非等待方法,無人機會在播放自訂聲音後立即執行下一個命令,而無需等待其播放完畢。
用法:
controller.sound.play_file(file, volume)
範圍 |
描述 |
|---|---|
|
使用者上傳的自訂聲音檔案之一,編號從 |
|
可選。音量大小,取值範圍為 0% 到 100% 的整數。預設值為 50%。 |
# Upload a sound file in VEXcode.
# Play the custom sound.
controller.sound.play_file(SOUND1)
# Upload a sound file in VEXcode.
# Play the custom sound at full volume.
controller.sound.play_file(SOUND1, 100)
# Play the custom sound sound.
# Wait until the sound is finished before taking off.
controller.sound.play_file(SOUND1)
while controller.sound.is_active():
wait(50, MSEC)
drone.take_off(climb_to=500)
wait(1, SECONDS)
drone.land()
play_note#
play_note 會播放特定音符,時長指定。由於這是一個非等待方法,無人機會播放特定音符後立即執行下一個命令,而無需等待其結束。
用法:
controller.sound.play_note(note, duration, volume)
範圍 |
描述 |
|---|---|
|
要播放的音高,以字符串形式书写(例如 |
|
音符的持續時間,以毫秒為單位的整數表示。 |
|
可選。音符音量,取值範圍為 0% 到 100% 的整數。預設值為 50%。 |
# Play C5 for 2 seconds
controller.sound.play_note("C5", 2000)
# Play C5 for 2 seconds at full volume
controller.sound.play_note("C5", 2000, 100)
# Play a short tune.
controller.sound.play_note("C5", 1000)
while controller.sound.is_active():
wait(50, MSEC)
controller.sound.play_note("D5", 1000)
while controller.sound.is_active():
wait(50, MSEC)
controller.sound.play_note("E5", 250)
while controller.sound.is_active():
wait(50, MSEC)
stop#
stop 停止目前正在播放的聲音。
用法:
controller.sound.stop()
參數 |
描述 |
|---|---|
此方法沒有參數。 |
# Upload a sound 10 seconds or greater in VEXcode.
# Stop the sound while it is playing.
controller.sound.play_file(SOUND1)
wait(6, SECONDS)
controller.sound.stop()
吸氣劑#
is_active#
is_active 回傳一個布林值,指示無人機目前是否正在播放聲音。
True— 目前正在播放聲音False— 沒有聲音播放
用法:
controller.sound.is_active()
參數 |
描述 |
|---|---|
此方法沒有參數。 |
# Display text once a sound starts playing
controller.sound.play(LOOPING)
while not controller.sound.is_active():
wait(50, MSEC)
print("I hear the sound!")