![]() Brew suggested that I should run the command below. Brew is a good installation utility, and it told me what I needed to do. My first attempt ran into a problem because I was missing the xcode command-line capabilities. #PLAY SOUNDS PYTHON INSTALL#I installed mpg123 on my MacBook using the Brew installation utility as follows: brew install mpg123 Importantly to us, it allows starting/stopping and quitting playback before an mp3 completes playing. I also learned that it allows user interaction via the stdin stream while it is playing a song. Reading its documentation, I learned it works on my MacBook as well as on other platforms, for example, Windows and Raspbian. Web research found me a player named mpg123. Since we want this capability in our MusicPlayer, we will not use afplay. ![]() But I learned that there is no support for starting/stopping and quitting playback of a currently playing song. I tried it out to be sure I could play an mp3 using it. I develop it on my MacBook, and I have learned that MacOS Monterey provides a built-in player named afplay. Depending on the external application’s capabilities, access to these streams can provide the MusicPlayer class with what is needed for a playback API. After launching an external application, subprocess provides access to the stdin, stdout, and stderr streams used by the application. The MusicPlayer should also provide a playback API to stop and restart a song and to terminate playback if a user wants to start a new song before an old one finishes. Python provides the subprocess module to launch and control external applications. External applications run in their own process separate from Python’s process. Python does not itself play music, so the MusicPlayer class developed here needs to use an external application to do the job. #PLAY SOUNDS PYTHON DRIVER#My UI is not a UI at all, it's just a terminal-based driver that uses the class, so it is very simple. My article is not about making a nice player application, it is about building a reusable Python class that uses subprocess to launch and control an external process. These combine nicely to produce a player with a GUI. Most of these use high-level modules like pygame (to play music) and tkinter (to provide a UI). Please note: There are lots of great Python music player projects that you can find on the web. I decided that a reusable MusicPlayer class would be a worthy topic. So my challenge was to think of a project that readers might find interesting. I prefer to write an engaging story about Python’s subprocess module (that’s a laugh) rather than just present a few facts that can be found elsewhere. ![]() I enjoy writing, and this felt like a topic that could be interesting to write about.īut I know that reading about syntax and APIs is not very much fun. I recently needed to launch and control an application from Python, so I had to learn about the subprocess module. #PLAY SOUNDS PYTHON CODE#The Python code and mpg123 should run on Windows, but this was not attempted by me. ![]() Code was developed using Python 3.9 on an Apple M1 MacBook running Monterey 12.1. ![]() A terminal-based driver is used to demonstrate the class. A MusicPlayer Class is developed that launches and controls the audio player application mpg123 to play mp3 files on your system. TL DR: Read this to learn a few things (but not close to everything) about Python’s subprocess module. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |