play.py 600 B

12345678910111213141516171819202122232425262728293031
  1. """PyAudio Example: Play a WAVE file."""
  2. import pyaudio
  3. import wave
  4. import sys
  5. CHUNK = 1024
  6. if len(sys.argv) < 2:
  7. print("Plays a wave file.\n\nUsage: %s filename.wav" % sys.argv[0])
  8. sys.exit(-1)
  9. wf = wave.open(sys.argv[1], 'rb')
  10. p = pyaudio.PyAudio()
  11. stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
  12. channels=wf.getnchannels(),
  13. rate=wf.getframerate(),
  14. output=True)
  15. data = wf.readframes(CHUNK)
  16. while data != '':
  17. stream.write(data)
  18. data = wf.readframes(CHUNK)
  19. stream.stop_stream()
  20. stream.close()
  21. p.terminate()