МРЗ Player

Програма МРЗ Player дає змогу прослухати музиальні файли формату. Слід звернути увагу, що на формі немає компонента MediaPlayer1, він створюється на початку роботи програми (робить це конструктор форми). Вибір папки (каталогу), у якому містяться файли формату МРЗ, здійснюється в стандартному діалоговому вікні Вибір папки, яке стає доступним у результаті клацання на кнопці Eject (speedButton1).

Код написання програми

#include vcl.h
#include MPlayer.hpp
#include "FileCtrl.hpp"
#include "mmsystem.hpp"
#pragma hdrstop
#include "Unit1.h"
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
TMediaPlayer *MediaPlayer1;
AnsiString SoundPath;
int min, sec, msec;
int mode = 0;
union TVolume {
unsigned long Volume;
struct {
Word Left;
Word Right;
};
} volume;
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
MediaPlayer1 = new TMediaPlayer (Form1->Handle);
}
void __fastcall TForm1::PlayList (AnsiString path)
{
TSearchRec SearchRec;
ListBox1->Clear();
if (FindFirst(path + "*.mp3", faAnyFile, SearchRec) != 0) {
SpeedButton2->Enabled = false;
SpeedButton4-> Enabled = false;
Label1->Caption = "";
return;
}
ListBox1->Items->Add(SearchRec.Name);
while (FindNext (SearchRec) ==0)
ListBox1->Items->Add(SearchRec.Name);
ListBox1->ItemIndex = 0;
Label1->Caption = ListBox1->Items->Strings[ListBox1->ItemIndex];
SpeedButton2->Enabled = false;
if (ListBox1->Count == 1)
SpeedButton4->Enabled = false;
else
SpeedButton4->Enabled = true;
}
void __fastcall TForm1::Play()
{
Label1->Caption = ListBox1->Items->Strings[ListBox1->ItemIndex];
MediaPlayer1->FileName = SoundPath + ListBox1->Items->Strings[ListBox1->ItemIndex];
MediaPlayer1->Open();
MediaPlayer1->Play();
min = 0;
sec = 0;
Timer1->Enabled = true;
}
void __fastcall TForm1::Stop()
{
MediaPlayer1->Stop();
Timer1->Enabled = false;
Label2->Caption = "0:";
Label3->Caption = "00";
}
void __fastcall TForm1::Pause()
{
MediaPlayer1->Pause();
Timer1->Enabled = false;
}
void __fastcall TForm1::FormCreate(TObject *Sender)
{
PlayList("");
waveOutGetVolume (0, &volume.Volume);
TrackBar1->Position = - volume.Left;
}
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
if (sec < 59){
sec++;
if (sec < 10)
Label3->Caption = "0" + IntToStr(sec);
else
Label3->Caption = IntToStr(sec);
} else{
sec = 0;
min++;
Label2->Caption = IntToStr(min) + ":";
Label3->Caption = "00";
}
if (MediaPlayer1->Position < MediaPlayer1->Length)
return;
Stop();
if (ListBox1->ItemIndex < ListBox1->Count - 1)
{
ListBox1->ItemIndex += 1;
Play();
if (ListBox1->ItemIndex == ListBox1->Count - 1)
SpeedButton4->Enabled = false;
} else {
mode = 0;
}
}
void __fastcall TForm1::SpeedButton2Click(TObject *Sender)
{
if (mode == 1)
Stop();
ListBox1->ItemIndex -= 1;
Label1->Caption = ListBox1->Items->Strings[ListBox1->ItemIndex];
if (!SpeedButton4->Enabled)
SpeedButton4->Enabled = true;
if (ListBox1->ItemIndex == 0)
SpeedButton2->Enabled = false;
if (mode == 1)
Play();
}
void __fastcall TForm1::ListBox1Click(TObject *Sender)
{
if (MediaPlayer1->Mode == 2) {
Stop();
Play();
msec = MediaPlayer1->Length;
min = msec / 60000;
sec =(msec / 1000) % 60;
Label4->Caption = IntToStr(min)+':'+IntToStr(sec);
}
if (ListBox1->ItemIndex == 0)
SpeedButton2->Enabled = false;
else
SpeedButton2->Enabled = true;
if ( ListBox1->ItemIndex == ListBox1->Count - 1)
SpeedButton4->Enabled = false;
else
SpeedButton4->Enabled = true;
}
void __fastcall TForm1::SpeedButton3Click(TObject *Sender)
{
if ( mode == 1)
{
Stop();
mode = 0;
}
else {
Play();
mode = 1;
}
}
void __fastcall TForm1::SpeedButton4Click(TObject *Sender)
{
if (mode == 1)
Stop();
ListBox1->ItemIndex += 1;
Label1->Caption = ListBox1->Items->Strings[ListBox1->ItemIndex];
if (ListBox1->ItemIndex == ListBox1->Count - 1)
SpeedButton4->Enabled = false;
if (!SpeedButton2->Enabled)
SpeedButton2->Enabled = true;
if (mode == 1)
Play();
}
void __fastcall TForm1::TrackBar1Change(TObject *Sender)
{
volume.Left = - TrackBar1->Position;
volume.Right = - TrackBar1->Position;
waveOutSetVolume(0, volume.Volume);
}
void __fastcall TForm1::SpeedButton1Click(TObject *Sender)
{
AnsiString dir;
if (SelectDirectory("Выберите каталог", "", dir))
{
if (mode == 1)
{
Stop();
mode = 0;
}
SoundPath = dir + "\\";
PlayList(SoundPath);
}
}
void __fastcall TForm1::SpeedButton5Click(TObject *Sender)
{
if (ListBox1->ItemIndex != -1)
ListBox1->Items->Delete(ListBox1->ItemIndex);
else
ShowMessage("Выберіть елементи");
}
void __fastcall TForm1::SpeedButton6Click(TObject *Sender)
{
Pause();
mode = 0;
}
У файлі *назва файлу*.h:
public: // User declarations
__fastcall TForm1(TComponent* Owner);
void __fastcall PlayList (AnsiString path);
void __fastcall Play();
void __fastcall Stop();
void __fastcall Pause();

Залишити відповідь

Ваша e-mail адреса не оприлюднюватиметься. Обов’язкові поля позначені *