Sign in to follow this  


Recommended Posts

Well, I got PyMedia to play music for me:
from OpenGL.GL import *
from OpenGL.GLU import *

from pygame import display, event
from pygame.time import Clock
from pygame.locals import *

import pymedia.muxer

from time import sleep

from os.path import exists

class Sound:
    FileName = ""
    Format = ""
    Loaded = False
    Demuxer = None
    SoundFile = None
    SoundPlayer = None
    Resampler = None
    Decoder = None
    Speed = 1
    def __init__(self, FileName = ""):
        if FileName:
    def Load(self, FileName):
        if not exists(FileName):
            raise "File does not exist."

        self.FileName = FileName
        self.Format = str.split(self.FileName, ".")[-1].lower()
        SoundCards =
        if 0 not in range(len(SoundCards)):
            raise "No sound card detected."
        self.Demuxer = pymedia.muxer.Demuxer(self.Format)
        self.SoundFile = open(self.FileName, "rb")
        self.SoundPlayer, self.Resampler, self.Decoder = None, None, None
        self.Loaded = True

    def Play(self):
        if self.Loaded:
            SoundData =
            while len(SoundData):
                SoundFrames = self.Demuxer.parse(SoundData)
                if SoundFrames:
                    for SoundFrame in SoundFrames:
                        if not self.Decoder:
                            self.Decoder =[SoundFrame[0]])
                        self.Resampled = self.Decoder.decode(SoundFrame[1])
                        if self.Resampled and
                            if not self.SoundPlayer:
                                self.SoundPlayer = * self.Speed), self.Resampled.channels,, 0)
                            FrameData =
                            if self.Resampler:
                                FrameData = self.Resampler.resample(FrameData)
                SoundData =
    def Stop(self):
        if self.SoundPlayer:
    def Pause(self):
        if self.SoundPlayer:
            if self.SoundPlayer.isPlaying():
    def Unpause(self):
        if self.SoundPlayer:
    def Rewind(self):
    def Position(self):
        if self.SoundPlayer:
            return self.SoundPlayer.getPosition()
    def IsPlaying(self):
        if self.SoundPlayer:
            return self.SoundPlayer.isPlaying()
    def SetSpeed(self, Speed):
        if Speed < 5 and Speed > 0:
            self.Speed = Speed

if __name__ == "__main__":
    Test = Sound()

(ignore the OpenGL/Pygame/etc parts) Anyway, it all works fine, but there is one major annoyance. The calls to Play() do not return control until the music is done playing. ;( Note that I did not use pygame.sound because A) I figure this is a nice learning experience <_< and B) I want a bit more control over things (such as the speed) without having to use pygames sndarray. Anyway, I've tried threading, but it's kind of an iffy subject to me. If anyone could tell me how I could set that up to use threading, I'd be appreciative. Or if there is another way. Thanks. ;)

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this