2

我正在尝试使用 PySide 的 Phonon 模块播放一些 .flac 文件(如果它有所不同,则在 Mac 上),但它不是用于播放的可用 mimetype。有没有办法启用这个或我需要安装的插件?

4

3 回答 3

2

您可以使用PydubPyaudio播放所有流行的音频格式,包括 flac

示例代码:

#-*- 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()
于 2016-04-23T21:59:36.893 回答
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()
于 2014-04-25T09:06:59.283 回答
0

这是一系列修改,允许选择播放任何一个文件夹中的多首歌曲,限制格式以消除用户看到隐藏文件或选择非音频文件。请让我知道这是否是一个合适的帖子并批评我的代码 - 如果你这样做的话,严厉但彻底。我是初学者......我在这里的第一篇文章@ SO In 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)
于 2016-05-25T03:46:27.543 回答