How to Use the Command 'mpg321' (with Examples)
mpg321 is a versatile audio player for MPEG 1.0/2.0/2.5 layers 1, 2, and 3, which essentially covers a broad range of MP3 file types. It was developed as a free alternative to the formerly non-free mpg123 audio player. mpg321 is designed to be lightweight while delivering high performance for playing audio files, making it an ideal tool for various audio playback scenarios in both personal and professional environments.
Play an Audio Source Exactly N Times
Code:
mpg321 -l N path/to/file_a path/to/file_b
Motivation: Imagine you are setting up an exhibit where a specific audio track is supposed to loop a fixed number of times, such as for a museum display or during a presentation. The ability to specify the number of loops can be crucial for timing with other media or elements of the event.
Explanation:
-l N
: This argument specifies the number of times mpg321 should loop the playlist. When N is set to 0, the playlist loops indefinitely.path/to/file_a path/to/file_b
: These are the paths to the files you want to play. They can be local files or URLs.
Example Output: You may see a console output similar to this, indicating the audio is playing:
Playing MPEG stream from file: path/to/file_a ...
Successfully looped 3 times
Playing next file: path/to/file_b ...
Play a Directory Recursively
Code:
mpg321 -B path/to/directory
Motivation: If you have a collection of audio files organized into albums or categories within a directory, and you want to play all the files seamlessly, using this command can simplify so you don’t need to specify individual files one by one.
Explanation:
-B
: This flag tells mpg321 to process directories and subdirectories recursively, adding any audio files found directly into the playback queue.path/to/directory
: Specifies the directory to search for audio files.
Example Output: The console will list files as they are played:
Playing file: path/to/directory/song1.mp3
Playing file: path/to/directory/album/song2.mp3
Enable Basic Keys While Playing
Code:
mpg321 -K path/to/file_a path/to/file_b
Motivation: This is particularly useful when you’re in a situation where you need manual control over the playback, such as during a party or a live event. The ability to adjust the volume or skip tracks without stopping the player makes for a smoother audio experience.
Explanation:
-K
: This flag enables the ‘Basic Keys’, allowing interactive control over the playback process via keyboard input. The keys*
or/
increase or decrease volume,n
skips to the next song, andm
toggles mute.path/to/file_a path/to/file_b
: Specifies the files or streams to be played.
Example Output: Interactive keys become available, with console output like:
Playing file: path/to/file_a ...
Press keys for control: * - Increase Volume, / - Decrease Volume, n - Next Track, m - Mute
Play Files Randomly Until Interrupted
Code:
mpg321 -Z path/to/file_a path/to/file_b
Motivation: Useful in a casual setting, like a relaxing afternoon or a laid-back gathering, where spontaneity in music can add to the ambiance. Letting the player choose songs at random means less hands-on control and more enjoyment.
Explanation:
-Z
: The shuffle mode for mpg321, which will keep playing songs in random order until explicitly interrupted.path/to/file_a path/to/file_b
: Lists the audio sources to be included in the random sequence.
Example Output: Files will be played in a non-sequential, random order:
Random play: path/to/file_b
Random play: path/to/file_a
Shuffle the Files Before Playing Them Once
Code:
mpg321 -z path/to/file_a path/to/file_b
Motivation: When you have a playlist of songs you’d like to hear in a fresh order each time you play them, such as getting a new experience out of a familiar playlist, shuffling once provides that variety without repetition.
Explanation:
-z
: Shuffles the playlist before playing, ensuring they are played in a different order than listed.path/to/file_a path/to/file_b
: Audio files or URLs to shuffle and then play.
Example Output: Console output will show shuffled playback order:
Shuffled play: path/to/file_b
Shuffled play: path/to/file_a
Play All Files Randomly in the Current Directory and Subdirectories with Basic Keys Enabled
Code:
mpg321 -B -Z -K .
Motivation: An ideal solution for managing a vast music library spread across multiple subdirectories. Allows for random play, giving a surprise element while retaining manual control over the player.
Explanation:
-B
: Enables recursive search for all audio files within the target directory.-Z
: Randomizes playback order until the user stops it.-K
: Enables interactive control using basic keyboard shortcuts for volume, skipping, and muting..
: Specifies the current directory as the starting point for file searches.
Example Output: The player handles all files it finds in a random order, displaying:
Recursive, random playback in path: ./subdir/song1.mp3
Using basic controls.
Conclusion:
mpg321 provides flexibility and robust control for playing audio in various use cases. Whether you need to loop tracks, recursively play through a directory, or shuffle songs, mpg321 offers command-line arguments that cater to these requirements. With added features like basic interactive control, it makes for a comprehensive solution for anyone needing a reliable audio player in a Linux environment.