以前から思っていたのですが、パソコンで音楽を再生するソフトは重たいです。Windows Media Playerとか、Quick Timeとかですね。ただ、BGMが欲しいだけなのに。変な視覚エフェクトとか、ネットワークから情報を集めてきたりとか、そういうのはいらないんですが。。。
というわけで、音楽再生のソフトを作ろうと思います。まぁ、Win32APIのmciSendString関数を使うだけの簡単なソフトです。
手始めに、mciSendString関数を使って、色々試してみました。
Statusの変化するタイミングとか、PauseとStopの違いとか、使ってみないと分からないこともあるもんです。面白かったのが、Volumeの設定です。Volumeは0-1000で変化するみたいですが、例えば800に設定しようとしても803になったりしました。ハードウェアの都合なのかもしれません。へぇー。
ソースコードはご自由にご利用ください。ただし、趣味のプログラムなので保証はありません。
(ちなみに、メニューを作っているのは、ウィルス対策ソフトと戦ったからです。)
using System;
using System.IO;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Collections.Generic;
/*--------------------------------------------------*/
class Program
{
[STAThread]
static void Main()
{
Application.Run( new MciTest());
}
}
/*--------------------------------------------------*/
class MciTest: Form
{
/*--------------------------------------------------*/
[DllImport( "Winmm.dll")]
extern static int mciSendString( string cmd, System.Text.StringBuilder retStr, uint ret, IntPtr hwnd);
/*--------------------------------------------------*/
private MenuStrip ms;
private ToolStripMenuItem[][] tsmi;
private Button[] btn;
private const string DeviceID = "mysound";
private string filename = @"C:\Windows\Media\Alarm02.wav";
/*--------------------------------------------------*/
public MciTest()
{
/*--------------------------------------------------*/
this.ClientSize = new Size( 360, 240);
this.Text = "MCITest";
this.Load += new EventHandler( this.Form_Load);
this.Closed += new EventHandler( this.Form_Closed);
/*--------------------------------------------------*/
this.CreateMenu();
/*--------------------------------------------------*/
this.btn = new Button[16];
for( int i = 0; i < this.btn.Length; i++)
{
this.btn[i] = new Button();
this.btn[i].SetBounds( 20 + 80 * ( i % 4), 40 + 40 * ( i / 4), 80, 40);
this.btn[i].Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom;
this.btn[i].Text = i.ToString();
this.btn[i].Click += new EventHandler( this.btn_Click);
this.Controls.Add( this.btn[i]);
}
this.btn[0].Text = "open";
this.btn[1].Text = "close";
this.btn[2].Text = "play";
this.btn[3].Text = "stop";
this.btn[4].Text = "pause";
this.btn[5].Text = "resume";
this.btn[6].Text = "seek";
this.btn[7].Text = "position";
this.btn[8].Text = "volume quiet";
this.btn[9].Text = "volume loud";
this.btn[10].Text = "audio on";
this.btn[11].Text = "audio off";
this.btn[12].Text = "info";
this.btn[13].Text = "mode";
}
/*--------------------------------------------------*/
private void Form_Load( object sender, EventArgs e)
{
Console.WriteLine( "Form_Load");
}
private void Form_Closed( object sender, EventArgs e)
{
Console.WriteLine( "Form_Closed");
int ret;
string cmd;
cmd = "stop " + MciTest.DeviceID;
ret = mciSendString( cmd, null, 0, IntPtr.Zero);
cmd = "close " + MciTest.DeviceID;
ret = mciSendString( cmd, null, 0, IntPtr.Zero);
}
/*--------------------------------------------------*/
private void CreateMenu()
{
this.ms = new MenuStrip();
this.tsmi = new ToolStripMenuItem[1][];
this.tsmi[0] = new ToolStripMenuItem[3];
for( int i = 0; i < this.tsmi.Length; i++)
{
for( int j = 0; j < this.tsmi[i].Length; j++)
{
this.tsmi[i][j] = new ToolStripMenuItem();
}
}
this.tsmi[0][0].Text = "Menu (&M)";
this.tsmi[0][1].Text = "File (&F)";
this.tsmi[0][2].Text = "Close (&C)";
for( int i = 0; i < this.tsmi.Length; i++)
{
this.ms.Items.Add( this.tsmi[i][0]);
for( int j = 1; j < this.tsmi[i].Length; j++)
{
this.tsmi[i][0].DropDownItems.Add( this.tsmi[i][j]);
this.tsmi[i][j].Click += new EventHandler( this.Menu_Click);
}
}
this.Controls.Add( this.ms);
this.MainMenuStrip = ms;
}
/*--------------------------------------------------*/
private void Menu_Click( object sender, EventArgs e)
{
Console.WriteLine( sender.ToString());
if( sender == this.tsmi[0][1])
{
MessageBox.Show( Application.ExecutablePath);
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "All files | *.*";
ofd.Multiselect = true;
if( ofd.ShowDialog() == DialogResult.OK)
{
MessageBox.Show( ofd.FileNames[0]);
this.filename = ofd.FileNames[0];
}
}
else if( sender == this.tsmi[0][2])
{
this.Close();
}
}
/*--------------------------------------------------*/
private void btn_Click( object sender, EventArgs e)
{
Console.WriteLine( "btn_Click" + "\t" + ( (Button)sender).Text);
int ret;
string cmd;
StringBuilder retStr = new StringBuilder( 256);
if( ( (Button)sender).Text == "open")
{
cmd = "open " + "\"" + this.filename + "\"" + " alias " + MciTest.DeviceID;
ret = mciSendString( cmd, null, 0, IntPtr.Zero);
Console.WriteLine( cmd);
}
else if( ( (Button)sender).Text == "close")
{
cmd = "close " + MciTest.DeviceID;
ret = mciSendString( cmd, null, 0, IntPtr.Zero);
Console.WriteLine( cmd);
}
else if( ( (Button)sender).Text == "play")
{
cmd = "play " + MciTest.DeviceID + " notify";
ret = mciSendString( cmd, null, 0, this.Handle);
Console.WriteLine( cmd);
}
else if( ( (Button)sender).Text == "stop")
{
cmd = "stop " + MciTest.DeviceID;
ret = mciSendString( cmd, null, 0, IntPtr.Zero);
Console.WriteLine( cmd);
}
else if( ( (Button)sender).Text == "pause")
{
cmd = "pause " + MciTest.DeviceID;
ret = mciSendString( cmd, null, 0, IntPtr.Zero);
Console.WriteLine( cmd);
cmd = "status " + MciTest.DeviceID + " pause timeout";
ret = mciSendString( cmd, retStr, (uint) retStr.Capacity, IntPtr.Zero);
Console.WriteLine( "pause timeout : " + retStr);
}
else if( ( (Button)sender).Text == "resume")
{
cmd = "resume " + MciTest.DeviceID;
ret = mciSendString( cmd, null, 0, IntPtr.Zero);
Console.WriteLine( cmd);
}
else if( ( (Button)sender).Text == "seek")
{
cmd = "seek " + MciTest.DeviceID + " to start";
ret = mciSendString( cmd, null, 0, IntPtr.Zero);
Console.WriteLine( cmd);
}
else if( ( (Button)sender).Text == "position")
{
cmd = "status " + MciTest.DeviceID + " position";
ret = mciSendString( cmd, retStr, (uint) retStr.Capacity, IntPtr.Zero);
Console.Write( "position : " + retStr);
cmd = "status " + MciTest.DeviceID + " length";
ret = mciSendString( cmd, retStr, (uint) retStr.Capacity, IntPtr.Zero);
Console.WriteLine( " / " + retStr);
}
else if( ( (Button)sender).Text == "volume quiet")
{
cmd = "status " + MciTest.DeviceID + " volume";
ret = mciSendString( cmd, retStr, (uint) retStr.Capacity, IntPtr.Zero);
Console.WriteLine( "volume : " + retStr);
cmd = "setaudio " + MciTest.DeviceID + " volume to 200";
ret = mciSendString( cmd, null, 0, IntPtr.Zero);
Console.WriteLine( cmd);
cmd = "status " + MciTest.DeviceID + " volume";
ret = mciSendString( cmd, retStr, (uint) retStr.Capacity, IntPtr.Zero);
Console.WriteLine( "volume : " + retStr);
}
else if( ( (Button)sender).Text == "volume loud")
{
cmd = "status " + MciTest.DeviceID + " volume";
ret = mciSendString( cmd, retStr, (uint) retStr.Capacity, IntPtr.Zero);
Console.WriteLine( "volume : " + retStr);
cmd = "setaudio " + MciTest.DeviceID + " volume to 800";
ret = mciSendString( cmd, null, 0, IntPtr.Zero);
Console.WriteLine( cmd);
cmd = "status " + MciTest.DeviceID + " volume";
ret = mciSendString( cmd, retStr, (uint) retStr.Capacity, IntPtr.Zero);
Console.WriteLine( "volume : " + retStr);
}
else if( ( (Button)sender).Text == "audio on")
{
cmd = "set " + MciTest.DeviceID + " audio all on";
ret = mciSendString( cmd, null, 0, IntPtr.Zero);
Console.WriteLine( cmd);
}
else if( ( (Button)sender).Text == "audio off")
{
cmd = "set " + MciTest.DeviceID + " audio all off";
ret = mciSendString( cmd, null, 0, IntPtr.Zero);
Console.WriteLine( cmd);
}
else if( ( (Button)sender).Text == "info")
{
cmd = "info " + MciTest.DeviceID + " file";
ret = mciSendString( cmd, retStr, (uint) retStr.Capacity, IntPtr.Zero);
Console.WriteLine( "info : " + retStr);
}
else if( ( (Button)sender).Text == "mode")
{
cmd = "status " + MciTest.DeviceID + " mode";
ret = mciSendString( cmd, retStr, (uint) retStr.Capacity, IntPtr.Zero);
Console.WriteLine( "mode : " + retStr);
}
}
/*--------------------------------------------------*/
protected override void WndProc( ref Message m)
{
base.WndProc( ref m);
if( m.Msg == 0x03B9) //MM_MCINOTIFY message
{
Console.WriteLine( "Notify");
StringBuilder retStr = new StringBuilder( 256);
string cmd = "status " + MciTest.DeviceID + " mode";;
int ret = mciSendString( cmd, retStr, (uint) retStr.Capacity, IntPtr.Zero);;
Console.WriteLine( "mode : " + retStr);
}
}
}
0 件のコメント:
コメントを投稿