声音#

介绍#

VEX AIR 无人机控制器允许用户播放内置音效、自定义音频文件和音符。它还包含用于停止声音和检测音频是否正在播放的控制功能。

以下是所有可用方法的列表:

操作 - 播放和停止声音

  • play — Plays a built-in sound from a predefined list.

  • play_file — Plays a user-uploaded sound.

  • play_note — Plays a musical note with a specified pitch, octave, and duration.

  • stop — Stops any currently playing sound.

Getters - Get Status of Sounds

  • is_active — Checks if a sound is currently playing.

行动#

play#

play plays one of the controller’s built-in sounds at a specified volume percentage. Since this is a non-waiting method, the controller plays the built-in sound and moves to the next command without waiting for it to finish.

用法:

controller.sound.play(sound, volume)

范围

描述

sound

下面列出的内置声音选项之一。

volume

Optional. The volume for the sound, as an integer from 0% to 100%. The default is 50%.

内置声音

播放声音

SUCCESS

FAULT

SENSING

DETECTED

OBSTACLE

LOOPING

COMPLETE

PAUSE

RESUME

SEND

RECEIVE

# 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 plays a custom sound loaded by the user at a specified volume percentage. Since this is a non-waiting method, the drone plays the custom sound and moves to the next command without waiting for it to finish.

用法:

controller.sound.play_file(file, volume)

范围

描述

file

One of the custom sound files uploaded by the user, from SOUND1 to SOUND10. The sound number aligns with the number shown in the AIR Control Panel.

volume

Optional. The volume for the sound, as an integer from 0% to 100%. The default is 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 plays a specific note for a specific duration. Since this is a non-waiting method, the drone plays the specific note and moves to the next command without waiting for it to finish.

用法:

controller.sound.play_note(note, duration, volume)

范围

描述

note

The musical pitch to play, written as a string (e.g., C5, A#6). Valid notes are A through G (with optional # for sharps) across octaves 5 to 8.

duration

音符的持续时间,以毫秒为单位的整数表示。

volume

Optional. The volume of the note, as an integer from 0% to 100%. The default is 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 stops a sound that is currently playing.

用法:

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 returns a Boolean indicating whether the drone is currently playing a sound.

  • True — A sound is currently playing

  • False — No sound is playing

Usage:
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!")