我正在尝试使用PySide的Phonon模块播放一些.flac文件(如果它有所不同,在Mac上),但它不是一个可用的播放模式。有没有办法启用这个或我需要安装的插件?
答案 0 :(得分:2)
示例代码:
#-*- coding: utf-8 -*-
from pydub import AudioSegment
from pydub.utils import make_chunks
from pyaudio import PyAudio
from threading import Thread
class Song(Thread):
def __init__(self, f, *args, **kwargs):
self.seg = AudioSegment.from_file(f)
self.__is_paused = True
self.p = PyAudio()
print self.seg.frame_rate
Thread.__init__(self, *args, **kwargs)
self.start()
def pause(self):
self.__is_paused = True
def play(self):
self.__is_paused = False
def __get_stream(self):
return self.p.open(format=self.p.get_format_from_width(self.seg.sample_width),
channels=self.seg.channels,
rate=self.seg.frame_rate,
output=True)
def run(self):
stream = self.__get_stream()
chunk_count = 0
chunks = make_chunks(self.seg, 100)
while chunk_count <= len(chunks):
if not self.__is_paused:
data = (chunks[chunk_count])._data
chunk_count += 1
else:
free = stream.get_write_available()
data = chr(0)*free
stream.write(data)
stream.stop_stream()
self.p.terminate()
song = Song("song.flac")
song.play()
答案 1 :(得分:1)
Phonon不直接支持音频格式,但使用底层操作系统功能。因此,答案取决于是否有为mime类型audio/flac
注册的服务。对我来说,有一个简短的示例脚本可以找到:
from PySide import QtCore
from PySide.phonon import Phonon
if __name__ == '__main__':
app = QtCore.QCoreApplication([])
app.setApplicationName('test')
mime_types = Phonon.BackendCapabilities.availableMimeTypes()
print(mime_types)
app.quit()
答案 2 :(得分:0)
以下是一系列修改,允许选择任意一个文件夹中的多首歌曲进行播放,限制格式以消除用户看到隐藏文件或选择非音频文件。如果这是一个合适的帖子并批评我的代码,请告诉我 - 如果你这样做,那就严厉但彻底。我是初学者...我的第一篇文章@ S.O.在python3.x中:
#-*- coding: utf-8 -*-
from threading import Thread
from pyaudio import PyAudio
from pydub import *
from pydub.utils import make_chunks
from tkinter.filedialog import askopenfilenames
from tkinter import Tk
import time
class Song(Thread):
def __init__(self, f, *args, **kwargs):
self.seg = AudioSegment.from_file(f)
self.__is_paused = True
self.p = PyAudio()
print(self.seg.frame_rate)
Thread.__init__(self, *args, **kwargs)
self.start()
def pause(self):
self.__is_paused = True
def play(self):
self.__is_paused = False
def __get_stream(self):
return self.p.open(format=self.p.get_format_from_width(self.seg.sample_width),
channels=self.seg.channels,
rate=self.seg.frame_rate,
output=True)
def run(self):
stream = self.__get_stream()
chunk_count = 0
chunks = make_chunks(self.seg, 100)
while chunk_count < len(chunks) and not self.__is_paused:
data = (chunks[chunk_count])._data
chunk_count += 1
stream.write(data)
stream.stop_stream()
self.p.terminate()
Tk().withdraw()
filename = askopenfilenames(initialdir='/default/path', title="choose your file(s)",
filetypes=(("audio files", "*.flac *.wav *.mp3 *.ogg"), ("none", "")))
# with this logic there is a short gap b/w files - os time to process,
# trying to shorten gap by removing
# 1 second from sleep time... works ok but will be system status
# dependent and may not work across runs??
# Better would be to kill each tread when self._is_paused = True.
# I have a bunch of active threads piling up
for a in filename:
song = Song(a)
song.play()
songLength = song.seg.duration_seconds
time.sleep(songLength - 1)