12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- import os
- import vlc
- from app import log
- class Player:
- '''
- args:设置 options
- '''
- def __init__(self, *args):
- self.instance = vlc.Instance(*args)
- self.media = self.instance.media_player_new()
- # 设置待播放的url地址或本地文件路径,每次调用都会重新加载资源
- def set_uri(self, uri):
- self.media.set_mrl(uri)
- # 播放 成功返回0,失败返回-1
- def play(self, path=None):
- if path:
- self.set_uri(path)
- return self.media.play()
- else:
- return self.media.play()
- # 暂停
- def pause(self):
- self.media.pause()
- # 恢复
- def resume(self):
- self.media.set_pause(0)
- # 停止
- def stop(self):
- self.media.stop()
- # 释放资源
- def release(self):
- return self.media.release()
- # 是否正在播放
- def is_playing(self):
- return self.media.is_playing()
- # 已播放时间,返回毫秒值
- def get_time(self):
- return self.media.get_time()
- # 拖动指定的毫秒值处播放。成功返回0,失败返回-1 (需要注意,只有当前多媒体格式或流媒体协议支持才会生效)
- def set_time(self, ms):
- return self.media.get_time()
- # 音视频总长度,返回毫秒值
- def get_length(self):
- return self.media.get_length()
- # 获取当前音量(0~100)
- def get_volume(self):
- return self.media.audio_get_volume()
- # 设置音量(0~100)
- def set_volume(self, volume):
- return self.media.audio_set_volume(volume)
- # 返回当前状态:正在播放;暂停中;其他
- def get_state(self):
- state = self.media.get_state()
- if state == vlc.State.Playing:
- return 1
- elif state == vlc.State.Paused:
- return 0
- else:
- return -1
- # 当前播放进度情况。返回0.0~1.0之间的浮点数
- def get_position(self):
- return self.media.get_position()
- # 拖动当前进度,传入0.0~1.0之间的浮点数(需要注意,只有当前多媒体格式或流媒体协议支持才会生效)
- def set_position(self, float_val):
- return self.media.set_position(float_val)
- # 获取当前文件播放速率
- def get_rate(self):
- return self.media.get_rate()
- # 设置播放速率(如:1.2,表示加速1.2倍播放)
- def set_rate(self, rate):
- return self.media.set_rate(rate)
- # 设置宽高比率(如"16:9","4:3")
- def set_ratio(self, ratio):
- self.media.video_set_scale(0) # 必须设置为0,否则无法修改屏幕宽高
- self.media.video_set_aspect_ratio(ratio)
|