V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
cosven
V2EX  ›  问与答

PyQt5 边缓存,边听歌... FeelUOwn 更新贴

  •  
  •   cosven · 2015-11-22 21:27:44 +08:00 · 2735 次点击
    这是一个创建于 3290 天前的主题,其中的信息可能已经有所发展或是发生改变。

    FeelUOwn 更新贴

    FeelUOwn 是一款为 Linux 专门开发的音乐软件。目前相当于一个第三方版的网易云音乐。Python Qt Linux

    旧贴 http://v2ex.com/t/235214#reply33

    • 新增加了歌单缓存,歌曲信息缓存。
    • 修复一些已知的 bug

    歌曲缓存指日可待... 哭笑脸

    对网速不好的朋友来说应该是个好消息.....

    截图来一发:

    v5.0alpha 截图

    最后附一段 Python + PyQt5 边缓存边听歌的解决方案 demo 版: (也是费劲苦心... Google 都被我用坏了,最后终于写了个 demo)

    #! /usr/bin/env python3
    import sys
    import time
    
    from PyQt5 import QtWidgets
    from PyQt5.QtWidgets import *
    from PyQt5.QtMultimedia import *
    from PyQt5.QtGui import *
    from PyQt5.QtCore import *
    
    from _thread import start_new_thread
    from threading import Thread, RLock
    
    import requests
    import os
    
    
    # secret base 
    mp3_url = 'http://m1.music.126.net/Gybpf5bX9zfNesjXxZl3qw==/2053887720715417.mp3'
    
    # chedan
    mp3_url = 'http://m2.music.126.net/7tEJKStKIbxKPVKw7urYkA==/6033020302257380.mp3'
    
    # liangzhu
    mp3_url = 'http://m2.music.126.net/Jx2_zeePijuCGjqGSgTGyw==/6656443395918618.mp3'
    
    global i
    i = 0
    
    def print_duration(duration):
        time_text = QTime(0, (duration / 60000) % 60, (duration/ 1000) % 60)
        print("duration:", time_text.toString("mm:ss"))
    
    def print_position(position):
        time_text = QTime(0, (position/ 60000) % 60, (position/ 1000) % 60)
        print('position:', time_text.toString("mm:ss"), i)
    
    def print_media_changed(media):
        print ("media changed")
    
    def print_buffer(status):
        print ('buffer', status)
    
    
    class QFileWithLock(QFile):
        def __init__(self, path):
            super().__init__(path)
            self.lock = RLock()
            self.use_lock = True
    
        def disable_lock(self):
            self.use_lock = False
    
        def enable_lock(self):
            self.use_lock = True
    
        def seek(self, pos):
            if self.use_lock:
                print('use lock')
                self.lock.acquire()
                flag = super().seek(pos)
                self.lock.release()
            else:
                flag = super().seek(pos)
            return flag
    
        def write_to_end(self, data):
            print ('write_to_end wait for lock')
            self.lock.acquire()
            print ('get lock')
            super().seek(self.size())
            data = super().write(data)
            self.lock.release()
            return data
    
    
    class Player(QObject):
        def __init__(self):
            super().__init__()
            self.player = QMediaPlayer(flags=QMediaPlayer.StreamPlayback)
            # self.player.positionChanged.connect(print_position)
            self.player.durationChanged.connect(print_duration)
            self.player.currentMediaChanged.connect(print_media_changed)
            self.player.bufferStatusChanged.connect(print_buffer)
            self.tmp_pos = 0
    
        def start(self):
            print('start')
            start_new_thread(self.async, ())
            time.sleep(2)
            self.player.setMedia(QMediaContent(), self.music)
            self.player.play()
    
        def async(self):
            global i
            self.res = requests.get(mp3_url, stream=True, timeout=1)
            self.music = QFileWithLock('temp.mp3')
            # self.music = QBuffer()
            flag = self.music.open(QIODevice.ReadWrite)
            for chunk in self.res.iter_content(1024000):
                print('i:', i, end='')
                self.player.pause()
                print ('file seek pos: ', self.music.pos(), end='')
                # self.music.seek(self.music.size())
                # self.music.write(chunk)
                self.music.write_to_end(chunk)
                print ('file seek pos: ', self.music.pos())
                self.player.play()
                i += 1
    
            print("finished")
            self.music.disable_lock()
    
    
    class Widget(QWidget):
        def __init__(self):
            super().__init__()
            self.player = Player()
            self.button = QPushButton('play', self)
            self.button.clicked.connect(self.player.start)
    
    app = QtWidgets.QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec_())
    
    2 条回复    2015-11-22 21:51:42 +08:00
    cosven
        1
    cosven  
    OP
       2015-11-22 21:37:43 +08:00
    我想删除这个帖子,然而貌似并不可以 ...
    bdbai
        2
    bdbai  
       2015-11-22 21:51:42 +08:00 via iPhone
    Secret Base 赞~
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2846 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 44ms · UTC 14:59 · PVG 22:59 · LAX 06:59 · JFK 09:59
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.