zoukankan      html  css  js  c++  java
  • C# Window Form播放音乐的4种方式

    C#播放背景音乐通常有四种方式:

      1.播放系统事件声音

      2.使用System.Media.SoundPlayer播放wav------------------------仅仅是对波形音乐

      3.使用MCI Command String多媒体设备程序接口播放mp3,avi等

      4.使用axWindowsMediaPlayer的COM组件来播放

    1.播放系统事件声音 

    System.Media.SystemSounds.Asterisk.Play(); System.Media.SystemSounds.Beep.Play(); 
    System.Media.SystemSounds.Exclamation.Play(); System.Media.SystemSounds.Hand.Play(); 
    System.Media.SystemSounds.Question.Play();

    2.使用System.Media.SoundPlayer播放wav

    System.Media.SoundPlayer sp = new SoundPlayer(); 
    sp.SoundLocation = @"D:/10sec.wav"; 
    sp.PlayLooping();

    3.使用MCI Command String多媒体设备程序接口播放mp3,avi等

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Runtime.InteropServices;
    
    namespace MyWenxinTool
    {
        public class musicplay
        {
    
            public static uint SND_ASYNC = 0x0001;
            public static uint SND_FILENAME = 0x00020000;
            [DllImport("winmm.dll")]
            public static extern uint mciSendString(string lpstrCommand, string lpstrReturnString, uint uReturnLength, uint hWndCallback);
    
            public static void PlayNmusinc(string path)
            {
                mciSendString(@"close temp_alias", null, 0, 0);
                mciSendString(@"open """+path+@""" alias temp_alias", null, 0, 0);
                mciSendString("play temp_alias repeat", null, 0, 0);
            }
    
            /// <summary>
            /// 播放音乐文件(重复)
            /// </summary>
            /// <param name="p_FileName">音乐文件名称</param>
            public static void PlayMusic_Repeat(string p_FileName)
            {
                try
                {
                    mciSendString(@"close temp_music", " ", 0, 0);
                    mciSendString(@"open " + p_FileName + " alias temp_music", " ", 0, 0);
                    mciSendString(@"play temp_music repeat", " ", 0, 0);
                }
                catch
                { }
            }
    
            /// <summary>
            /// 播放音乐文件
            /// </summary>
            /// <param name="p_FileName">音乐文件名称</param>
            public static void PlayMusic(string p_FileName)
            {
                try
                {
                    mciSendString(@"close temp_music", " ", 0, 0);
                    //mciSendString(@"open " + p_FileName + " alias temp_music", " ", 0, 0);
                    mciSendString(@"open """ + p_FileName + @""" alias temp_music", null, 0, 0);
                    mciSendString(@"play temp_music", " ", 0, 0);
                }
                catch
                { }
            }
    
            /// <summary>
            /// 停止当前音乐播放
            /// </summary>
            /// <param name="p_FileName">音乐文件名称</param>
            public static void StopMusic(string p_FileName)
            {
                try
                {
                    mciSendString(@"close " + p_FileName, " ", 0, 0);
                }
                catch { }
            }
    
        }
    
    }

    关于mciSendString的详细参数说明,请参见MSDN,或是 http://blog.csdn.net/psongchao/archive/2007/01/19/1487788.aspx

    4.使用axWindowsMediaPlayer的COM组件来播放

      a.加载COM组件:添加引用-->Com组件--> Windows Media Player如下图:

      b.把Windows Media Player控件拖放到Winform窗体中,把axWindowsMediaPlayer1中URL属性设置为MP3或是AVI的文件路径,F5运行。

    如何使用Windows Media Player循环播放列表中的媒体文件?

    假设我们有一个播放列表,下面的代码可以实现自动循环播放

    MediaPlayer.MediaPlayer mr = new MediaPlayer.MediaPlayer();
    mr.FileName = txpath.Text.Trim();
    mr.Play();

    MCI Command String和Windows Media Player都有非常丰富的功能接口,这里不能一一介绍,可以参考MSDN中的具体描述.

  • 相关阅读:
    理解 QEMU/KVM 和 Ceph(2):QEMU 的 RBD 块驱动(block driver)
    Neutron VxLAN + Linux Bridge 环境中的网络 MTU
    理解 QEMU/KVM 和 Ceph(1):QEMU-KVM 和 Ceph RBD 的 缓存机制总结
    [译] 企业级 OpenStack 的六大需求(第 3 部分):弹性架构、全球交付
    [译] 企业级 OpenStack 的六大需求(第 2 部分):开放架构和混合云兼容
    [译] 企业级 OpenStack 的六大需求(第 1 部分):API 高可用、管理和安全
    Javascript中的Array(数组) 、{}(映射) 与JSON解析
    HTML中显示特殊字符,如尖括号 “<”,">"等等
    Ubuntu 12.04 安装配置 Apache2
    Python中函数的参数传递与可变长参数
  • 原文地址:https://www.cnblogs.com/xibei666/p/4586767.html
Copyright © 2011-2022 走看看