Jump to content
  • Advertisement
Sign in to follow this  


This topic is 3893 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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:
    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)
                SoundData = self.SoundFile.read(512)
    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
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!