Hi,
I’m Germano Carella, from Italy.
I’m a blind developer and I’m working on python since 2009.
I use wxpython, 'cause wx controls are accessible on windows for screen reading software.
Now, I’m having problems with wx.media.MediaCtrl.
I’m writing an application that searchses and plays video from youtube.
I post my code, is very simple.
I deleted my api key for security reasons.
from googleapiclient.discovery import build
from pytube import YouTube
import wx
import wx.media
class MyFrame(wx.Frame):
def init(self):
super().init(parent=None, title=‘YouTube Music Player’)
self.panel = wx.Panel(self)
self.text_ctrl = wx.TextCtrl(self.panel)
self.search_button = wx.Button(self.panel)
self.playlist = wx.ListCtrl(self.panel,style=wx.LC_REPORT)
self.playlist.InsertColumn(0, ‘Title’, width=200)
self.playlist.InsertColumn(1, ‘Channel’, width=75)
self.player=wx.media.MediaCtrl(self.panel)
self.play_button = wx.Button(self.panel)
self.mainsizer = wx.BoxSizer(wx.VERTICAL)
self.searchbox = wx.BoxSizer(wx.HORIZONTAL)
self.searchbox.Add(self.text_ctrl)
self.searchbox.Add(self.search_button)
self.mainsizer.Add(self.searchbox)
self.mainsizer.Add(self.playlist)
self.mainsizer.Add(self.player)
self.panel.SetSizer(self.mainsizer)
self.videos=dict()
self.Bind(wx.EVT_BUTTON, self.on_search_button_click, self.search_button)
self.Bind(wx.EVT_BUTTON,self.on_play_button_click,self.play_button)
self.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self.on_play_button_click, self.playlist)
self.Bind(wx.media.EVT_MEDIA_LOADED,self.onload,self.player)
self.Show()
def on_search_button_click(self, event):
api_key = 'MyApiKey'
youtube = build('youtube', 'v3', developerKey=api_key)
request = youtube.search().list(
part='id,snippet',
q=self.text_ctrl.GetValue(),
type='video',
videoDefinition='high',
maxResults=25
)
response = request.execute()
self.playlist.DeleteAllItems()
for item in response['items']:
title = item['snippet']['title']
channel = item['snippet']['channelTitle']
video_id = item['id']['videoId']
self.playlist.Append((title, channel))
id = wx.NewIdRef()
self.videos[id]=video_id
self.playlist.SetItemData(self.playlist.GetItemCount()-1, id)
def on_play_button_click(self, event):
index = event.GetIndex()
id = self.playlist.GetItemData(index)
video_id=self.videos[id]
yt = YouTube('https://www.youtube.com/watch?v=' + video_id)
best_audio_stream = yt.streams.first()
audio_url = best_audio_stream.url
self.play_audio(audio_url)
def on_play_button_click(self,event):
self.player.Play()
def play_audio(self, audio_url):
if (self.player.Load(audio_url)):
print("media loading")
else:
print("Media not found!")
def onload(self,event):
self.player.Play()
print("playing")
if name == ‘main’:
app = wx.App()
frame = MyFrame()
frame.Show()
app.MainLoop()
I simply want to play video automatically, when I select it in ListView.
MediaCtrl controls are not accessible to screen reader, so, I can’t be sure video is loaded.
Windows seems to not fire MEDIA_LOADED event, and Play() call does not work in any way.
What I wrong?
Thanks,
Germano