Procedura: riprodurre un file audio mediante richiamo piattaforma (dispositivi)

Aggiornamento: novembre 2007

Nell'esempio di codice riportato di seguito viene illustrato come utilizzare il richiamo piattaforma (PInvoke) per riprodurre un file audio wave in un dispositivo mobile.

Esempio

In questo codice di esempio viene riprodotto un file audio utilizzando PlaySound nel dispositivo mobile. Nel codice viene utilizzato System.Runtime.InteropServices per richiamare il metodo PlaySound del file CoreDll.DLL di .NET Compact Framework.

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

    }
}

Compilazione del codice

  • Creare un nuovo progetto di applicazione Smartphone C# in Visual Studio e denominarlo MobileSoundPInvoke.

  • Copiare il codice dall'esempio precedente e incollarlo sul file Form1.cs del progetto MobileSoundPInvoke in un'applicazione console.

Programmazione efficiente

  • Accertarsi che alla funzione CMobilePlaySound (string szSound, IntPtr hMod, int flags) vengano passati i parametri corretti, tra cui il percorso e il nome del file WAV.

Sicurezza

Per ulteriori informazioni sulla sicurezza,vedere .NET Framework Security (informazioni in lingua inglese).

Vedere anche

Attività

Esempio di tecnologia di richiamo piattaforma

Concetti

Esempi relativi ai dispositivi Smart Device

Procedure relative allo sviluppo per Smart Device

Altre risorse

Utilizzo esplicito di PInvoke in C++ (attributo DllImport)