博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
pyglet -- 视频播放器 (简单实现,效果不是太好,切换资源会卡死)(三)
阅读量:4700 次
发布时间:2019-06-09

本文共 6948 字,大约阅读时间需要 23 分钟。

实现一个简单的视频播放器,效果不是很好。这里不多说,直接贴代码了。

1 #-*- coding:gbk -*-  2 import pyglet  3 import os  4 from pyglet.gl import *  5   6 def draw_rec(x,y,width,height):  7     """  8     矩形  9     """ 10     glLoadIdentity() 11     glPushMatrix() 12     glBegin(GL_LINE_LOOP) 13     glVertex2f(x,y) 14     glVertex2f(x+width,y) 15     glVertex2f(x+width,y+height) 16     glVertex2f(x,y+height) 17     glEnd() 18     glPopMatrix() 19  20  21 class Button(pyglet.event.EventDispatcher): 22  23     def __init__(self): 24         super(Button,self).__init__() 25         self.x=y=0  #按钮的位置以及大小 26         self.width=height=10 27         self.hit=False  #按钮是否被鼠标点击 28         self._text=pyglet.text.Label('',anchor_x='center',anchor_y='center')   #初始化是必要的,必须写在__init__中,不然给他赋文本值会当成全局量,最终显示的按钮的文本是最后一个赋值(即,作为私有量) 29      30     def draw(self): 31         """ 32         画按钮 33         """ 34         if self.hit: 35             glColor3f(0.0,1.0,0.0) 36         draw_rec(self.x,self.y,self.width,self.height) 37         glColor3f(1.0,1.0,1.0) 38         self.draw_label() 39     def set_text(self,button_text): 40         """ 41         改变按钮的文本 42         """ 43         self._text.text=button_text 44     button_text=property(lambda self: self._text.text,set_text) 45     def set_size(self,x,y,width,height): 46         """ 47         改变按钮的位置和大小 48         """ 49         self.x=x 50         self.y=y 51         self.width=width 52         self.height=height 53  54     def on_mouse_press(self,x,y,button,modifiers): 55         self.dispatch_event('on_press')   #调度事件 56         self.hit=True  #鼠标点击,颜色变化 57     def on_mouse_drag(self,x,y,dx,dy,button,modifiers): 58         """ 59         拖动 60         """ 61         self.dispatch_event('on_value_change') 62     def on_mouse_release(self,x,y,button,modifiers): 63         self.dispatch_event('on_release') 64         self.hit=False  #释放鼠标,恢复颜色 65     def draw_label(self): 66         """ 67         添加标签 68         """ 69         self._text.x=self.x+self.width/2 70         self._text.y=self.y+self.height/2 71         self._text.draw() 72     def hit_test(self,x,y): 73         return (self.x
1:113 self.width*=self.source.video_format.sample_aspect114 else:115 self.height/=self.source.video_format.sample_aspect116 117 118 119 class MyPlayer(pyglet.window.Window):120 def __init__(self,caption):121 super(MyPlayer,self).__init__(caption=caption,resizable=True)122 self.padding=10 #默认的间距以及长宽123 self.width=50124 self.height=30125 126 #下面列出要显示的列表127 self.drawable=[] #显示列表128 129 #播放器130 self.player=Player()131 self.player.load_source('E:\music')132 self.player.set_locate(0,self.padding*2+30) #播放器显示位置133 self.player.get_and_set_video_size() #得到视频的大小并设置134 self.player.EOS_NEXT='next' #按顺序进行播放135 self.player.push_handlers(self)136 137 #播放/暂停控制按钮138 self.play_pause_control=Button()139 self.play_pause_control.width=50140 self.play_pause_control.height=30141 self.play_pause_control.set_size(self.padding,self.padding,self.play_pause_control.width,self.play_pause_control.height) #位置以及大小142 self.play_pause_control.button_text='play' #文本设置143 self.play_pause_control.on_press=lambda:self.on_play_pause()144 self.drawable.append(self.play_pause_control) #将这个按钮加入到显示列表中145 146 #全屏控制按钮147 self.isscreenfull=Button()148 self.isscreenfull.width=50149 self.isscreenfull.height=30150 self.isscreenfull.set_size(self.padding*2+self.play_pause_control.width,self.padding,self.isscreenfull.width,self.isscreenfull.height)151 self.isscreenfull.button_text='全屏'152 self.isscreenfull.on_press=lambda: self.set_fullscreen(True)153 self.drawable.append(self.isscreenfull)154 155 def on_draw(self):156 self.clear()157 #显示播放器158 if self.player.source and self.player.source.video_format: 159 self.player.get_texture().blit((self.width-self.player.width)/2,self.player.y,width=self.player.width,height=self.player.height) #注意这里width=,height=不能省略,否则画面不会出现的160 161 162 #画列表中所有的控制按钮163 if self.drawable:164 for draw_c in self.drawable:165 draw_c.draw()166 def on_mouse_press(self,x,y,button,modifiers):167 for dc in self.drawable:168 if dc.hit_test(x,y):169 dc.on_mouse_press(x,y,button,modifiers)170 def on_play_pause(self):171 if self.player.playing:172 self.player.pause()173 self.play_pause_control.set_text('pause') 174 else:175 if self.player.time > self.player.source.duration:176 self.player.seek(0)177 self.player.play()178 self.play_pause_control.set_text('play')179 def on_mouse_release(self,x,y,button,modifiers):180 for dc in self.drawable:181 if dc.hit_test(x,y):182 dc.on_mouse_release(x,y,button,modifiers)183 def on_resize(self,width,height):184 super(MyPlayer,self).on_resize(width,height)185 if self.player.source:186 video_width,video_height=self.player.width,self.player.height187 188 display_aspect=width/float(height)189 video_aspect=video_width/float(video_height)190 191 if video_aspect>display_aspect:192 self.player.width=width193 self.player.height=width/video_aspect194 else:195 self.player.height=height196 self.player.width=height * video_aspect197 self.player.x=(width-self.player.width)/2198 self.player.y=(height-self.player.height)/2199 200 if __name__ == "__main__":201 wn=MyPlayer('my player')202 wn.set_size(int(wn.player.width),int(wn.player.height)) #将窗口的大小设置的和视频一样大小203 wn.set_visible(True) #可见204 wn.player.play()205 pyglet.app.run()206

 

转载于:https://www.cnblogs.com/yinwei-space/p/4608327.html

你可能感兴趣的文章
Pycharm配置autopep8让Python代码更符合pep8规范
查看>>
我的第一篇博客
查看>>
【C++算法与数据结构学习笔记------单链表实现多项式】
查看>>
C#垃圾回收机制
查看>>
31、任务三十一——表单联动
查看>>
python之hasattr、getattr和setattr函数
查看>>
maven使用阿里镜像配置文件
查看>>
Copy code from eclipse to word, save syntax.
查看>>
arguments.callee的作用及替换方案
查看>>
P2709 小B的询问
查看>>
PHP echo 和 print 语句
查看>>
第一讲 一个简单的Qt程序分析
查看>>
Centos 6.5下的OPENJDK卸载和SUN的JDK安装、环境变量配置
查看>>
poj 1979 Red and Black(dfs)
查看>>
【.Net基础03】HttpWebRequest模拟浏览器登陆
查看>>
zTree async 动态参数处理
查看>>
Oracle学习之常见错误整理
查看>>
数据库插入数据乱码问题
查看>>
altium annotate 选项设置 complete existing packages
查看>>
【模式识别与机器学习】——SVM举例
查看>>