steel_dragon
05-10-2010, 00:01
Em có file code của 1 chương trình mp3 player viết bằng C++ sử dụng thư viện BASS và wxWidgets
Mô tả: chương trình chỉ đọc file MP3 và có các nút cơ bản (play, stop, ...)
Tuy nhiên, em ko làm sao mà chạy được , bác nào có thời gian rỗi ngâm cứu cho nó chạy , em xin hậu tạ (1 chầu cafe + thù lao tương xứng)
main.h:
#ifndef __MAIN_H__
#define __MAIN_H__
#include <wx/wx.h>
#include "simple.h"
#include "bass.h"
class MyApp : public wxApp
{
public:
virtual bool OnInit();
virtual int OnExit();
};
#endif // __MAIN_H__
main.cpp
#include "main.h"
IMPLEMENT_APP(MyApp)
bool MyApp::OnInit()
{
if ( HIWORD( BASS_GetVersion() ) != BASSVERSION )
{
wxMessageBox( wxT("Invalid BASS.DLL Version"), wxT("BASS.DLL Error") );
return false;
}
if ( !BASS_Init( -1, 44100, 0,0,NULL) )
{
wxMessageBox( wxT("BASS Init Error"), wxT("BASS.DLL Error") );
return false;
}
Simple *simple = new Simple(wxT("Simple"));
simple->Show(true);
return true;
}
int MyApp::OnExit()
{
BASS_Free();
return true;
}
simple.h
#ifndef __SIMPLE_H__
#define __SIMPLE_H__
#include <wx/wx.h>
#include <wx/filedlg.h>
#include <wx/filename.h>
#include "bass.h"
//Pre Define Class
class Simple;
class SimpleTimer;
enum
{
wxIDSimple_Open = 1000,
wxIDSimple_Play,
wxIDSimple_Stop,
wxIDSimple_TextPanel
};
class Simple : public wxFrame
{
public:
Simple(const wxString& title);
void OnOpen(wxCommandEvent & event);
void OnPlay(wxCommandEvent & event);
void OnStop(wxCommandEvent & event);
//Buttons Poinsters
wxButton *m_OpenButton;
wxButton *m_PlayButton;
wxButton *m_StopButton;
//Text Display
wxStaticText *m_text;
wxString m_FileName;
// Timer Task
SimpleTimer *m_timer;
//Bass
HSTREAM channel;
};
class SimpleTimer : public wxTimer
{
public:
SimpleTimer( Simple *frame )
{
m_frame = frame;
}
void Notify();
Simple *m_frame;
};
#endif // __SIMPLE_H__
simple.cpp
#include "simple.h"
Simple::Simple(const wxString& title)
: wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(242, 100), wxMINIMIZE_BOX | wxSYSTEM_MENU | wxCAPTION | wxCLOSE_BOX | wxCLIP_CHILDREN)
{
//SetIcon(wxICON(iconresource)); // Enable this if you want to build with resources
//Set Channel to 0
channel = 0;
wxPanel *panel = new wxPanel(this, wxID_ANY);
// Buttons
m_OpenButton = new wxButton(panel, wxIDSimple_Open, wxT("Open"), wxPoint(5, 5));
Connect(wxIDSimple_Open, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(Simple::OnOpen));
m_OpenButton->SetFocus();
m_PlayButton = new wxButton(panel, wxIDSimple_Play, wxT("Play"), wxPoint(80, 5));
Connect(wxIDSimple_Play, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(Simple::OnPlay));
m_PlayButton->SetFocus();
m_PlayButton->Disable();
m_StopButton = new wxButton(panel, wxIDSimple_Stop, wxT("Stop"), wxPoint(155, 5));
Connect(wxIDSimple_Stop, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(Simple::OnStop));
m_StopButton->SetFocus();
m_StopButton->Disable();
//Text
m_text = new wxStaticText(panel, wxIDSimple_TextPanel, wxT(""), wxPoint(5, 30), wxSize( 228, 50 ), wxALIGN_CENTRE );
m_text->SetFocus();
//Timer Task
m_timer = new SimpleTimer(this);
m_timer->Start( 500 );
Centre();
}
void Simple::OnOpen(wxCommandEvent & WXUNUSED(event))
{
wxFileDialog fd(this, wxFileSelectorPromptStr, wxEmptyString, wxEmptyString, wxT("streamable files|*.mp3;*.mp2;*.mp1;*.ogg;*.wav;*.aif") );
if(fd.ShowModal() == wxID_OK)
{
BASS_StreamFree( channel );
channel = BASS_StreamCreateFile(FALSE, fd.GetPath(), 0, 0, BASS_SAMPLE_LOOP );
if( channel != NULL )
{
m_StopButton->Enable();
m_PlayButton->Enable();
m_PlayButton->SetLabel( wxT("Pause") );
BASS_ChannelPlay( channel, true );
m_FileName = wxFileName(fd.GetPath()).GetName();
}
else
{
m_StopButton->Disable();
m_PlayButton->Disable();
m_PlayButton->SetLabel( wxT("Play") );
}
}
}
void Simple::OnPlay(wxCommandEvent & WXUNUSED(event))
{
if( BASS_ChannelIsActive( channel ) != BASS_ACTIVE_PLAYING )
{
BASS_ChannelPlay( channel, BASS_ChannelIsActive( channel ) == BASS_ACTIVE_PAUSED ? false : true );
m_PlayButton->SetLabel( wxT("Pause") );
}
else
{
BASS_ChannelPause( channel );
m_PlayButton->SetLabel( wxT("Play") );
}
m_StopButton->Enable();
}
void Simple::OnStop(wxCommandEvent & WXUNUSED(event))
{
BASS_ChannelStop( channel );
m_PlayButton->SetLabel( wxT("Play") );
m_StopButton->Disable();
}
void SimpleTimer::Notify()
{
if( m_frame->channel == 0 )
{
m_frame->m_text->SetLabel( wxT("") );
return;
}
// Yes this can do it only on file open!! But is a simple example. (totaltime)
double postime = BASS_ChannelBytes2Seconds(m_frame->channel, BASS_ChannelGetPosition(m_frame->channel, BASS_POS_BYTE));
double totaltime = BASS_ChannelBytes2Seconds(m_frame->channel, BASS_ChannelGetLength(m_frame->channel, BASS_POS_BYTE));
m_frame->m_text->SetLabel( wxString::Format( wxT("%02i:%02i/%02i:%02i\n%s"), (long)(postime / 60), (long)((long)postime % 60), (long)(totaltime / 60), (long)((long)totaltime % 60), m_frame->m_FileName ) );
m_frame->m_text->SetSize( 228, 50 ); // Re format text in lines.
}
Mô tả: chương trình chỉ đọc file MP3 và có các nút cơ bản (play, stop, ...)
Tuy nhiên, em ko làm sao mà chạy được , bác nào có thời gian rỗi ngâm cứu cho nó chạy , em xin hậu tạ (1 chầu cafe + thù lao tương xứng)
main.h:
#ifndef __MAIN_H__
#define __MAIN_H__
#include <wx/wx.h>
#include "simple.h"
#include "bass.h"
class MyApp : public wxApp
{
public:
virtual bool OnInit();
virtual int OnExit();
};
#endif // __MAIN_H__
main.cpp
#include "main.h"
IMPLEMENT_APP(MyApp)
bool MyApp::OnInit()
{
if ( HIWORD( BASS_GetVersion() ) != BASSVERSION )
{
wxMessageBox( wxT("Invalid BASS.DLL Version"), wxT("BASS.DLL Error") );
return false;
}
if ( !BASS_Init( -1, 44100, 0,0,NULL) )
{
wxMessageBox( wxT("BASS Init Error"), wxT("BASS.DLL Error") );
return false;
}
Simple *simple = new Simple(wxT("Simple"));
simple->Show(true);
return true;
}
int MyApp::OnExit()
{
BASS_Free();
return true;
}
simple.h
#ifndef __SIMPLE_H__
#define __SIMPLE_H__
#include <wx/wx.h>
#include <wx/filedlg.h>
#include <wx/filename.h>
#include "bass.h"
//Pre Define Class
class Simple;
class SimpleTimer;
enum
{
wxIDSimple_Open = 1000,
wxIDSimple_Play,
wxIDSimple_Stop,
wxIDSimple_TextPanel
};
class Simple : public wxFrame
{
public:
Simple(const wxString& title);
void OnOpen(wxCommandEvent & event);
void OnPlay(wxCommandEvent & event);
void OnStop(wxCommandEvent & event);
//Buttons Poinsters
wxButton *m_OpenButton;
wxButton *m_PlayButton;
wxButton *m_StopButton;
//Text Display
wxStaticText *m_text;
wxString m_FileName;
// Timer Task
SimpleTimer *m_timer;
//Bass
HSTREAM channel;
};
class SimpleTimer : public wxTimer
{
public:
SimpleTimer( Simple *frame )
{
m_frame = frame;
}
void Notify();
Simple *m_frame;
};
#endif // __SIMPLE_H__
simple.cpp
#include "simple.h"
Simple::Simple(const wxString& title)
: wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(242, 100), wxMINIMIZE_BOX | wxSYSTEM_MENU | wxCAPTION | wxCLOSE_BOX | wxCLIP_CHILDREN)
{
//SetIcon(wxICON(iconresource)); // Enable this if you want to build with resources
//Set Channel to 0
channel = 0;
wxPanel *panel = new wxPanel(this, wxID_ANY);
// Buttons
m_OpenButton = new wxButton(panel, wxIDSimple_Open, wxT("Open"), wxPoint(5, 5));
Connect(wxIDSimple_Open, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(Simple::OnOpen));
m_OpenButton->SetFocus();
m_PlayButton = new wxButton(panel, wxIDSimple_Play, wxT("Play"), wxPoint(80, 5));
Connect(wxIDSimple_Play, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(Simple::OnPlay));
m_PlayButton->SetFocus();
m_PlayButton->Disable();
m_StopButton = new wxButton(panel, wxIDSimple_Stop, wxT("Stop"), wxPoint(155, 5));
Connect(wxIDSimple_Stop, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(Simple::OnStop));
m_StopButton->SetFocus();
m_StopButton->Disable();
//Text
m_text = new wxStaticText(panel, wxIDSimple_TextPanel, wxT(""), wxPoint(5, 30), wxSize( 228, 50 ), wxALIGN_CENTRE );
m_text->SetFocus();
//Timer Task
m_timer = new SimpleTimer(this);
m_timer->Start( 500 );
Centre();
}
void Simple::OnOpen(wxCommandEvent & WXUNUSED(event))
{
wxFileDialog fd(this, wxFileSelectorPromptStr, wxEmptyString, wxEmptyString, wxT("streamable files|*.mp3;*.mp2;*.mp1;*.ogg;*.wav;*.aif") );
if(fd.ShowModal() == wxID_OK)
{
BASS_StreamFree( channel );
channel = BASS_StreamCreateFile(FALSE, fd.GetPath(), 0, 0, BASS_SAMPLE_LOOP );
if( channel != NULL )
{
m_StopButton->Enable();
m_PlayButton->Enable();
m_PlayButton->SetLabel( wxT("Pause") );
BASS_ChannelPlay( channel, true );
m_FileName = wxFileName(fd.GetPath()).GetName();
}
else
{
m_StopButton->Disable();
m_PlayButton->Disable();
m_PlayButton->SetLabel( wxT("Play") );
}
}
}
void Simple::OnPlay(wxCommandEvent & WXUNUSED(event))
{
if( BASS_ChannelIsActive( channel ) != BASS_ACTIVE_PLAYING )
{
BASS_ChannelPlay( channel, BASS_ChannelIsActive( channel ) == BASS_ACTIVE_PAUSED ? false : true );
m_PlayButton->SetLabel( wxT("Pause") );
}
else
{
BASS_ChannelPause( channel );
m_PlayButton->SetLabel( wxT("Play") );
}
m_StopButton->Enable();
}
void Simple::OnStop(wxCommandEvent & WXUNUSED(event))
{
BASS_ChannelStop( channel );
m_PlayButton->SetLabel( wxT("Play") );
m_StopButton->Disable();
}
void SimpleTimer::Notify()
{
if( m_frame->channel == 0 )
{
m_frame->m_text->SetLabel( wxT("") );
return;
}
// Yes this can do it only on file open!! But is a simple example. (totaltime)
double postime = BASS_ChannelBytes2Seconds(m_frame->channel, BASS_ChannelGetPosition(m_frame->channel, BASS_POS_BYTE));
double totaltime = BASS_ChannelBytes2Seconds(m_frame->channel, BASS_ChannelGetLength(m_frame->channel, BASS_POS_BYTE));
m_frame->m_text->SetLabel( wxString::Format( wxT("%02i:%02i/%02i:%02i\n%s"), (long)(postime / 60), (long)((long)postime % 60), (long)(totaltime / 60), (long)((long)totaltime % 60), m_frame->m_FileName ) );
m_frame->m_text->SetSize( 228, 50 ); // Re format text in lines.
}