记得在刚学delphi时,第一个程序就是一个播放flash的小软件,当时好开心.
今天,我在C#中实现了这个小程序.
播放Flash动画的原理是引用Flash的dll.用它来播放.
现在工具箱中添加这个控件:
1.工具箱上点鼠标右键,出现菜单,选择 "Choose Items...",在出现的窗口选择"Com components"选项卡,
选择Shockwave Flash Object,打上勾,点"OK"按钮.
2.从toolbox中拖放一个flash控件到form中.
3.添加代码.
{
axShockwaveFlash1.Movie = Application.StartupPath+" //music.swf ";
axShockwaveFlash1.Play();
}
注意,传给flash控件Movie属性时一定要传给完整的路径.
另外,还有个属性,我没测试.
EmbedMovie: 是否嵌入到程序的资源中。
From:http://blog.csdn.net/greenerycn/archive/2007/09/03/1770706.aspx
刚刚实现了用c#来播放flash文件,顺便也实现了用c#来播放rm文件.
播放rm文件还是要用activx控件,在安装realplayer后就有了.
1.toolbox->鼠标右键,菜单中选->Choose Items...
代码:
{
this .axRealAudio1.SetControls( " ImageWindow,controlpanel " );
}
private void button1_Click( object sender, EventArgs e)
{
openFileDialog1.Filter = " rm 文件 (*.rm)|*.rm|所有文件 (*.*)|*.* " ;
openFileDialog1.DefaultExt = " rm " ;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
axRealAudio1.Source = openFileDialog1.FileName;
axRealAudio1.DoPlay();
}
}
注意: 如果不设置SetControls则默认没有视频显示.
完成的界面:
同时,还支持mp3的播放.
更详细的AxRealAudio 控件的使用说明参见这里:
http://service.real.com/help/library/guides/production/htmfiles/control.htm
From:http://blog.csdn.net/greenerycn/archive/2007/09/03/1770741.aspx
经过我的验证,完全可以使用,而且不仅支持mp3,还支持wma,wmv.播放wmv的话会自动弹出一个视频播放小窗口.
代码:
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.IO ;
using System.Windows.Forms;
namespace winformFlash
{
/// <summary>
/// 用Win32Api实现的媒体播放类.支持Mp3,wma,wmv
/// </summary>
class Mp3
{
public Mp3()
{
}
//定义API函数使用的字符串变量
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
private string Name = "";
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
private string durLength = "";
[MarshalAs(UnmanagedType.LPTStr, SizeConst = 128)]
private string TemStr = "";
int ilong;
//定义播放状态枚举变量
public enum State
{
mPlaying = 1,
mPuase = 2,
mStop = 3
};
//结构变量
public struct structMCI
{
public bool bMut;
public int iDur;
public int iPos;
public int iVol;
public int iBal;
public string iName;
public State state;
};
public structMCI mc = new structMCI();
//取得播放文件属性
public string FileName
{
get
{
return mc.iName;
}
set
{
//ASCIIEncoding asc = new ASCIIEncoding();
try
{
TemStr = "";
TemStr = TemStr.PadLeft(127, Convert.ToChar(" "));
Name = Name.PadLeft(260, Convert.ToChar(" "));
mc.iName = value;
ilong = APIClass.GetShortPathName(mc.iName, Name, Name.Length);
Name = GetCurrPath(Name);
//Name = "open " + Convert.ToChar(34) + Name + Convert.ToChar(34) + " alias media";
Name = "open " + Convert.ToChar(34) + Name + Convert.ToChar(34) + " alias media";
ilong = APIClass.mciSendString("close all", TemStr, TemStr.Length, 0);
ilong = APIClass.mciSendString(Name, TemStr, TemStr.Length, 0);
ilong = APIClass.mciSendString("set media time format milliseconds", TemStr, TemStr.Length, 0);
mc.state = State.mStop;
}
catch
{
MessageBox.Show("出错错误!");
}
}
}
//播放
public void play()
{
TemStr = "";
TemStr = TemStr.PadLeft(127, Convert.ToChar(" "));
APIClass.mciSendString("play media", TemStr, TemStr.Length, 0);
mc.state = State.mPlaying;
}
//停止
public void StopT()
{
TemStr = "";
TemStr = TemStr.PadLeft(128, Convert.ToChar(" "));
ilong = APIClass.mciSendString("close media", TemStr, 128, 0);
ilong = APIClass.mciSendString("close all", TemStr, 128, 0);
mc.state = State.mStop;
}
public void Puase()
{
TemStr = "";
TemStr = TemStr.PadLeft(128, Convert.ToChar(" "));
ilong = APIClass.mciSendString("pause media", TemStr, TemStr.Length, 0);
mc.state = State.mPuase;
}
private string GetCurrPath(string name)
{
if (name.Length < 1) return "";
name = name.Trim();
name = name.Substring(0, name.Length - 1);
return name;
}
//总时间
public int Duration
{
get
{
durLength = "";
durLength = durLength.PadLeft(128, Convert.ToChar(" "));
APIClass.mciSendString("status media length", durLength, durLength.Length, 0);
durLength = durLength.Trim();
if (durLength == "") return 0;
return (int)(Convert.ToDouble(durLength) / 1000f);
}
}
//当前时间
public int CurrentPosition
{
get
{
durLength = "";
durLength = durLength.PadLeft(128, Convert.ToChar(" "));
APIClass.mciSendString("status media position", durLength, durLength.Length, 0);
mc.iPos = (int)(Convert.ToDouble(durLength) / 1000f);
return mc.iPos;
}
}
}
}
public class APIClass
{
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern int GetShortPathName (
string lpszLongPath,
string shortFile,
int cchBuffer
);
[DllImport("winmm.dll", EntryPoint="mciSendString", CharSet = CharSet.Auto)]
public static extern int mciSendString (
string lpstrCommand,
string lpstrReturnString,
int uReturnLength,
int hwndCallback
);
}