Sound Player


cat SoundPlayer_test.cpp
// SoundPlayer_test.cpp

#using <mscorlib.dll>
#using <System.dll>
#using <System.Windows.Forms.dll>

using namespace System;
using namespace System::Windows::Forms;

#include "SoundPlayer_test.h"
#include "myApp.h"

[STAThreadAttribute]
int main(array<System::String ^> ^args){
	Application::Run(gcnew myApp()); // message loop
	return EXIT_SUCCESS;
}
cat myApp.h
// myApp.h

#pragma once
ref class myApp : public Form {
public:
	myApp();		//constructor
//	~myApp();		//destructor
//	!myApp();		//finalizer
private:
	void InitializeComponent(void);		// 
	int gridSize;		//

	System::Windows::Forms::Label^ label1;
	System::Windows::Forms::Button^ playOnceSyncButton;
	System::Windows::Forms::Button^ playOnceAsyncButton;
	System::Windows::Forms::Button^ playLoopAsyncButton;
	System::Windows::Forms::Button^ selectFileButton;
	System::Windows::Forms::Button^ stopButton;
	System::Windows::Forms::Button^ loadSyncButton;
	System::Windows::Forms::Button^ loadAsyncButton;
	System::Windows::Forms::StatusBar^ statusBar;
	void selectFileButton_Click(System::Object^ sender, System::EventArgs^ e);
	void loadSyncButton_Click(System::Object^ sender, System::EventArgs^ e);
	void loadAsyncButton_Click(System::Object^ sender, System::EventArgs^ e);

	System::Windows::Forms::TextBox^ filePathTextBox;
	void filePathTextBox_TextChanged(Object^ sender, EventArgs^ e);

	System::Media::SoundPlayer^ player;
	void playOnceSyncButton_Click(Object^ sender, System::EventArgs^ e);
	void playOnceAsyncButton_Click(Object^ sender, System::EventArgs^ e);
	void playLoopAsyncButton_Click(Object^ sender, System::EventArgs^ e);
	void stopButton_Click(System::Object^ sender, System::EventArgs^ e);
	void player_LoadCompleted(Object^ sender, System::ComponentModel::AsyncCompletedEventArgs^ e);
	void player_LocationChanged(Object^ sender, System::EventArgs^ e);

	void EnablePlaybackControls(bool enabled);
	void ReportStatus(String^ statusMessage);
	void InitializeSound();
	void InitializeControls();
	        
	
	        
};		//semi-colon
cat myApp.cpp
// myApp.cpp

#using <mscorlib.dll>
#using <System.dll>
#using <System.Drawing.dll>
#using <System.Windows.Forms.dll>

using namespace System;
using namespace System::ComponentModel;
using namespace System::Drawing;
using namespace System::Windows::Forms;

#include "SoundPlayer_test.h"
#include "myApp.h"

//	myApp::~myApp(){}		//destructor
//	myApp::!myApp(){}		//finalizer
myApp::myApp(){		//constructor
	this->InitializeComponent();
	this->EnablePlaybackControls(false);
	this->InitializeControls();
	this->InitializeSound();
}
void myApp::InitializeControls(void){
	System::Windows::Forms::StatusBarPanel^ panel = gcnew StatusBarPanel();
	panel->BorderStyle = System::Windows::Forms::StatusBarPanelBorderStyle::Sunken;
	panel->Text = "Ready.";
	panel->AutoSize = System::Windows::Forms::StatusBarPanelAutoSize::Spring;
	this->statusBar->ShowPanels = true;
	this->statusBar->Panels->Add(panel);

}
void myApp::InitializeSound(void) {
	this->player = gcnew System::Media::SoundPlayer();
	this->player->LoadCompleted += gcnew System::ComponentModel::AsyncCompletedEventHandler(this, &myApp::player_LoadCompleted);

	this->player->SoundLocationChanged += gcnew EventHandler(this,&myApp::player_LocationChanged);
}

void myApp::selectFileButton_Click(Object^ sender, System::EventArgs^ e) {
	System::Windows::Forms::OpenFileDialog^ dlg = gcnew System::Windows::Forms::OpenFileDialog();
	dlg->CheckFileExists = true;
	dlg->Filter = "WAV files (*.wav)|*.wav";
	dlg->DefaultExt = ".wav";
	if (dlg->ShowDialog() == System::Windows::Forms::DialogResult::OK)
	{
		this->filePathTextBox->Text = dlg->FileName;
		this->player->SoundLocation = filePathTextBox->Text;
	}
}
void myApp::ReportStatus(System::String^ statusMessage) {
	if ((statusMessage != nullptr) && (statusMessage != System::String::Empty)) {
		this->statusBar->Panels[0]->Text = statusMessage;
	}
}
void myApp::EnablePlaybackControls(bool enabled)
{   
	this->playOnceSyncButton->Enabled = enabled;
	this->playOnceAsyncButton->Enabled = enabled;
	this->playLoopAsyncButton->Enabled = enabled;
	this->stopButton->Enabled = enabled;
}
void myApp::filePathTextBox_TextChanged(Object^ sender, EventArgs^ e) {
	this->EnablePlaybackControls(false);
}

void myApp::loadSyncButton_Click(Object^ sender, System::EventArgs^ e) {   
	this->EnablePlaybackControls(false);

	try {
		player->SoundLocation = filePathTextBox->Text;
		player->Load();
	} catch (Exception^ ex) {
		ReportStatus(ex->Message);
	}
}

void myApp::loadAsyncButton_Click(System::Object^ sender, System::EventArgs^ e) {
	EnablePlaybackControls(false);

	try {
		player->SoundLocation = this->filePathTextBox->Text;
		player->LoadAsync();
	} catch (Exception^ ex) {
		ReportStatus(ex->Message);
	}
}
void myApp::playOnceSyncButton_Click(Object^ sender, System::EventArgs^ e) {    
	ReportStatus("Playing .wav file synchronously.");
	player->PlaySync();
	ReportStatus("Finished playing .wav file synchronously.");
}

void myApp::playOnceAsyncButton_Click(Object^ sender, System::EventArgs^ e)
{
	ReportStatus("Playing .wav file asynchronously.");
	player->Play();
}
void myApp::playLoopAsyncButton_Click(Object^ sender, System::EventArgs^ e) {
	ReportStatus("Looping .wav file asynchronously.");
	player->PlayLooping();
}
void myApp::stopButton_Click(System::Object^ sender, System::EventArgs^ e) {    
	player->Stop();
	ReportStatus("Stopped by user.");
}
void myApp::player_LoadCompleted(Object^ sender, System::ComponentModel::AsyncCompletedEventArgs^ e) {   
	String^ message = String::Format("LoadCompleted: {0}", this->filePathTextBox->Text);
	ReportStatus(message);
	EnablePlaybackControls(true);
}
void myApp::player_LocationChanged(Object^ sender, EventArgs^ e) {   
	String^ message = String::Format("SoundLocationChanged: {0}", player->SoundLocation);
	ReportStatus(message);
}




void myApp::InitializeComponent(void){		//constructor
	this->gridSize=20;		//
	int tabindex=0;		//

	this->filePathTextBox = gcnew System::Windows::Forms::TextBox();
	this->selectFileButton = gcnew System::Windows::Forms::Button();
	this->label1 = gcnew System::Windows::Forms::Label();
	this->loadSyncButton = gcnew System::Windows::Forms::Button();
	this->playOnceSyncButton = gcnew System::Windows::Forms::Button();
	this->playOnceAsyncButton = gcnew System::Windows::Forms::Button();
	this->stopButton = gcnew System::Windows::Forms::Button();
	this->playLoopAsyncButton = gcnew System::Windows::Forms::Button();
	this->statusBar = gcnew System::Windows::Forms::StatusBar();
	this->loadAsyncButton = gcnew System::Windows::Forms::Button();
	this->SuspendLayout();

	this->filePathTextBox->Anchor = ((System::Windows::Forms::AnchorStyles)(((System::Windows::Forms::AnchorStyles::Top | System::Windows::Forms::AnchorStyles::Left) | System::Windows::Forms::AnchorStyles::Right)));
	this->filePathTextBox->Location = System::Drawing::Point(7, 25);
	this->filePathTextBox->Name = "filepathTextbox";
	this->filePathTextBox->Size = System::Drawing::Size(263, 20);
	this->filePathTextBox->TabIndex = 1;
	this->filePathTextBox->Text = "";
	this->filePathTextBox->TextChanged += gcnew System::EventHandler(this,&myApp::filePathTextBox_TextChanged);

	this->selectFileButton->Anchor = ((System::Windows::Forms::AnchorStyles)((System::Windows::Forms::AnchorStyles::Top | System::Windows::Forms::AnchorStyles::Right)));
	this->selectFileButton->Location = System::Drawing::Point(276, 25);
	this->selectFileButton->Name = "selectFileButton";
	this->selectFileButton->Size = System::Drawing::Size(23, 21);
	this->selectFileButton->TabIndex = 2;
	this->selectFileButton->Text = "...";
	this->selectFileButton->Click += gcnew System::EventHandler(this, &myApp::selectFileButton_Click);

	this->label1->Location = System::Drawing::Point(7, 7);
	this->label1->Name = "label1";
	this->label1->Size = System::Drawing::Size(145, 17);
	this->label1->TabIndex = 3;
	this->label1->Text = ".wav path or URL:";

	this->loadSyncButton->Location = System::Drawing::Point(7, 53);
	this->loadSyncButton->Name = "loadSyncButton";
	this->loadSyncButton->Size = System::Drawing::Size(142, 23);
	this->loadSyncButton->TabIndex = 4;
	this->loadSyncButton->Text = "Load Synchronously";
	this->loadSyncButton->Click += gcnew System::EventHandler(this,&myApp::loadSyncButton_Click);

	this->playOnceSyncButton->Location = System::Drawing::Point(7, 86);
	this->playOnceSyncButton->Name = "playOnceSyncButton";
	this->playOnceSyncButton->Size = System::Drawing::Size(142, 23);
	this->playOnceSyncButton->TabIndex = 5;
	this->playOnceSyncButton->Text = "Play Once Synchronously";
	this->playOnceSyncButton->Click += gcnew System::EventHandler(this, &myApp::playOnceSyncButton_Click);

	this->playOnceAsyncButton->Location = System::Drawing::Point(149, 86);
	this->playOnceAsyncButton->Name = "playOnceAsyncButton";
	this->playOnceAsyncButton->Size = System::Drawing::Size(147, 23);
	this->playOnceAsyncButton->TabIndex = 6;
	this->playOnceAsyncButton->Text = "Play Once Asynchronously";
	this->playOnceAsyncButton->Click += gcnew System::EventHandler(this,&myApp::playOnceAsyncButton_Click);

	this->stopButton->Location = System::Drawing::Point(149, 109);
	this->stopButton->Name = "stopButton";
	this->stopButton->Size = System::Drawing::Size(147, 23);
	this->stopButton->TabIndex = 7;
	this->stopButton->Text = "Stop";
	this->stopButton->Click += gcnew System::EventHandler(this,&myApp::stopButton_Click);

	this->playLoopAsyncButton->Location = System::Drawing::Point(7, 109);
	this->playLoopAsyncButton->Name = "playLoopAsyncButton";
	this->playLoopAsyncButton->Size = System::Drawing::Size(142, 23);
	this->playLoopAsyncButton->TabIndex = 8;
	this->playLoopAsyncButton->Text = "Loop Asynchronously";
	this->playLoopAsyncButton->Click += gcnew System::EventHandler(this,&myApp::playLoopAsyncButton_Click);

	this->statusBar->Location = System::Drawing::Point(0, 146);
	this->statusBar->Name = "statusBar";
	this->statusBar->Size = System::Drawing::Size(306, 22);
	this->statusBar->SizingGrip = false;
	this->statusBar->TabIndex = 9;
	this->statusBar->Text = "(no status)";

	this->loadAsyncButton->Location = System::Drawing::Point(149, 53);
	this->loadAsyncButton->Name = "loadAsyncButton";
	this->loadAsyncButton->Size = System::Drawing::Size(147, 23);
	this->loadAsyncButton->TabIndex = 10;
	this->loadAsyncButton->Text = "Load Asynchronously";
	this->loadAsyncButton->Click += gcnew System::EventHandler(this,&myApp::loadAsyncButton_Click);

	this->ClientSize = System::Drawing::Size(306, 168);
	this->Controls->Add(this->loadAsyncButton);
	this->Controls->Add(this->statusBar);
	this->Controls->Add(this->playLoopAsyncButton);
	this->Controls->Add(this->stopButton);
	this->Controls->Add(this->playOnceAsyncButton);
	this->Controls->Add(this->playOnceSyncButton);
	this->Controls->Add(this->loadSyncButton);
	this->Controls->Add(this->label1);
	this->Controls->Add(this->selectFileButton);
	this->Controls->Add(this->filePathTextBox);
	this->MinimumSize = System::Drawing::Size(310, 165);
	this->Name = "SoundTestForm";
	this->SizeGripStyle = System::Windows::Forms::SizeGripStyle::Show;
	this->Text = "Sound API Test Form";
	this->ResumeLayout(false);

}


cat AssemblyInfo.cpp
// AssemblyInfo.cpp
//
using namespace System;
using namespace System::Reflection;
using namespace System::Runtime::CompilerServices;
using namespace System::Runtime::InteropServices;
using namespace System::Security::Permissions;
	
//
// General Assembly Information
//
[assembly:AssemblyCompanyAttribute("smnb")];
[assembly:AssemblyCopyrightAttribute("Copyright smnb(c)  2007")];
[assembly:AssemblyTrademarkAttribute("smnb")];

[assembly:AssemblyProductAttribute("AssemblyInfo_test")];
[assembly: AssemblyFileVersion ("2.1.1852.0")];
[assembly: AssemblyInformationalVersion("1.2")] ; // Win32 version visible in Property

[assembly:AssemblyTitleAttribute("AssemblyInfo_test")];
[assembly:AssemblyDescriptionAttribute("This is a test.")];

	// Assembly Version  (Major Version).(Minor Version).(Build Number).(Revision)
//[assembly:AssemblyVersionAttribute("1.0.*")]; // Build and Revision are using default.
// Build will be number of days since 2000/1/1
// Revision will be half of number of seconds since midnight of the day.
// ... doesn't work outside Visual Studio probably..
[assembly:AssemblyVersionAttribute("1.0.1.0")]; // Build and Revision are using default.
[assembly:AssemblyConfigurationAttribute("Release")]; // or debug
[assembly:AssemblyCultureAttribute("")]; // don't define this for main assembly

[assembly:ComVisible(false)]; // COM can't refer types in this Assembly

[assembly:CLSCompliantAttribute(true)];// Class marked as CLS compliant.

// study http://www.microsoft.com/japan/msdn/security/guidance/secmod81.mspx
[assembly:SecurityPermission(SecurityAction::RequestMinimum, UnmanagedCode = true)];

cat makefile
# makefile for SoundPlayer_test

.PHONY: all test mmm clean 
.SUFFIXES: .exe .obj .txt .resources .dll

TARGET=SoundPlayer_test

CULTUREDIRS=
DLLS=

all: $(TARGET).exe $(DLLS)


LOPT=-ENTRY:main -SUBSYSTEM:WINDOWS

OBJS=$(TARGET).obj myApp.obj AssemblyInfo.obj

$(TARGET).obj: $(TARGET).cpp myApp.h

myApp.obj: myApp.cpp myApp.h


AssemblyInfo.obj: AssemblyInfo.cpp

meta: $(TARGET).exe
	ILDasm /out:$@ /metadata $<

$(TARGET).exe: $(OBJS)  
	link $(LOPT) $^ 

.cpp.obj:
	cl -c -Wall -Od -clr:safe $<

test: $(TARGET).exe
	./$(TARGET).exe &

clean:
	@-rm $(TARGET).exe *.obj *~ $(TARGET).exe.manifest 1>/dev/null 2>&1
	@-rm *.resources 1>/dev/null 2>&1
	@-rm -fr $(CULTUREDIRS)

mmm:
	cat $(TARGET).cpp
	cat myApp.h
	cat myApp.cpp
	cat AssemblyInfo.cpp
	@echo
	cat makefile

html:
	cat /dev/clipboard | code2html.exe | unix2dos

install: $(TARGET).exe
	cp $(TARGET).exe ~/bin
	cp $(TARGET).exe.manifest ~/bin