当前位置: 首页>>代码示例>>Python>>正文


Python Playlist.get_current方法代码示例

本文整理汇总了Python中playlist.Playlist.get_current方法的典型用法代码示例。如果您正苦于以下问题:Python Playlist.get_current方法的具体用法?Python Playlist.get_current怎么用?Python Playlist.get_current使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在playlist.Playlist的用法示例。


在下文中一共展示了Playlist.get_current方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __init__

# 需要导入模块: from playlist import Playlist [as 别名]
# 或者: from playlist.Playlist import get_current [as 别名]
class MusicPlayer:
    def __init__(self):
        self.observers = list()
        self.player = gst.element_factory_make("playbin2", "player")
        fakesink = gst.element_factory_make("fakesink", "fakesink")
        bus = self.player.get_bus()
        bus.add_signal_watch()
        bus.connect("message", self.on_message)

        self.playing = False
        self.playlist = Playlist("New playlist")
        self.currentFile = None
        self.currentSong = None

    def get_player(self):
        return self.player

    def is_playing(self):
        return self.playing

    def set_current_file(self, cfile):
        self.currentFile = cfile

    def add_observer(self, observer):
        self.observers.append(observer)

    def notify_observers(self, message, song=None):
        for obs in self.observers:
            obs.update(message, song)

    def on_message(self, bus, message):
        t = message.type
        if t == gst.MESSAGE_EOS:
            self.player.set_state(gst.STATE_NULL)
            self.notify_observers("reset")
            self.play_next()
        elif t == gst.MESSAGE_ERROR:
            self.player.set_state(gst.STATE_NULL)
            err, debug = message.parse_error()
            print("Error {} {}".format(err, debug))
            self.notify_observers("reset")

    def set_playlist(self, pl):
        if len(pl) > 0:
            tmplist = list()
            for filepath in pl:
                infos = self.get_file_infos(filepath)
                tmplist.append(Song(infos["title"], infos["artist"], infos["album"], infos["track_number"], filepath))
            self.playlist = Playlist("New Playlist", tmplist)
            self.currentSong = self.playlist.get_current()
            self.currentFile = self.currentSong.get_path()
            self.notify_observers("update_playlist")

    def open_file(self):
        self.player.set_property("uri", "file://" + self.currentFile)

    def play(self, song=None):
        if song is not None:
            self.playlist.set_current(song)
            self.currentFile = song.get_path()
            self.currentSong = song

        if self.currentFile is not None:
            self.currentFile = self.currentSong.get_path()
            if not self.is_playing():
                self.open_file()
            self.player.set_state(gst.STATE_PLAYING)
            self.playing = True
            self.notify_observers("play", self.currentSong)

    def get_file_infos(self, sfile):
        if sfile is not None:
            mfile = auto.File(sfile)
            fileInfos = dict()
            fileInfos.update(
                {"artist": mfile.artist, "album": mfile.album, "title": mfile.title, "track_number": mfile.track}
            )
            return fileInfos

    def get_playlist(self):
        return self.playlist

    def pause(self):
        self.player.set_state(gst.STATE_PAUSED)
        self.playing = False
        self.notify_observers("pause")

    def stop(self):
        self.player.set_state(gst.STATE_NULL)
        self.notify_observers("reset")
        self.playing = False

    def play_next(self):
        if self.is_playing():
            self.stop()
            self.currentSong = self.playlist.get_next_track()
            self.play()

    def play_prev(self):
        if self.is_playing():
#.........这里部分代码省略.........
开发者ID:lesnuages,项目名称:yamp,代码行数:103,代码来源:player.py


注:本文中的playlist.Playlist.get_current方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。