實驗環境:Windows10,wxWidgets6.0.2,VisualStudio 2013函數
HanApp.hthis
#ifndef HANAPP_H_ #define HANAPP_H_ #include<wx/wxprec.h> #include<wx/wx.h> class HanApp :public wxApp { public: virtual bool OnInit(); }; #endif
#include"HanApp.h" #include"HanFrame.h" #include<wx/mediactrl.h> bool HanApp::OnInit() { HanFrame * f = new HanFrame(0, 1, "x"); f->Show(); return true; } IMPLEMENT_APP(HanApp)HanFrame.h
#ifndef HANFRAME_H_ #define HANFRAME_H_ #include<wx/frame.h> #include<wx/mediactrl.h> class HanFrame :public wxFrame { private: wxMediaCtrl * m_Player; public: HanFrame(wxWindow * parent,wxWindowID id,const wxString & title); private: void OnMediaLoaded(wxMediaEvent &evt); wxDECLARE_EVENT_TABLE(); }; #endifHanFrame.cpp
#include"HanFrame.h" #include<wx/mediactrl.h> #include<wx/wx.h> #include<wx/uri.h> HanFrame::HanFrame(wxWindow * parent, wxWindowID id, const wxString & title) :wxFrame(parent, id, title) { this->m_Player = new wxMediaCtrl(this, (wxWindowID)2); bool re = this->m_Player->Load("C:\\Users\\han\\Music\\林俊杰 - 只對你有感受.mp3"); if (!re) wxLogMessage("載入失敗"); this->m_Player->SetVolume(1.0); this->m_Player->ShowPlayerControls(); //|第二種鏈接 事件-處理函數 的方法 //this->Connect(2,wxEVT_MEDIA_LOADED, (wxObjectEventFunction)(wxEventFunction)(wxMediaEventFunction)&HanFrame::OnMediaLoaded); } void HanFrame::OnMediaLoaded(wxMediaEvent &evt) { wxMessageBox("載入成功!"); bool re2 = this->m_Player->Play(); if (!re2) wxLogMessage("播放失敗"); } wxBEGIN_EVENT_TABLE(HanFrame, wxFrame) EVT_MEDIA_LOADED(2,HanFrame::OnMediaLoaded) EVT_MEDIA_STOP(2,HanFrame::OnMediaLoaded) wxEND_EVENT_TABLE()這裏有一個小問題,EVT_MEDIA_LOADED不能正確響應,待之後解決。