Using afplay (with examples)
- Osx
- November 5, 2023
Play a sound file (waits until playback ends)
To play a sound file using the afplay
command, simply provide the path to the file as an argument. The command will wait until the playback of the sound file is complete before returning.
Code:
afplay path/to/file
Motivation:
This use case is helpful when you want to play a sound or audio file and ensure that it is fully played before executing the next command. It allows you to synchronize the playback with other actions or scripts that depend on the completion of the audio playback.
Example Output:
Playing the sound file specified by path/to/file
until the playback ends.
Play a sound file at 2x speed (playback rate)
To play a sound file at a specific playback rate, you can use the --rate
option followed by the desired value. In this example, we set the playback rate to 2, which plays the audio file at double the normal speed.
Code:
afplay --rate 2 path/to/file
Motivation:
Increasing the playback rate allows you to listen to the audio content more quickly. This can be useful when you want to review audio recordings or lectures in a shorter amount of time without missing any important information.
Example Output:
Playing the sound file specified by path/to/file
at 2x speed.
Play a sound file at half speed
To play a sound file at a slower rate, you can set the --rate
option to a value less than 1. In this example, we set the playback rate to 0.5, which plays the audio file at half its normal speed.
Code:
afplay --rate 0.5 path/to/file
Motivation:
Decreasing the playback rate can help you analyze audio content more carefully. It allows for a detailed examination of the audio, making it easier to transcribe or understand specific parts that may require more attention.
Example Output:
Playing the sound file specified by path/to/file
at half speed.
Play the first N seconds of a sound file
If you only want to play a specific portion of a sound file, you can use the --time
option followed by the desired length in seconds. This allows you to play only the specified duration of the audio file.
Code:
afplay --time seconds path/to/file
Motivation:
Playing only a portion of a sound file can be useful when you want to quickly review or verify a specific section without playing the entire file. This can save time, especially for longer audio recordings or when you are specifically interested in a particular segment.
Example Output:
Playing the first N seconds (specified by seconds
) of the sound file specified by path/to/file
.