PyQt5 听歌缓存 && FeelUOwn 更新贴

2015-11-22 21:37:12 +08:00
 cosven
### FeelUOwn 更新贴

[FeelUOwn]( https://github.com/cosven/FeelUOwn) 是一款为 Linux 专门开发的音乐软件。目前相当于一个第三方版的网易云音乐。`Python` `Qt` `Linux`

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

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

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

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

截图来一发:

![v5.0alpha 截图]( http://7xnn7w.com1.z0.glb.clouddn.com/v5.0a.png)

最后附一段 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_())
```
4927 次点击
所在节点    Linux
25 条回复
cosven
2015-11-23 12:39:49 +08:00
@hei1000 Soga , 非常感谢
ehs2013
2015-11-23 15:29:07 +08:00
@Arthur2e5 我记得 API 没版权吧。另外就算是比较弱的 LGPL (Lesser GPL) 也是要求你把非 LGPL (闭源)部分的 object 文件可以让其他人使用其他版本的 LGPL 的那个库来链接的。
fly3949
2015-11-23 20:54:21 +08:00
壁纸居然是言叶之庭 QAQ !赞
Arthur2e5
2015-11-23 21:56:34 +08:00
@ehs2013 你提到的那点正是 LGBL 宽松的地方啊……
Arthur2e5
2015-11-23 21:56:56 +08:00
@Arthur2e5 …… LGPL …………………………………………………………

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/238101

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX