Python Forum
Sound-player standalone
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sound-player standalone
#1
Gone look at making at making simple standalone Sound-player that can play most format eg .mp3.
Gone make it for Windows and Linux.
The goal is one standalone file(Windows:play.exe and Linux:play) this file can be moved to any OS and just work.

Install
Tool for this pipenv, pyinstaller, pyglet and Click.
Start with pipenv which is great for keeping track of dependencies,it's a marriage Heart of pip and virtualenv.
# Install pipenv
pip install -U pipenv

# Now pipenv in a folder eg (mkdir)sound_env
pipenv install pyinstaller, pyglet, click

# Activate sound_env folder
pipenv shell
Take a look at dependencies in Windows and Linux.
pipenv graph
See all dependencies is installed also newer pypiwin32 and not older win32api,
which could be used i doing this without using virtual environment.

Code
import pyglet
import click
from msvcrt import getch
pyglet.lib.load_library('avbin.dll')
pyglet.have_avbin=True

@click.command()
@click.argument('file', type=click.Path(exists=True), metavar='<file>')
def play(file):
    '''
    Play sound file from command line Supported format:
    AU,MP2,MP3,OGG/Vorbis,WAV,WMA
    
    \b
    Example:
    play song.mp3
    # File with space use quote.
    play "Clean Bandit.mp3"        
    <Space> quit the song
    '''
    music = pyglet.media.load(file)
    music.play()
    while True:
        key = ord(getch())
        # 32 use space to quit
        if key == 32:
            break
        pyglet.app.run()

if __name__ == '__main__':
    play()
Test code:
E:\sound_env
λ which python
/c/Users/Tom/.virtualenvs/sound_env-zFUYyERb/Scripts/python

E:\sound_env
λ python play.py "Clean Bandit.mp3"

E:\sound_env
λ
Linux:
Pyglet for playing which is using AVbin avbin.dll(windows) and libavbin.so.10(Linux),
even if i try to make clear so do not Pyinstaller find these files,so add it manually.

Click is great command line tool,type=click.Path(exists=True) will do error checking so we don't.
metavar='<file>' will use docstring of function when do play --help

Build
Build with Pyinstaller:
Command for Windows:
pyinstaller --onefile --console --icon=sound.ico --add-data avbin.dll;. play.py

Command for Linux:
pyinstaller --onefile --console --icon=sound.ico --add-data libavbin.so.10:. play.py
There is a little difference ;. Win and Linux :..
. - Destination path of dll in .exe or play file(Linux).
Every time Pyinstaller run a .spec file is generated,in this case play.spec
This file can be edit to add more stuff,then run like this.
pyinstaller --clean play.spec

Usage
It's a very simple player,eg place play.exe it environment variable Path(Windows),
and it work from anywhere in cmd.
Example:
# Excutebale
mint@mint ~/sound_env/dist $ chmod +x play

# Use
mint@mint ~/sound_env/dist $ ./play --help
Usage: play [OPTIONS] <file>

  Play sound file from command line Supported format:
  AU,MP2,MP3,OGG/Vorbis,WAV,WMA

  Example:
  play song.mp3
  # File with space use quote.
  play "Clean Bandit.mp3"        
  <Ctrl+c> quit the song

# Play Song Linux  
mint@mint ~/sound_env/dist $ ./play faded.mp3

# Play Song windows
C:\1>play "Clean Bandit.mp3"
Look with icon Windows:
[Image: SWhvZx.jpg]

Take away
A look a pipenv a cool that keep track of dependencies and make pip and virtual environment work with one command.
Pyinstaller a good tool for bundle all dependencies is single single package.
Click for creating beautiful command line interfaces,in my option the best of the bunch.
Reply


Messages In This Thread
Sound-player standalone - by snippsat - Mar-17-2018, 11:16 PM
RE: Sound-player standalone - by snippsat - Mar-21-2018, 08:17 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020