⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 media.cs

📁 清华大学出版社出版的 移动应用开发宝典 张大威(2008)的附书源代码
💻 CS
字号:
using System;
using System.ComponentModel;
using System.IO;
using System.Runtime.InteropServices;

namespace Chapter14.Media
{
    public sealed class SoundPlayer : Component
    {
        //path to the file
        private string soundLocation = "";

        public SoundPlayer() { }

        public SoundPlayer(string soundLocation)
        {
            //set the path
            this.soundLocation = soundLocation;
        }

        public string SoundLocation
        {
            get
            {
                return soundLocation;
            }
            set
            {
                if (File.Exists(value))
                {
                    soundLocation = value;
                }
                OnSoundLocationChanged(EventArgs.Empty);
            }
        }

        public event EventHandler SoundLocationChanged;

        private void OnSoundLocationChanged(EventArgs e)
        {
            if (this.SoundLocationChanged != null)
            {
                this.SoundLocationChanged(this, e);
            }
        }

        public void Play()
        {
            //play async
            NativeMethods.PlaySound(soundLocation, IntPtr.Zero, NativeMethods.SND.FILENAME | NativeMethods.SND.ASYNC);
        }

        public void PlaySync()
        {
            //play sync
            NativeMethods.PlaySound(soundLocation, IntPtr.Zero, NativeMethods.SND.FILENAME | NativeMethods.SND.SYNC);
        }

        public void PlayLooping()
        {
            //play looping
            NativeMethods.PlaySound(soundLocation, IntPtr.Zero, NativeMethods.SND.FILENAME | NativeMethods.SND.ASYNC | NativeMethods.SND.LOOP);
        }

        public void Stop()
        {
            NativeMethods.PlaySound(null, IntPtr.Zero, NativeMethods.SND.NODEFAULT);
        }
    }

    public static class SystemSounds
    {
        public static SystemSound Beep
        {
            get
            {
                return new SystemSound(0);
            }
        }

        public static SystemSound Asterisk
        {
            get
            {
                return new SystemSound(NativeMethods.MB.ICONASTERISK);
            }
        }

        public static SystemSound Exclamation
        {
            get
            {
                return new SystemSound(NativeMethods.MB.ICONEXCLAMATION);
            }
        }

        public static SystemSound Question
        {
            get
            {
                return new SystemSound(NativeMethods.MB.ICONQUESTION);
            }
        }

        public static SystemSound Hand
        {
            get
            {
                return new SystemSound(NativeMethods.MB.ICONHAND);
            }
        }
    }

    public sealed class SystemSound
    {
        //type of sound
        private NativeMethods.MB soundType;

        internal SystemSound(NativeMethods.MB soundType)
        {
            //set type
            this.soundType = soundType;
        }

        public void Play()
        {
            //play
            NativeMethods.MessageBeep(soundType);
        }

    }

    internal static class NativeMethods
    {
        [DllImport("coredll.dll")]
        internal static extern void MessageBeep(MB type);

        internal enum MB
        {
            ICONHAND = 0x00000010,
            ICONQUESTION  = 0x00000020,
            ICONEXCLAMATION = 0x00000030,
            ICONASTERISK = 0x00000040,
        }

        [DllImport("coredll.dll", EntryPoint = "PlaySoundW")]
        [return:MarshalAs(UnmanagedType.Bool)]
        internal static extern bool PlaySound(string lpszName, IntPtr hModule, SND dwFlags);

        [Flags()]
        internal enum SND
        {
            //ALIAS = 0x00010000,
            FILENAME = 0x00020000,
            //RESOURCE = 0x00040004,
            SYNC = 0x00000000,
            ASYNC = 0x00000001,
            NODEFAULT = 0x00000002,
            //MEMORY = 0x00000004,
            LOOP = 0x00000008,
            NOSTOP = 0x00000010,
            NOWAIT = 0x00002000
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -