python3 写一个WAV音频文件播放器的代码

yipeiwu_com7年前Python基础

环境:ubuntu 16.04 python3.5 pycharm

包 : wave pyaudio sys

上代码:AudioPlayer.py

# coding:utf-8
# author:king
# brief : 播放wav音频文件
import wave
from pyaudio import PyAudio
import sys
def player(filename):
  chunk = 1024
  wf = wave.open(filename, 'rb')
  p = PyAudio()
  stream = p.open(format=p.get_format_from_width(wf.getsampwidth()), channels=wf.getnchannels(),
          rate=wf.getframerate(), output=True)
  data = wf.readframes(chunk)
  print(data)
  while data != b'':
    data = wf.readframes(chunk)
    stream.write(data)
  stream.stop_stream()
  stream.close()
  p.terminate()
if __name__ == '__main__':
  audiofile = sys.argv[1];
  player(audiofile);

使用方法是直接用pyinstaller 生成可执行文件

pyinstaller -F AudioPlayer.py

在dist下即可找到生成的可执行文件复制到/usr/bin/下即可使用

使用方法是 AudioPlayer dingwav

总结

以上所述是小编给大家介绍的python3 写一个WAV音频文件播放器的代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对【听图阁-专注于Python设计】网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

相关文章

python读写csv文件的方法

python读写csv文件的方法

1.爬取豆瓣top250书籍 import requests import json import csv from bs4 import BeautifulSoup books =...

Python实现计算对象的内存大小示例

本文实例讲述了Python实现计算对象的内存大小。分享给大家供大家参考,具体如下: 一般的sys.getsizeof()显示不了复杂的字典。 查看类中的内容: def dump(ob...

python 协程 gevent原理与用法分析

本文实例讲述了python 协程 gevent原理与用法。分享给大家供大家参考,具体如下: gevent greenlet已经实现了协程,但是这个还的人工切换,是不是觉得太麻烦了,不要捉...

使用Python操作Elasticsearch数据索引的教程

使用Python操作Elasticsearch数据索引的教程

Elasticsearch是一个分布式、Restful的搜索及分析服务器,Apache Solr一样,它也是基于Lucence的索引服务器,但我认为Elasticsearch对比Solr...

浅谈Python由__dict__和dir()引发的一些思考

关于__dict__和dir()的区别和作用请参考这篇文章: 基于Python __dict__与dir()的区别详解 说下我当时遇到的问题: class Demo: def _...