none
C#2.0开发VS2005上的Pocket PC 2003游戏, 播放背景音乐怎么不行啊? RRS feed

  • 问题

  • 我要实现的功能是在玩游戏时可以切换背景音乐。我先把三个音乐文件Music1.wav,Music2.wav,Music3.wav拖到资源文件了去。然后就敲下面的代码。

    namespace PlayMusic
    {
        class APIPlayer
        {
            private String File_Path;
            private Byte[] Res_Name;
            private enum PalyFlags
            {
                SND_SYNC = 0x0000,                      /* play synchronously (default) */
                SND_ASYNC = 0x0001,                     /* play asynchronously */
                SND_NODEFAULT = 0x0002,                 /* silence (!default) if sound not found */
                SND_MEMORY = 0x0004,                    /* pszSound points to a memory file */
                SND_LOOP = 0x0008,                      /* loop the sound until next sndPlaySound */
                SND_NOSTOP = 0x0010,                    /* don't stop any currently playing sound */
                SND_NOWAIT = 0x00002000,                /* don't wait if the driver is busy */
                SND_ALIAS = 0x00010000,                 /* name is a registry alias */
                SND_ALIAS_ID = 0x00110000,              /* alias is a predefined ID */
                SND_FILENAME = 0x00020000,              /* name is file name */
                SND_RESOURCE = 0x00040004               /* name is resource name or atom */
            }
    
            [DllImport("Coredll.dll")]
            private static extern int PlaySound(String lpszName, IntPtr hMod, int dwFlag);
    
            [DllImport("Coredll.dll")]
            private static extern int PlaySound(Byte[] szSound, IntPtr hMod, int dwFlag);
    
            public APIPlayer(string strFilename)
            {
                File_Path = strFilename;
            }
    
            public APIPlayer(Stream streamRes)
            {           
                Res_Name = new Byte[streamRes.Length];
                streamRes.Read(Res_Name, 0, (int)streamRes.Length);
            }
    
            public void Play()                                          // 播放音乐
            {
                if (!String.IsNullOrEmpty(File_Path))
                {
                    ClearLastInfo();
                    PlaySound(File_Path, IntPtr.Zero, (int)(PalyFlags.SND_ASYNC | PalyFlags.SND_FILENAME));                
                }
                else if (Res_Name != null)
                {
                    ClearLastInfo();
                    PlaySound(Res_Name, IntPtr.Zero, (int)(PalyFlags.SND_ASYNC | PalyFlags.SND_MEMORY));
                }
                else
                {
                }
            }
    
            public void Stop()                                        // 关闭音乐
            {
                ClearLastInfo();
                PlaySound(String.Empty, IntPtr.Zero, (int)(PalyFlags.SND_ASYNC | PalyFlags.SND_FILENAME)); // 找不到文件名就会播放静音了
            }
    
            private void ClearLastInfo()
            {
                File_Path = String.Empty;
                Res_Name = null;
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Reflection;
    using PlayMusic.Properties;
    
    namespace PlayMusic
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            APIPlayer myAPIPlayer;
            private void MI_Music1_Click(object sender, EventArgs e)   // 播放资源文件里的Music1.wav
            {          
                myAPIPlayer = new  APIPlayer(Resource.sMusic1);
                myAPIPlayer.Play(); // 怎么把资源转化成比特流啊?
            }
    
            private void MI_Music2_Click(object sender, EventArgs e)  // 播放资源文件里的Music2.wav
            {
                myAPIPlayer = new  APIPlayer(Resources.Music2);
                myAPIPlayer.Play(); // 怎么把资源转化成比特流啊?
            }
    
            private void MI_Music3_Click(object sender, EventArgs e)  // 播放资源文件里的Music3.wav
            {
                 myAPIPlayer = new  APIPlayer(Resources.Music3);
                 myAPIPlayer.Play(); // 怎么把资源转化成比特流啊?
            }
    
            private void MI_MusicSelfD_Click(object sender, EventArgs e) // 播放用户打开的某个文件夹下的音乐
            {
                OpenFileDialog myOpenFileDialog = new OpenFileDialog();
                myOpenFileDialog.Filter = "wma files|*.wma|wav files|*.wav|mp3 files|*.mp3";
                myOpenFileDialog.InitialDirectory = "\\Storage Card"; 
                if (myOpenFileDialog.ShowDialog() == DialogResult.OK)
                {
                    myAPIPlayer = new APIPlayer(myOpenFileDialog.FileName); // 只有一声"嘟" 任何事件都能灭掉它
                    myAPIPlayer.Play();
                }
            }
    
            private void MI_MusicClose_Click(object sender, EventArgs e) // 关闭音乐(静音)
            {
                try
                {
                    myAPIPlayer.Stop();
                }
                catch
                {
                    // 假使菜单"灰化"功能没做好,还没开始就结束会有异常的
                }
            }
        }
    }
    问题,就是我这个方法不行(见注释),谁能帮帮我吗?
    拉比"忘"
    2009年6月4日 0:19

全部回复