Como: executar um som wave arquivo usando invocação de plataforma (dispositivos)

O exemplo de código a seguir ilustra como usar invocação de plataforma (PInvoke) para executar um arquivo de som wave em um dispositivo móvel.

Exemplo

Este código de exemplo reproduz um arquivo de som usando PlaySound no dispositivo móvel. Esse código usa System.Runtime.InteropServices para chamar a PlaySound método CoreDll.DLL do Compact estrutura.

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));
        }

    }
}

Compilando o código

  • Criar um novo projeto translation from VPE for Csharp Smartphone aplicativo em Visual Studio e chame-MobileSoundPInvoke.

  • Copie o código do exemplo anterior e colá-lo sobre o arquivo Form1.cs do seu projeto MobileSoundPInvoke em um aplicativo de console.

Programação robusta

  • Make sure the appropriate parameters are passed to the CMobilePlaySound (string szSound, IntPtr hMod, int flags) function, including the path and file name for the .wav file.

Segurança

Para obter mais informações sobre segurança, consulte Segurança do .NET estrutura.

Consulte também

Tarefas

Invocação de plataforma Tecnologia Exemplo

Conceitos

Exemplos dispositivo inteligente

Como fazer no dispositivo inteligente Development

Outros recursos

Usando PInvoke Explicit no C++ (atributo DllImport)