//--------------------------------------------------------------------------- #include
#pragma hdrstop #include "MP3PlayerU.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "WMPLib_OCX"
#pragma resource "*.dfm"
TForm1 *Form1;
TStringList *fn = new TStringList();
int next=0;
bool on_off=true;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void TForm1::FindFilesInDirectory(String directory, String extension, bool searchSubdir)
{
WIN32_FIND_DATA filedata ; // Structure for file data
HANDLE filehandle ; // Handle for searching if( extension == "" )
extension = "*.*";
// Pass 1 - Search for the files within the directory.
filehandle = FindFirstFile((directory extension).c_str(), &filedata) ;
if( filehandle != INVALID_HANDLE_VALUE )
{
do
{
if( (filedata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0 )
{
// Display the file name.
TListItem *li = ListView1->Items->Add();
// dirPath = ExtractFileDir(directory filedata.cFileName);
// dirPath=directory;
fn->Add(directory filedata.cFileName);
li->Caption = filedata.cFileName ;
// Display the file extension.
li->SubItems->Add(ExtractFileExt(filedata.cFileName));
/*
// Display the file size.
if( filedata.nFileSizeHigh == 0 )
{
int filesize = filedata.nFileSizeLow 1023;
if( filesize >= 1073741824 ) // 1024^3
li->SubItems->Add(String(filesize / 1073741824) " GB") ;
else if( filesize >= 1048576 ) // 1024^2
li->SubItems->Add(String(filesize / 1048576) " MB") ;
else
li->SubItems->Add(String(filesize / 1024) " KB") ;
// Add filesize to SubItems, for sorting file size purpose.
li->SubItems->Add(String(filesize));
}
else
{
// If nFileSizeHigh is non-zero then this is a **HUGE** file.
double filesize = (MAXDWORD * filedata.nFileSizeHigh) filedata.nFileSizeLow;
if( filesize >= 1073741824.0 ) // 1024^3
li->SubItems->Add(String(filesize / 1073741824.0) " GB") ;
else if( filesize >= 1048576.0 ) // 1024^2
li->SubItems->Add(String(filesize / 1048576.0) " MB") ;
else
li->SubItems->Add(String(filesize / 1024.0) " KB") ;
// Add filesize to SubItems, for sorting file size purpose.
li->SubItems->Add(String(filesize));
}
*/
Application->ProcessMessages() ;
}
} while(FindNextFile(filehandle, &filedata)) ;
FindClose(filehandle) ;
} if(searchSubdir) // If user wanna search subdirectories..
{
// Pass 2 - Search for all the subdirectories within this directory
filehandle = FindFirstFile ((directory "*.*").c_str (), &filedata) ;
if( filehandle != INVALID_HANDLE_VALUE)
{
do
{
if( (filedata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0 &&
String (filedata.cFileName) != "." && String (filedata.cFileName) != "..")
{
// Recursive call here
FindFilesInDirectory(directory filedata.cFileName "\\", extension, true) ;
}
Application->ProcessMessages() ;
} while(FindNextFile(filehandle, &filedata)) ;
FindClose(filehandle) ;
}
Application->ProcessMessages() ;
}
}
//-------------------------------------------------------------------------
// 檔案區點選資料
void __fastcall TForm1::ListView1Click(TObject *Sender)
{
WindowsMediaPlayer1->controls->stop(); next =ListView1->Selected->Index;
IWMPMediaPtr media = WindowsMediaPlayer1->mediaCollection->add(WideString(fn->Strings[next]).c_bstr());
WindowsMediaPlayer1->URL = media->get_sourceURL();
WindowsMediaPlayer1->controls->play();
Form1->Caption=ExtractFileDir(fn->Strings[next]);
StatusBar1->Panels->Items[0]->Text="檔名︰" ExtractFileName(fn->Strings[next]);
StatusBar1->Panels->Items[1]->Text= "Index︰【" IntToStr(next) "】";
StatusBar1->Panels->Items[2]->Text= "Total︰〔" IntToStr(fn->Count-1) "〕";
}
//--------------------------------------------------------------------------- void __fastcall TForm1::FormCreate(TObject *Sender)
{
RadioGroup1->ItemIndex=0;
Animate1->Visible=false;
}
//---------------------------------------------------------------------------
// 蒐集媒體資料 void __fastcall TForm1::ToolButton1Click(TObject *Sender)
{
String extend,dir;
Animate1->Visible=true;
Animate1->Active=true;
switch(RadioGroup1->ItemIndex)
{
case 0: extend="*.mp3"; break;
case 1: extend="*.WAV"; break;
case 2: extend="*.MIDI"; break;
case 3: extend=""; break;
}
dir= DriveComboBox1->Drive;
ListView1->Items->Clear(); FindFilesInDirectory(dir AnsiString(":\\"), extend.c_str(), true); // 全部格式
Animate1->Active=false;
Animate1->Visible=false;
ToolButton1->Enabled=false;
}
//--------------------------------------------------------------------------- // 網路廣播
void __fastcall TForm1::RadioGroup2Click(TObject *Sender)
{
switch(RadioGroup2->ItemIndex)
{
case 0: WindowsMediaPlayer1->controls->stop(); // 台北之音
WindowsMediaPlayer1->URL="mms://203.187.31.160/fm1077";
WindowsMediaPlayer1->controls->play();
break;
case 1: WindowsMediaPlayer1->controls->stop();// 綠色和平廣播電台
WindowsMediaPlayer1->URL="http://voiceoftaiwan.streamguys.com:7040";
WindowsMediaPlayer1->controls->play();
break;
}
}
//--------------------------------------------------------------------------- void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
int mode=WindowsMediaPlayer1->playState;
if(mode==1 && on_off==true)
{
next ;
ListView1->Items->Item[next]->Selected;
IWMPMediaPtr media = WindowsMediaPlayer1->mediaCollection->add(WideString(fn->Strings[next]).c_bstr());
WindowsMediaPlayer1->URL = media->get_sourceURL();
WindowsMediaPlayer1->controls->play(); ListView1->Items->Item[next]->Selected=true;
ListView1->SetFocus(); Form1->Caption=ExtractFileDir(fn->Strings[next]);
StatusBar1->Panels->Items[0]->Text="檔名︰" ExtractFileName(fn->Strings[next]);
StatusBar1->Panels->Items[1]->Text= "Index︰【" IntToStr(next) "】";
//StatusBar1->Panels->Items[2]->Text= "Total︰〔" IntToStr(fn->Count-1) "〕"; }
}
//--------------------------------------------------------------------------- void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
delete fn;
}
//--------------------------------------------------------------------------- void __fastcall TForm1::DriveComboBox1Change(TObject *Sender)
{
ToolButton1->Enabled=true;
}
//--------------------------------------------------------------------------- void __fastcall TForm1::ToolButton2Click(TObject *Sender)
{
if(on_off) // 按鍵浮上= 自動
{
Timer1->Enabled=true;
ToolButton2->Down=true;
on_off=false;
ToolButton2->Hint="自動下一曲目。";
}
else { // 按鍵凹下= 手動
Timer1->Enabled=false;
ToolButton2->Down=false;
on_off=true;
ToolButton2->Hint="取消下一曲目。";
}
}
//--------------------------------------------------------------------------- yangshuh
------
yangshuh