import subprocess
from tempfile import NamedTemporaryFile
from .utils import get_player_name

PLAYER = get_player_name()

def play(audio_segment):
    with NamedTemporaryFile("w+b", suffix=".wav") as f:
        audio_segment.export(f.name, "wav")
        subprocess.call([PLAYER, "-nodisp", "-autoexit", f.name])

import subprocess
from tempfile import NamedTemporaryFile
from .utils import get_player_name

PLAYER = get_player_name()



def _play_with_ffplay(seg):
	with NamedTemporaryFile("w+b", suffix=".wav") as f:
		seg.export(f.name, "wav")
		subprocess.call([PLAYER, "-nodisp", "-autoexit", f.name])


def _play_with_pyaudio(seg):
	import pyaudio

	p = pyaudio.PyAudio()
	stream = p.open(format=p.get_format_from_width(seg.sample_width),  
                	channels=seg.channels,
	                rate=seg.frame_rate,
    	            output=True)

	stream.write(seg._data)
	stream.stop_stream()  
	stream.close()  

	p.terminate()  


def play(audio_segment):
	try:
		import pyaudio
		_play_with_pyaudio(audio_segment)
	except ImportError:
		_play_with_ffplay(audio_segment)
	    

Use Pyaudio when available, ffplay as fallback
Use Pyaudio when available, ffplay as fallback
