1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Media;
using System.Runtime.InteropServices;
using System.Threading;
namespace JoshPlayer
{
public partial class MidiForm : Form
{
private String filename;
private String filelength;
private String currenttime;
private bool go;
delegate void playcallback();
Thread playthread;
public MidiForm(String f)
{
InitializeComponent();
filename = f;
this.Text = filename;
}
private void MidiForm_Load(object sender, EventArgs e)
{
doload();
}
private void doload()
{
this.Text = filename;
open();
playthread = new Thread(new ThreadStart(play));
playthread.Start();
}
[DllImport("winmm.dll")]
private static extern long mciSendString(string strCommand, StringBuilder strReturn, int iReturnLength, IntPtr hwndCallback);
[DllImport("Winmm.dll")]
private static extern long PlaySound(byte[] data, IntPtr hMod, UInt32 dwFlags);
public void play()
{
StringBuilder temptimer=new StringBuilder();
mciSendString("Play mymidi", null, 0, IntPtr.Zero);
if (this.timelabel.InvokeRequired)
{
playcallback i = new playcallback(play);
this.Invoke(i);
}
else
{
while (go)
{
mciSendString("status mymidi position", temptimer, 128, IntPtr.Zero);
this.timelabel.Text = temptimer.ToString() + " \\ " + filelength;
// playthread.Suspend();
}
}
}
public void open()
{
StringBuilder temptime=new StringBuilder(128);
mciSendString("open \"" + filename + "\" alias " + "mymidi", null, 0, IntPtr.Zero);
mciSendString("set mymidi time format samples", null, 0, IntPtr.Zero);
mciSendString("status mymidi length", temptime, 128, IntPtr.Zero);
filelength = temptime.ToString();
currenttime = "0";
timelabel.Text = currenttime + " \\ " + filelength;
go=true;
}
public void stop()
{
mciSendString("close mymidi", null, 0, IntPtr.Zero);
go=false;
playthread.Abort();
}
public void pause()
{
mciSendString("pause mymidi", null, 0, IntPtr.Zero);
}
private void MidiForm_FormClosing(object sender, FormClosingEventArgs e)
{
stop();
}
private void stopbutton_Click(object sender, EventArgs e)
{
stop();
}
private void playbutton_Click(object sender, EventArgs e)
{
playthread = new Thread(new ThreadStart(play));
playthread.Start();
}
private void pausebutton_Click(object sender, EventArgs e)
{
pause();
}
}
}
|