Sign in to follow this  
Koyeall

PyMedia

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
import pymedia.audio.acodec
import pymedia.audio.sound

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:
            self.Load(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 = pymedia.audio.sound.getODevices()
        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 = self.SoundFile.read(32000)
            
            while len(SoundData):
                SoundFrames = self.Demuxer.parse(SoundData)
                if SoundFrames:
                    for SoundFrame in SoundFrames:
                        if not self.Decoder:
                            self.Decoder = pymedia.audio.acodec.Decoder(self.Demuxer.streams[SoundFrame[0]])
                        
                        self.Resampled = self.Decoder.decode(SoundFrame[1])
                        if self.Resampled and self.Resampled.data:
                            if not self.SoundPlayer:
                                self.SoundPlayer = pymedia.audio.sound.Output(int(self.Resampled.sample_rate * self.Speed), self.Resampled.channels, pymedia.audio.sound.AFMT_S16_LE, 0)
                            
                            FrameData = self.Resampled.data
                            if self.Resampler:
                                FrameData = self.Resampler.resample(FrameData)
                            
                            self.SoundPlayer.play(FrameData)
                            
                SoundData = self.SoundFile.read(512)
    
    def Stop(self):
        if self.SoundPlayer:
            self.SoundPlayer.stop()
                
    def Pause(self):
        if self.SoundPlayer:
            if self.SoundPlayer.isPlaying():
                self.SoundPlayer.pause()
   
    def Unpause(self):
        if self.SoundPlayer:
            self.SoundPlayer.unpause()
    
    def Rewind(self):
        self.Stop()
        self.Play()
    
    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()
    Test.Load("Test.mp3")
    Test.Play()

(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