Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add playmode #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ NetEase-MusicBox
<tr> <td>[</td> <td>Prev song</td> <td>上一曲</td> </tr>
<tr> <td>]</td> <td>Next song</td> <td>下一曲</td> </tr>
<tr> <td>Space</td> <td>Play/Pause</td> <td>播放/暂停</td> </tr>
<tr> <td>,</td> <td>Play in list mode</td> <td>列表循环(默认)</td> </tr>
<tr> <td>.</td> <td>Play in single mode</td> <td>单曲循环</td> </tr>
<tr> <td>/</td> <td>Play in random mode</td> <td>随机播放</td> </tr>
<tr> <td>M</td> <td>Menu</td> <td>主菜单</td> </tr>
<tr> <td>P</td> <td>Present</td> <td>当前播放列表</td> </tr>
<tr> <td>A</td> <td>Add</td> <td>添加曲目到打碟</td> </tr>
Expand Down
51 changes: 33 additions & 18 deletions src/menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,27 @@
carousel = lambda left, right, x: left if (x>right) else (right if x<left else x)

shortcut = [
['j', 'Down ', '下移'],
['k', 'Up ', '上移'],
['h', 'Back ', '后退'],
['l', 'Forward ', '前进'],
['u', 'Prev page ', '上一页'],
['d', 'Next page ', '下一页'],
['f', 'Search ', '快速搜索'],
['[', 'Prev song ', '上一曲'],
[']', 'Next song ', '下一曲'],
[' ', 'Play/Pause', '播放/暂停'],
['m', 'Menu ', '主菜单'],
['p', 'Present ', '当前播放列表'],
['a', 'Add ', '添加曲目到打碟'],
['z', 'DJ list ', '打碟列表'],
['s', 'Star ', '添加到收藏'],
['c', 'Collection', '收藏列表'],
['r', 'Remove ', '删除当前条目'],
['q', 'Quit ', '退出']
['j', 'Down ', '下移'],
['k', 'Up ', '上移'],
['h', 'Back ', '后退'],
['l', 'Forward ', '前进'],
['u', 'Prev page ', '上一页'],
['d', 'Next page ', '下一页'],
['f', 'Search ', '快速搜索'],
['[', 'Prev song ', '上一曲'],
[']', 'Next song ', '下一曲'],
[' ', 'Play/Pause ', '播放/暂停'],
[',', 'list mode ', '列表循环'],
['.', 'single mode', '单曲循环'],
['/', 'random mode', '随机播放'],
['m', 'Menu ', '主菜单'],
['p', 'Present ', '当前播放列表'],
['a', 'Add ', '添加曲目到打碟'],
['z', 'DJ list ', '打碟列表'],
['s', 'Star ', '添加到收藏'],
['c', 'Collectio n', '收藏列表'],
['r', 'Remove ', '删除当前条目'],
['q', 'Quit ', '退出']
]


Expand Down Expand Up @@ -158,6 +161,18 @@ def start(self):
self.player.prev()
time.sleep(0.1)

# 列表循环
elif key == ord(','):
self.player.playmode = 'list'

# 单曲循环
elif key == ord('.'):
self.player.playmode = 'single'

# 随机播放
elif key == ord('/'):
self.player.playmode = 'random'

# 播放、暂停
elif key == ord(' '):
if datatype == 'songs':
Expand Down
17 changes: 14 additions & 3 deletions src/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import subprocess
import threading
import random
import time
import os
import signal
Expand All @@ -27,6 +28,7 @@ def __init__(self):
# flag stop, prevent thread start
self.playing_flag = False
self.pause_flag = False
self.playmode = 'list'
self.songs = []
self.idx = 0

Expand All @@ -41,7 +43,7 @@ def runInThread(onExit, popenArgs):
self.popen_handler = subprocess.Popen(['mpg123', popenArgs], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
self.popen_handler.wait()
if self.playing_flag:
self.idx = carousel(0, len(self.songs)-1, self.idx+1 )
self.pick_song()
onExit()
return
thread = threading.Thread(target=runInThread, args=(onExit, popenArgs))
Expand Down Expand Up @@ -115,11 +117,20 @@ def resume(self):
def next(self):
self.stop()
time.sleep(0.01)
self.idx = carousel(0, len(self.songs)-1, self.idx+1 )
self.pick_song()
self.recall()

def prev(self):
self.stop()
time.sleep(0.01)
self.idx = carousel(0, len(self.songs)-1, self.idx-1 )
self.pick_song(next=False)
self.recall()

def pick_song(self, next=True):
if self.playmode == 'list':
self.idx = carousel(0, len(self.songs)-1, (self.idx+1) if next else (self.idx-1))
elif self.playmode == 'single':
pass
elif self.playmode == 'random':
self.idx = random.randint(0, len(self.songs)-1)