pythonでfoobarのalternativeを作る 22 version 28
:追加された部分
:削除された部分
(差分が大きい場合、文字単位では表示しません)
pythonでfoobarのalternativeを作る 22
## Player
190531-0602
https://live.staticflickr.com/65535/47985537206_2be1771ebc_z.jpg
- 音が出るようになった(・∀・)ノ
- treeのインクリメンタルは一文字のアルファベットや数字は無視するようにしたので結構快適になってきた
- 右下のリストをきちんと作った
treeからのドラッグでリストに追加されます
これって自分で実装しなくてもリストのドラッグの属性設定だけで出来ちゃうんだなあ、すごいわ (゚Д゚)
- リストのダブルクリックで再生
treeとListって基本同じだ
Qtのこのwidgetは使いづらいと思ってたけど慣れたら簡単でいいのかもしれない
- toolbar を付けた、やっぱり無いと不便(¯―¯٥)
- pickle を使ってtreeのmodelを保存したかったんだけどエラーで出来なかった
ショック・・・(¯―¯٥)
- タグのビューはvlcで取得した
mutageの変わりにこれを使ったら速さはどうだろう?
```
import sqlite3 , re ,vlc ,time
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication,QWidget,QMainWindow ,QAction , QListView,QVBoxLayout
from PyQt5.QtGui import QStandardItemModel , QStandardItem
from PyQt5.QtCore import QTimer
import ui
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.ui = ui.Ui_MainWindow()
self.ui.setupUi(self)
self.db=r'e:\Programs\python\_研究\database\database.db'
line='select artist ,album,title ,path from musics order by artist ,album,title'
self.model=self.sqlexecute(line)
self.ui.treeView.setModel(self.model) # tree
self.model2 = QStandardItemModel()
self.ui.listView.setModel(self.model2) # list_tag
self.setaction()
self.makenewtab('default')
self.currentfile=r'(¯―¯٥)'
self.p = vlc.MediaPlayer()
self.timer = QtCore.QTimer(self)
self.timer.setInterval(500)
self.timer.timeout.connect(self.ShowInfo)
def setaction(self):
self.ui.actionBye.triggered.connect(self.actionbye)
self.ui.actionchange.triggered.connect(self.actionchange)
self.ui.actionclear.triggered.connect(self.actionclear)
self.ui.actionreload.triggered.connect(self.actionreload)
self.ui.actionplay.triggered.connect(self.actionplay)
self.ui.actionpause.triggered.connect(self.actionpause)
self.ui.actionstop.triggered.connect(self.actionstop)
actionPlay = QAction('Play', self)
actionPlay.triggered.connect(self.actionplay)
actionPause = QAction('Pause', self)
actionPause.triggered.connect(self.actionpause)
actionStop = QAction('Stop', self)
actionStop.triggered.connect(self.actionstop)
actionBye = QAction('BYE', self)
actionBye.triggered.connect(self.actionbye)
self.ui.toolBar.addAction(actionPlay)
self.ui.toolBar.addAction(actionPause)
self.ui.toolBar.addAction(actionStop)
self.ui.toolBar.addAction(actionBye)
def makenewtab(self,name):
obj = QtWidgets.QListView()
self.ui.tabWidget.addTab(obj, name)
self.ui.tabWidget.setCurrentWidget(obj)
lyt = QVBoxLayout()
lyt.setContentsMargins(0, 0, 0, 0)
lyt.setSpacing(0)
obj.setLayout(lyt)
model = QStandardItemModel()
obj.setModel(model)
obj.setEditTriggers(QtWidgets.QAbstractItemView.NoEditTriggers)
obj.doubleClicked.connect(self.ListdoubleClicked)
obj.setDragEnabled(True)
obj.setDragDropMode(QtWidgets.QAbstractItemView.DragDrop)
self.current=obj
def ListdoubleClicked(self,e):
# print(e.data())
# print(e.siblingAtColumn(1).data() )
self.actionplay(e.siblingAtColumn(1).data())
def actionplay(self,f):
self.currentfile=f
self.p.set_mrl(f)
self.p.play()
self.timer.start()
def ShowInfo(self):
if self.p.is_playing() :
self.setWindowTitle(self.currentfile)
self.ui.statusbar.showMessage(str(self.p.get_time()))
else:
self.timer.stop()
# print('kita')
def actionpause(self):
self.p.pause()
def actionstop(self):
self.p.stop()
self.timer.stop()
def actionchange(self):
model2 = QStandardItemModel()
for i in range(3):
parent1 = QStandardItem('Family {}. Some long status text for sp'.format(i))
model2.appendRow(parent1)
for j in range(3):
child1 = QStandardItem('Child {}'.format(i*3+j))
child2 = QStandardItem('row: {}, col: {}'.format(i, j+1))
child3 = QStandardItem('row: {}, col: {}'.format(i, j+2))
parent1.appendRow([child1, child2, child3])
self.ui.treeView.setModel(model2)
def actionreload(self):
pass
def sqlexecute( self, line ):
conn = sqlite3.connect( self.db )
c = conn.cursor()
item=['×','×']
tree=[None,None,None,None]
model = QStandardItemModel()
for l in c.execute(line) :
# print(l)
if item[0] != l[0] : # 1 a-artist
tree[1] = QStandardItem(l[0])
model.appendRow(tree[1])
item[0]=l[0]
if item[1] != l[1] : # 1 date album
tree[2] = QStandardItem( l[1] )
tree[1].appendRow(tree[2])
item[1]=l[1]
tree[3]=QStandardItem(l[2]) , QStandardItem(l[3])
# tree[3].setData(l[3])
tree[2].appendRow(tree[3]) # l[3] path 入れてない
c.close()
conn.close()
return model
def actionbye(self):
# with open( 'data.pickle', 'wb') as f :
# pickle.dump( self.model ,f)
quit()
def actionclear(self):
self.model.clear()
def search(self,e):
item=self.ui.lineEdit.text()
temp=self.ui.treeView.model()
if item=='' or (len(item)==1 and re.match('[a-zA-Z0-9_.,]',item) ) :
# if item=='' :
if temp == self.model :
pass
else:
self.ui.treeView.setModel(self.model)
del temp
else :
item="'%"+item+"%'"
line='select artist ,album,title ,path from musics where \
artist like {} or path like {} or title like {}\
order by artist ,album,title'.format(item,item,item)
self.ui.treeView.setModel(self.sqlexecute(line))
if temp != self.model:
del temp
def treeClicked(self,e):
path=( e.siblingAtColumn(1).data() )
unko=QStandardItem('hoge')
ins=vlc.Instance()
media = ins.media_new(path)
media.parse()
self.model2.clear()
self.model2.appendRow(QStandardItem(media.get_meta(vlc.Meta.Title)))
self.model2.appendRow(QStandardItem(media.get_meta(vlc.Meta.Album)))
self.model2.appendRow(QStandardItem(str(media.get_duration()/1000)))
ins.release()
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
main_window = MainWindow()
main_window.show()
sys.exit(app.exec_())
```
- 一応コード全部のせたけどさすがに見づらいな(¯―¯٥)
# VLC module
- 音出すだけなら簡単なんだけど、しっかり使おうと思ったらそれなりに調べないと難しい
でもサイトにはpyqt用のexampleなんてものも置いてあってかなり親切です
どちらかというと動画用なのかもしれないけど・・・
何とか上のコードまでは出来た
---
今日の予定
```
def search(self,e):
item=self.ui.lineEdit.text()
temp=self.ui.treeView.model()
if item=='' : # <----------------------
if temp == self.model :
pass
else:
self.ui.treeView.setModel(self.model)
del temp
if item=='' or (len(item)==1 and re.match('[a-zA-Z0-9_.,]',item) ) :
```
- 一文字をpassする --> 一行変えるだけかな
p='o'
print (p)
if len(p)==1 and re.match('[a-zA-Z0-9_.,]',p) :
print('no')
else:
print(p)
- 曲の再生
とりあえずvlc 使うか
https://qiita.com/johejo/items/19ee319c62ed63d62c58
**メニューバーも必要になるよなあ**
- タグの取得表示~mutagen? VLCでもイケるかも
vlc get_meta meta でタグ情報を取得できる
libvlc_media_get_meta(p_md, e_meta)
https://www.olivieraubert.net/vlc/python-ctypes/doc/vlc.Meta-class.html
- listview プレイリストもそろそろ作るか
- アルバムアートも考えなくちゃ
タグ埋め込みのアートの取得とか
# tree のモデルを保存しておけば少し起動が速いかもしれない
[pickleでオブジェクトを保存する方法を解説!](https://www.sejuku.net/blog/31480)
```
import pickle
保存
file=r'<<pass>>'
f = open( file, 'wb')
pickle.dump( self.model ,f)
f.close
with open( file, 'wb') as f :
pickle.dump( self.model ,f)
読み込み
with open( file, 'rb') as f :
self.model=pickle.load( f)
```
複数オブジェクトを保存して読み込み
```
import pickle
# write a file
f = open("example", "w")
pickle.dump(["hello", "world"], f)
pickle.dump([2, 3], f)
f.close()
f = open("example", "r")
value1 = pickle.load(f)
value2 = pickle.load(f)
f.close()
```
Player
190531-0602
-
音が出るようになった(・∀・)ノ
-
treeのインクリメンタルは一文字のアルファベットや数字は無視するようにしたので結構快適になってきた
-
右下のリストをきちんと作った
treeからのドラッグでリストに追加されます
これって自分で実装しなくてもリストのドラッグの属性設定だけで出来ちゃうんだなあ、すごいわ (゚Д゚) -
リストのダブルクリックで再生
treeとListって基本同じだ
Qtのこのwidgetは使いづらいと思ってたけど慣れたら簡単でいいのかもしれない -
toolbar を付けた、やっぱり無いと不便(¯―¯٥)
-
pickle を使ってtreeのmodelを保存したかったんだけどエラーで出来なかった
ショック・・・(¯―¯٥) -
タグのビューはvlcで取得した
mutageの変わりにこれを使ったら速さはどうだろう?
import sqlite3 , re ,vlc ,time
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication,QWidget,QMainWindow ,QAction , QListView,QVBoxLayout
from PyQt5.QtGui import QStandardItemModel , QStandardItem
from PyQt5.QtCore import QTimer
import ui
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.ui = ui.Ui_MainWindow()
self.ui.setupUi(self)
self.db=r'e:\Programs\python\_研究\database\database.db'
line='select artist ,album,title ,path from musics order by artist ,album,title'
self.model=self.sqlexecute(line)
self.ui.treeView.setModel(self.model) # tree
self.model2 = QStandardItemModel()
self.ui.listView.setModel(self.model2) # list_tag
self.setaction()
self.makenewtab('default')
self.currentfile=r'(¯―¯٥)'
self.p = vlc.MediaPlayer()
self.timer = QtCore.QTimer(self)
self.timer.setInterval(500)
self.timer.timeout.connect(self.ShowInfo)
def setaction(self):
self.ui.actionBye.triggered.connect(self.actionbye)
self.ui.actionchange.triggered.connect(self.actionchange)
self.ui.actionclear.triggered.connect(self.actionclear)
self.ui.actionreload.triggered.connect(self.actionreload)
self.ui.actionplay.triggered.connect(self.actionplay)
self.ui.actionpause.triggered.connect(self.actionpause)
self.ui.actionstop.triggered.connect(self.actionstop)
actionPlay = QAction('Play', self)
actionPlay.triggered.connect(self.actionplay)
actionPause = QAction('Pause', self)
actionPause.triggered.connect(self.actionpause)
actionStop = QAction('Stop', self)
actionStop.triggered.connect(self.actionstop)
actionBye = QAction('BYE', self)
actionBye.triggered.connect(self.actionbye)
self.ui.toolBar.addAction(actionPlay)
self.ui.toolBar.addAction(actionPause)
self.ui.toolBar.addAction(actionStop)
self.ui.toolBar.addAction(actionBye)
def makenewtab(self,name):
obj = QtWidgets.QListView()
self.ui.tabWidget.addTab(obj, name)
self.ui.tabWidget.setCurrentWidget(obj)
lyt = QVBoxLayout()
lyt.setContentsMargins(0, 0, 0, 0)
lyt.setSpacing(0)
obj.setLayout(lyt)
model = QStandardItemModel()
obj.setModel(model)
obj.setEditTriggers(QtWidgets.QAbstractItemView.NoEditTriggers)
obj.doubleClicked.connect(self.ListdoubleClicked)
obj.setDragEnabled(True)
obj.setDragDropMode(QtWidgets.QAbstractItemView.DragDrop)
self.current=obj
def ListdoubleClicked(self,e):
# print(e.data())
# print(e.siblingAtColumn(1).data() )
self.actionplay(e.siblingAtColumn(1).data())
def actionplay(self,f):
self.currentfile=f
self.p.set_mrl(f)
self.p.play()
self.timer.start()
def ShowInfo(self):
if self.p.is_playing() :
self.setWindowTitle(self.currentfile)
self.ui.statusbar.showMessage(str(self.p.get_time()))
else:
self.timer.stop()
# print('kita')
def actionpause(self):
self.p.pause()
def actionstop(self):
self.p.stop()
self.timer.stop()
def actionchange(self):
model2 = QStandardItemModel()
for i in range(3):
parent1 = QStandardItem('Family {}. Some long status text for sp'.format(i))
model2.appendRow(parent1)
for j in range(3):
child1 = QStandardItem('Child {}'.format(i*3+j))
child2 = QStandardItem('row: {}, col: {}'.format(i, j+1))
child3 = QStandardItem('row: {}, col: {}'.format(i, j+2))
parent1.appendRow([child1, child2, child3])
self.ui.treeView.setModel(model2)
def actionreload(self):
pass
def sqlexecute( self, line ):
conn = sqlite3.connect( self.db )
c = conn.cursor()
item=['×','×']
tree=[None,None,None,None]
model = QStandardItemModel()
for l in c.execute(line) :
# print(l)
if item[0] != l[0] : # 1 a-artist
tree[1] = QStandardItem(l[0])
model.appendRow(tree[1])
item[0]=l[0]
if item[1] != l[1] : # 1 date album
tree[2] = QStandardItem( l[1] )
tree[1].appendRow(tree[2])
item[1]=l[1]
tree[3]=QStandardItem(l[2]) , QStandardItem(l[3])
# tree[3].setData(l[3])
tree[2].appendRow(tree[3]) # l[3] path 入れてない
c.close()
conn.close()
return model
def actionbye(self):
# with open( 'data.pickle', 'wb') as f :
# pickle.dump( self.model ,f)
quit()
def actionclear(self):
self.model.clear()
def search(self,e):
item=self.ui.lineEdit.text()
temp=self.ui.treeView.model()
if item=='' or (len(item)==1 and re.match('[a-zA-Z0-9_.,]',item) ) :
# if item=='' :
if temp == self.model :
pass
else:
self.ui.treeView.setModel(self.model)
del temp
else :
item="'%"+item+"%'"
line='select artist ,album,title ,path from musics where \
artist like {} or path like {} or title like {}\
order by artist ,album,title'.format(item,item,item)
self.ui.treeView.setModel(self.sqlexecute(line))
if temp != self.model:
del temp
def treeClicked(self,e):
path=( e.siblingAtColumn(1).data() )
unko=QStandardItem('hoge')
ins=vlc.Instance()
media = ins.media_new(path)
media.parse()
self.model2.clear()
self.model2.appendRow(QStandardItem(media.get_meta(vlc.Meta.Title)))
self.model2.appendRow(QStandardItem(media.get_meta(vlc.Meta.Album)))
self.model2.appendRow(QStandardItem(str(media.get_duration()/1000)))
ins.release()
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
main_window = MainWindow()
main_window.show()
sys.exit(app.exec_())
- 一応コード全部のせたけどさすがに見づらいな(¯―¯٥)
VLC module
- 音出すだけなら簡単なんだけど、しっかり使おうと思ったらそれなりに調べないと難しい
でもサイトにはpyqt用のexampleなんてものも置いてあってかなり親切です
どちらかというと動画用なのかもしれないけど・・・
何とか上のコードまでは出来た