更新 : 2007 年 11 月
次のサンプル コードは、モバイル デバイスでプラットフォーム呼び出し (PInvoke) を使用して wave サウンド ファイルを再生する方法を示しています。
使用例
このサンプル コードでは、モバイル デバイスで PlaySound を使用してサウンド ファイルを再生します。このコードでは、System.Runtime.InteropServices を使用して、Compact Framework の CoreDll.DLL の PlaySound メソッドを呼び出します。
using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
namespace MobileSoundPInvoke
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.MainMenu mainMenu1;
public Form1()
{
InitializeComponent();
PlaySound(".\\sound.wav");
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.Menu = this.mainMenu1;
this.Text = "Form1";
}
#endregion
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
}
static void Main()
{
Application.Run(new Form1());
}
private enum Flags
{
SND_SYNC = 0x0000,
SND_ASYNC = 0x0001,
SND_NODEFAULT = 0x0002,
SND_MEMORY = 0x0004,
SND_LOOP = 0x0008,
SND_NOSTOP = 0x0010,
SND_NOWAIT = 0x00002000,
SND_ALIAS = 0x00010000,
SND_ALIAS_ID = 0x00110000,
SND_FILENAME = 0x00020000,
SND_RESOURCE = 0x00040004
}
[DllImport("CoreDll.DLL", EntryPoint = "PlaySound", SetLastError = true)]
private extern static int MobilePlaySound(string szSound, IntPtr hMod, int flags);
public void PlaySound(string fileName)
{
MobilePlaySound(fileName, IntPtr.Zero, (int)(Flags.SND_ASYNC | Flags.SND_FILENAME));
}
}
}
コードのコンパイル方法
新しい C# Smartphone アプリケーション プロジェクトを Visual Studio で作成し、MobileSoundPInvoke という名前を付けます。
上記のサンプルのコードをコピーし、コンソール アプリケーションの MobileSoundPInvoke プロジェクトの Form1.cs ファイルに貼り付けます。
堅牢性の高いプログラム
- 適切なパラメータを C の MobilePlaySound (string szSound, IntPtr hMod, int flags) 関数に渡します。このとき、.wav ファイルのパスとファイル名を含めます。
セキュリティ
セキュリティの詳細については、「patterns & practices セキュリティ ガイドライン インデックス」を参照してください。