1 // g++ wx_player.cpp `wx-config --libs` `wx-config --cxxflags` `pkg-config --cflags gtk+-2.0 libvlc` `pkg-config --libs gtk+-2.0 libvlc` -o wx_player
3 /* License WTFPL http://sam.zoy.org/wtfpl/ */
4 /* Written by Vincent Schüßler */
7 #include <wx/filename.h>
14 #include <wx/gtk/win_gtk.h>
15 #define GET_XID(window) GDK_WINDOW_XWINDOW(GTK_PIZZA(window->m_wxwindow)->bin_window)
18 #define myID_PLAYPAUSE wxID_HIGHEST+1
19 #define myID_STOP wxID_HIGHEST+2
20 #define myID_TIMELINE wxID_HIGHEST+3
21 #define myID_VOLUME wxID_HIGHEST+4
23 #define TIMELINE_MAX (INT_MAX-9)
24 #define VOLUME_MAX 100
26 DECLARE_EVENT_TYPE(vlcEVT_END
, -1)
27 DECLARE_EVENT_TYPE(vlcEVT_POS
, -1)
28 DEFINE_EVENT_TYPE(vlcEVT_END
)
29 DEFINE_EVENT_TYPE(vlcEVT_POS
)
31 void OnPositionChanged_VLC(const libvlc_event_t
*event
, void *data
);
32 void OnEndReached_VLC(const libvlc_event_t
*event
, void *data
);
34 class MainWindow
: public wxFrame
{
36 MainWindow(const wxString
& title
);
42 void OnOpen(wxCommandEvent
& event
);
43 void OnPlayPause(wxCommandEvent
& event
);
44 void OnStop(wxCommandEvent
& event
);
45 void OnPositionChanged_USR(wxCommandEvent
& event
);
46 void OnPositionChanged_VLC(wxCommandEvent
& event
);
47 void OnEndReached_VLC(wxCommandEvent
& event
);
48 void OnVolumeChanged(wxCommandEvent
& event
);
49 void OnVolumeClicked(wxMouseEvent
& event
);
50 void OnTimelineClicked(wxMouseEvent
& event
);
55 void setTimeline(float value
);
56 void connectTimeline();
58 wxButton
*playpause_button
;
59 wxButton
*stop_button
;
61 wxSlider
*volume_slider
;
62 wxWindow
*player_widget
;
64 libvlc_media_player_t
*media_player
;
65 libvlc_instance_t
*vlc_inst
;
66 libvlc_event_manager_t
*vlc_evt_man
;
69 MainWindow
*mainWindow
;
71 MainWindow::MainWindow(const wxString
& title
) : wxFrame(NULL
, wxID_ANY
, title
, wxDefaultPosition
) {
75 menubar
= new wxMenuBar
;
77 file
->Append(wxID_OPEN
, wxT("&Open"));
78 menubar
->Append(file
, wxT("&File"));
80 Connect(wxID_OPEN
, wxEVT_COMMAND_MENU_SELECTED
, wxCommandEventHandler(MainWindow::OnOpen
));
83 wxBoxSizer
*vbox
= new wxBoxSizer(wxVERTICAL
);
87 player_widget
= new wxWindow(this, wxID_ANY
);
88 player_widget
->SetBackgroundColour(wxColour(wxT("black")));
89 vbox
->Add(player_widget
, 1, wxEXPAND
| wxALIGN_TOP
);
91 //setup timeline slider
92 timeline
= new wxSlider(this, myID_TIMELINE
, 0, 0, TIMELINE_MAX
);
93 timeline
->Enable(false);
94 vbox
->Add(timeline
, 0, wxEXPAND
);
96 timeline
->Connect(myID_TIMELINE
, wxEVT_LEFT_UP
, wxMouseEventHandler(MainWindow::OnTimelineClicked
));
99 wxPanel
*controlPanel
= new wxPanel(this, wxID_ANY
);
102 wxBoxSizer
*hbox
= new wxBoxSizer(wxHORIZONTAL
);
103 controlPanel
->SetSizer(hbox
);
104 vbox
->Add(controlPanel
, 0, wxEXPAND
);
107 playpause_button
= new wxButton(controlPanel
, myID_PLAYPAUSE
, wxT("Play"));
108 stop_button
= new wxButton(controlPanel
, myID_STOP
, wxT("Stop"));
109 volume_slider
= new wxSlider(controlPanel
, myID_VOLUME
, VOLUME_MAX
, 0, VOLUME_MAX
, wxDefaultPosition
, wxSize(100, -1));
110 playpause_button
->Enable(false);
111 stop_button
->Enable(false);
112 hbox
->Add(playpause_button
);
113 hbox
->Add(stop_button
);
114 hbox
->AddStretchSpacer();
115 hbox
->Add(volume_slider
);
116 Connect(myID_PLAYPAUSE
, wxEVT_COMMAND_BUTTON_CLICKED
, wxCommandEventHandler(MainWindow::OnPlayPause
));
117 Connect(myID_STOP
, wxEVT_COMMAND_BUTTON_CLICKED
, wxCommandEventHandler(MainWindow::OnStop
));
118 Connect(myID_VOLUME
, wxEVT_COMMAND_SLIDER_UPDATED
, wxCommandEventHandler(MainWindow::OnVolumeChanged
));
119 volume_slider
->Connect(myID_VOLUME
, wxEVT_LEFT_UP
, wxMouseEventHandler(MainWindow::OnVolumeClicked
));
122 vlc_inst
= libvlc_new(0, NULL
);
123 media_player
= libvlc_media_player_new(vlc_inst
);
124 vlc_evt_man
= libvlc_media_player_event_manager(media_player
);
125 libvlc_event_attach(vlc_evt_man
, libvlc_MediaPlayerEndReached
, ::OnEndReached_VLC
, NULL
);
126 libvlc_event_attach(vlc_evt_man
, libvlc_MediaPlayerPositionChanged
, ::OnPositionChanged_VLC
, NULL
);
127 Connect(wxID_ANY
, vlcEVT_END
, wxCommandEventHandler(MainWindow::OnEndReached_VLC
));
128 Connect(wxID_ANY
, vlcEVT_POS
, wxCommandEventHandler(MainWindow::OnPositionChanged_VLC
));
134 MainWindow::~MainWindow() {
135 libvlc_media_player_release(media_player
);
136 libvlc_release(vlc_inst
);
139 void MainWindow::initVLC() {
141 libvlc_media_player_set_xwindow(media_player
, GET_XID(this->player_widget
));
143 libvlc_media_player_set_hwnd(media_player
, this->player_widget
->GetHandle());
147 void MainWindow::OnOpen(wxCommandEvent
& event
) {
148 wxFileDialog
openFileDialog(this, wxT("Choose File"));
150 if (openFileDialog
.ShowModal() == wxID_CANCEL
) {
154 libvlc_media_t
*media
;
155 wxFileName filename
= wxFileName::FileName(openFileDialog
.GetPath());
156 filename
.MakeRelativeTo();
157 media
= libvlc_media_new_path(vlc_inst
, filename
.GetFullPath().mb_str());
158 libvlc_media_player_set_media(media_player
, media
);
160 libvlc_media_release(media
);
164 void MainWindow::OnPlayPause(wxCommandEvent
& event
) {
165 if(libvlc_media_player_is_playing(media_player
) == 1) {
173 void MainWindow::OnStop(wxCommandEvent
& event
) {
177 void MainWindow::OnPositionChanged_USR(wxCommandEvent
& event
) {
178 libvlc_media_player_set_position(media_player
, (float) event
.GetInt() / (float) TIMELINE_MAX
);
181 void MainWindow::OnPositionChanged_VLC(wxCommandEvent
& event
) {
182 float factor
= libvlc_media_player_get_position(media_player
);
186 void MainWindow::OnEndReached_VLC(wxCommandEvent
& event
) {
190 void MainWindow::OnVolumeChanged(wxCommandEvent
& event
) {
191 libvlc_audio_set_volume(media_player
, volume_slider
->GetValue());
194 void MainWindow::OnVolumeClicked(wxMouseEvent
& event
) {
195 wxSize size
= mainWindow
->volume_slider
->GetSize();
196 float position
= (float) event
.GetX() / (float) size
.GetWidth();
197 mainWindow
->volume_slider
->SetValue(position
*VOLUME_MAX
);
198 libvlc_audio_set_volume(mainWindow
->media_player
, position
*VOLUME_MAX
);
202 void MainWindow::OnTimelineClicked(wxMouseEvent
& event
) {
203 wxSize size
= mainWindow
->timeline
->GetSize();
204 float position
= (float) event
.GetX() / (float) size
.GetWidth();
205 libvlc_media_player_set_position(mainWindow
->media_player
, position
);
206 mainWindow
->setTimeline(position
);
210 void MainWindow::play() {
211 libvlc_media_player_play(media_player
);
212 playpause_button
->SetLabel(wxT("Pause"));
213 playpause_button
->Enable(true);
214 stop_button
->Enable(true);
215 timeline
->Enable(true);
218 void MainWindow::pause() {
219 libvlc_media_player_pause(media_player
);
220 playpause_button
->SetLabel(wxT("Play"));
223 void MainWindow::stop() {
225 libvlc_media_player_stop(media_player
);
226 stop_button
->Enable(false);
228 timeline
->Enable(false);
231 void MainWindow::setTimeline(float value
) {
232 if(value
< 0.0) value
= 0.0;
233 if(value
> 1.0) value
= 1.0;
234 Disconnect(myID_TIMELINE
);
235 timeline
->SetValue((int) (value
* TIMELINE_MAX
));
239 void MainWindow::connectTimeline() {
240 Connect(myID_TIMELINE
, wxEVT_COMMAND_SLIDER_UPDATED
, wxCommandEventHandler(MainWindow::OnPositionChanged_USR
));
243 class MyApp
: public wxApp
{
245 virtual bool OnInit();
248 void OnPositionChanged_VLC(const libvlc_event_t
*event
, void *data
) {
249 wxCommandEvent
evt(vlcEVT_POS
, wxID_ANY
);
250 mainWindow
->GetEventHandler()->AddPendingEvent(evt
);
253 void OnEndReached_VLC(const libvlc_event_t
*event
, void *data
) {
254 wxCommandEvent
evt(vlcEVT_END
, wxID_ANY
);
255 mainWindow
->GetEventHandler()->AddPendingEvent(evt
);
258 bool MyApp::OnInit() {
259 mainWindow
= new MainWindow(wxT("wxWidgets libVLC demo"));