2022-08-03 20:37:37 +08:00
|
|
|
import time
|
|
|
|
from ffpyplayer.player import MediaPlayer
|
|
|
|
from ffpyplayer.tools import set_log_callback, get_log_callback, formats_in
|
|
|
|
|
|
|
|
class AudioPlayer:
|
2022-08-04 15:50:49 +08:00
|
|
|
def __init__(self, source=None, autoplay=False,
|
|
|
|
loop=False,
|
|
|
|
on_stop=None):
|
2022-08-03 20:37:37 +08:00
|
|
|
self.volume = 1
|
|
|
|
self.state = None
|
|
|
|
self.source = None
|
|
|
|
self.quitted = False
|
|
|
|
self.loop = loop
|
|
|
|
self.autoplay = autoplay
|
|
|
|
self.player = None
|
2022-08-04 15:50:49 +08:00
|
|
|
self.on_stop = on_stop
|
2022-08-03 20:37:37 +08:00
|
|
|
self.cmds = []
|
|
|
|
if source:
|
|
|
|
self.set_source(source)
|
|
|
|
|
|
|
|
def set_source(self, source):
|
|
|
|
self.source = source
|
|
|
|
self.load()
|
|
|
|
|
|
|
|
def player_callback(self, selector, value):
|
2022-08-17 10:40:51 +08:00
|
|
|
print(f'player_callback(): {selector}, {value}')
|
2022-08-03 20:37:37 +08:00
|
|
|
if self.player is None:
|
2022-08-17 10:40:51 +08:00
|
|
|
print(f'player_callback():self.player is None, {selector}, {value}')
|
2022-08-03 20:37:37 +08:00
|
|
|
return
|
|
|
|
if selector == 'quit':
|
|
|
|
def close(*args):
|
|
|
|
self.quitted = True
|
|
|
|
self.unload()
|
2022-08-04 15:50:49 +08:00
|
|
|
close()
|
2022-08-03 20:37:37 +08:00
|
|
|
|
|
|
|
elif selector == 'eof':
|
2022-08-04 15:50:49 +08:00
|
|
|
self._do_eos()
|
2022-08-03 20:37:37 +08:00
|
|
|
|
|
|
|
def load(self):
|
|
|
|
if self.source is None:
|
|
|
|
return
|
|
|
|
source = self.source
|
|
|
|
self.unload()
|
|
|
|
ff_opts = {'vn':True, 'sn':True}
|
|
|
|
self.player = MediaPlayer(source,
|
|
|
|
callback=self.player_callback,
|
|
|
|
loglevel='info',
|
|
|
|
ff_opts=ff_opts)
|
|
|
|
player = self.player
|
|
|
|
self.state = 'pause'
|
|
|
|
s = time.perf_counter()
|
|
|
|
while (player.get_metadata()['duration'] is None and
|
|
|
|
not self.quitted and
|
|
|
|
time.perf_counter() - s < 10.):
|
|
|
|
time.sleep(0.005)
|
2022-08-17 10:40:51 +08:00
|
|
|
player.toggle_pause()
|
|
|
|
time.sleep(0.04)
|
|
|
|
player.set_volume(self.volume)
|
2022-08-03 20:37:37 +08:00
|
|
|
if self.autoplay:
|
|
|
|
self.play()
|
|
|
|
|
|
|
|
def unload(self):
|
|
|
|
self.player = None
|
|
|
|
self.state = 'stop'
|
|
|
|
self.quitted = False
|
|
|
|
|
|
|
|
def __del__(self):
|
|
|
|
self.unload()
|
|
|
|
|
|
|
|
def play(self):
|
|
|
|
if self.player is None:
|
|
|
|
self.load()
|
|
|
|
if self.player is None:
|
2022-08-17 10:40:51 +08:00
|
|
|
print('play():self.player is None')
|
2022-08-03 20:37:37 +08:00
|
|
|
return
|
|
|
|
if self.state == 'play':
|
|
|
|
return
|
|
|
|
self.player.toggle_pause()
|
|
|
|
self.state = 'play'
|
|
|
|
|
|
|
|
def pause(self):
|
|
|
|
if self.player is None:
|
|
|
|
self.load()
|
|
|
|
if self.player is None:
|
2022-08-17 10:40:51 +08:00
|
|
|
print('pause():self.player is None')
|
2022-08-03 20:37:37 +08:00
|
|
|
return
|
|
|
|
if self.state == 'pause':
|
|
|
|
return
|
|
|
|
self.player.toggle_pause()
|
|
|
|
self.state = 'pause'
|
|
|
|
|
2022-08-04 15:50:49 +08:00
|
|
|
def is_busy(self):
|
2022-08-03 20:37:37 +08:00
|
|
|
if self.player and self.state == 'play':
|
2022-08-04 15:50:49 +08:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
if self.player is None:
|
|
|
|
return
|
|
|
|
if self.state == 'play':
|
2022-08-03 20:37:37 +08:00
|
|
|
self.player.toggle_pause()
|
2022-08-04 15:50:49 +08:00
|
|
|
self.state = 'stop'
|
|
|
|
self.seek(0)
|
|
|
|
if self.on_stop:
|
|
|
|
self.on_stop()
|
2022-08-03 20:37:37 +08:00
|
|
|
|
|
|
|
def seek(self, pos):
|
|
|
|
if self.player is None:
|
2022-08-17 10:40:51 +08:00
|
|
|
print('seek():self.player is None')
|
2022-08-03 20:37:37 +08:00
|
|
|
return
|
|
|
|
self.player.seek(pos, relative=False)
|
|
|
|
|
|
|
|
def get_pos(self):
|
|
|
|
if self.player is None:
|
|
|
|
return 0
|
|
|
|
return self.player.get_pts()
|
|
|
|
|
|
|
|
def _do_eos(self, *args):
|
|
|
|
print('_do_eos() called ...')
|
|
|
|
if self.loop:
|
|
|
|
self.seek(0.)
|
|
|
|
else:
|
2022-08-04 15:50:49 +08:00
|
|
|
print('go stop')
|
2022-08-03 20:37:37 +08:00
|
|
|
self.stop()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
import sys
|
|
|
|
p = AudioPlayer(autoplay=True, loop=True)
|
|
|
|
p.source = sys.argv[1]
|
|
|
|
p.load()
|
|
|
|
p.play()
|
|
|
|
while True:
|
|
|
|
while p._pump():
|
|
|
|
pass
|
|
|
|
|
|
|
|
print("""
|
|
|
|
play: play it,
|
|
|
|
stop: stop play
|
|
|
|
pause:pause it
|
|
|
|
quit: exit
|
|
|
|
""")
|
|
|
|
x = input()
|
|
|
|
if x == 'quit':
|
|
|
|
p.quitted = True
|
|
|
|
p.stop()
|
|
|
|
break
|
|
|
|
if x == 'play':
|
|
|
|
p.play()
|
|
|
|
continue
|
|
|
|
if x == 'stop':
|
|
|
|
p.stop()
|
|
|
|
continue
|
|
|
|
if x == 'pause':
|
|
|
|
p.pause()
|
|
|
|
continue
|
|
|
|
|