Seite 1 von 1

Python MPlayer anbindung [Kein Ton zu hören, warum?]

Verfasst: Montag 15. April 2013, 19:29
von jtschoch
Hey Pythonfreunde,

Ich habe vor mit mit meinen Raspberry einen kleinen Musikplayer mit Display zu bauen und wollte die nötige Software mit Python realisieren.
Ich habe als Anbindung MPlayer gedacht und habe den Code von der MPlayer-Anbindung den ich hatte von meinen anderen Projekt für GTK genommen.
Ich habe versucht es umzuschreiben so das es normal verwendbar ist, aber jetzt gibt es kein Ton mehr von sich.
Könnt ihr mir Helfen? Was kann ich als alternative für GObjekt nehmen?

Code: Alles auswählen

#!/usr/bin/env python

from gi.repository import GObject
import os
import fcntl
import subprocess
import time

GObject.threads_init()

class MPlayer():
    """Control for mplayer"""
    
    mplayerIn, mplayerOut = None, None
    eofHandler, statusQuery = 0, 0
    paused = False
    
    def __init__(self, display):
        self.display = display

    def play(self, target):
        """Play a file and create a pipe"""
        cmd = 'mplayer -slave '
        cmd = cmd.split()
        cmd.append(target)
        
        pipe = subprocess.Popen(cmd, shell=False, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
        (self.mplayerIn, self.mplayerOut) = (pipe.stdin, pipe.stdout)

        fcntl.fcntl(self.mplayerOut, fcntl.F_SETFL, os.O_NONBLOCK)
        
        #~Stop all
        self.stopStatusQuery()
        self.stopEofHandler()
        
        #~Start
        self.startEofHandler()
        self.startStatusQuery()

    def command_to_player(self, command):
        """Send a command to the Player"""
        
        if not self.mplayerIn:
            return
        try:
            # Write the command to Pipe
            self.mplayerIn.write(command + "\n")
            self.mplayerIn.flush()
        except StandardError:
            return

    def pause(self):
        """Rest for one song/video"""
        
        if not self.mplayerIn:
            return

        if self.paused:
            self.startStatusQuery()
            self.paused = False

        else:  
            self.stopStatusQuery()
            self.paused = True

        self.command_to_player("pause")

    def seek(self, amount, mode=0):
        """ Seek"""
        self.command_to_player("seek " + str(amount) + " " + str(mode))

    def volume(self, amount):
        """Set Volume"""
        self.command_to_player("volume " + str(amount))
        
    def close(self):
        """Close the player"""
        if self.paused:  
            self.pause()

        self.stopEofHandler()  

        self.command_to_player("quit") 

        try:
            self.mplayerIn.close()  
            self.mplayerOut.close()
        except StandardError:
            pass

        self.mplayerIn, self.mplayerOut = None, None

    def handleEof(self, source, condition):
        self.stopStatusQuery()
        self.mplayerIn, self.mplayerOut = None, None

        return False
    
    def queryStatus(self):
        self.command_to_player("get_percent_pos") 
        self.command_to_player("get_time_pos")
        #self.command_to_player("get_lenght")

        time.sleep(0.05)

        line, percent, seconds, lenght = None, -1, -1, -1
        
        while True:
            try: 
                line = self.mplayerOut.readline()
            except StandardError:
                break
            if not line:
                break
            print line
            if line.startswith("ANS_PERCENT_POSITION"):
                percent = int(line.replace("ANS_PERCENT_POSITION=", ""))

            if line.startswith("ANS_TIME_POSITION"):
                seconds = float(line.replace("ANS_TIME_POSITION=", ""))

            if line.startswith("ICY Info: StreamTitle="):  
                pass
            print seconds

        
        #statusbar = ''
        #
        #16 * ((percent / 100) / lenght)
        #self.display.message("{0}\n{1}".format(self.display.get_title(), statusbar))
        
        return True

    def startStatusQuery(self):
        self.statusQuery = GObject.timeout_add(1000, self.queryStatus)

    def stopStatusQuery(self):
        if self.statusQuery:
            GObject.source_remove(self.statusQuery)
        self.statusQuery = 0

    def startEofHandler(self):
        self.eofHandler = GObject.io_add_watch(self.mplayerOut, GObject.IO_HUP, self.handleEof)

    def stopEofHandler(self):
        if self.eofHandler:
            GObject.source_remove(self.eofHandler)
        self.eofHandler = 0
        
mplayer = MPlayer("")
mplayer.play("/home/jtschoch/Musik/Andere_Daten/retroshare.wav")
Die Ausgabe von MPlayer

Code: Alles auswählen

mplayer: could not connect to socket
mplayer: No such file or directory
Failed to open LIRC support. You will not be able to use your remote control.


MPlayer interrupted by signal 13 in module: unknown


MPlayer interrupted by signal 13 in module: demux_open

Re: Python MPlayer anbindung [Kein Ton zu hören, warum?]

Verfasst: Dienstag 16. April 2013, 10:49
von Leonidas
Wenn du eh GObject nutzt, warum dann nicht gleich auch GStreamer? Das kommt direkt mit Bindings für Python.

Re: Python MPlayer anbindung [Kein Ton zu hören, warum?]

Verfasst: Montag 22. April 2013, 21:34
von jtschoch
Danke, aber wie bekomme ich da die Gesammtlänge des Liedes und die Aktuelle Position des Titels?