首页 新闻 会员 周边

【windows mobile+C#】播放wav+wma+MP3+mid

0
悬赏园豆:50 [已关闭问题] 关闭于 2010-04-19 10:41

我现在用的是CoreDll.DLL,但是只能播放wav格式的,我想实现的功能是读取到短信铃声,并播放出来,请高手帮帮忙。

 

代码
public class Sound
{
private byte[] m_soundBytes;
private string m_fileName;

private enum Flags
{
SND_SYNC
= 0x0000, /* play synchronously (default) */
SND_ASYNC
= 0x0001, /* play asynchronously */
SND_NODEFAULT
= 0x0002, /* silence (!default) if sound not found */
SND_MEMORY
= 0x0004, /* pszSound points to a memory file */
SND_LOOP
= 0x0008, /* loop the sound until next sndPlaySound */
SND_NOSTOP
= 0x0010, /* don't stop any currently playing sound */
SND_NOWAIT
= 0x00002000, /* don't wait if the driver is busy */
SND_ALIAS
= 0x00010000, /* name is a registry alias */
SND_ALIAS_ID
= 0x00110000, /* alias is a predefined ID */
SND_FILENAME
= 0x00020000, /* name is file name */
SND_RESOURCE
= 0x00040004 /* name is resource name or atom */
}

[DllImport(
"CoreDll.DLL", EntryPoint = "PlaySound", SetLastError = true)]
private extern static int WCE_PlaySound(string szSound, IntPtr hMod, int flags);

[DllImport(
"CoreDll.DLL", EntryPoint = "PlaySound", SetLastError = true)]
private extern static int WCE_PlaySoundBytes(byte[] szSound, IntPtr hMod, int flags);

/// <summary>
/// Construct the Sound object to play sound data from the specified file.
/// </summary>
public Sound(string fileName)
{
m_fileName
= fileName;
}
public Sound()
{ }
/// <summary>
/// Construct the Sound object to play sound data from the specified stream.
/// </summary>
public Sound(Stream stream)
{
// read the data from the stream
m_soundBytes = new byte[stream.Length];
stream.Read(m_soundBytes,
0, (int)stream.Length);
}

/// <summary>
/// Play the sound
/// </summary>
public void Play()
{
// if a file name has been registered, call WCE_PlaySound,
// otherwise call WCE_PlaySoundBytes
if (m_fileName != null)
WCE_PlaySound(m_fileName, IntPtr.Zero, (
int)(Flags.SND_ASYNC | Flags.SND_FILENAME));
else
WCE_PlaySoundBytes(m_soundBytes, IntPtr.Zero, (
int)(Flags.SND_ASYNC | Flags.SND_MEMORY));
}



public void Stop()
{
if (m_fileName != null)
WCE_PlaySound(
null, IntPtr.Zero, (int)(Flags.SND_ASYNC | Flags.SND_FILENAME));
else
WCE_PlaySoundBytes(
null, IntPtr.Zero, (int)(Flags.SND_ASYNC | Flags.SND_MEMORY));
}

}

 

 

另外,我在网上也看到了下面的方法,但是,只能在winform里面使用,我想在mobile上用改怎么办呢。

代码
/// <summary>
/// 用Win32Api实现的媒体播放类.支持Mp3,wma,wmv
/// </summary>
public 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
  );
 }

 

安①居的主页 安①居 | 初学一级 | 园豆:200
提问于:2010-04-14 12:05
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册