| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3291 人关注过本帖
标题:后台播放音乐
只看楼主 加入收藏
wangqiqi21
Rank: 1
等 级:新手上路
帖 子:48
专家分:2
注 册:2009-3-26
结帖率:90.91%
收藏
已结贴  问题点数:20 回复次数:7 
后台播放音乐
请问到一定时间怎么 自动播放音乐 音频文件在后台显示
就向闹钟一样 代码怎么写?
搜索更多相关主题的帖子: 播放 后台 音乐 
2009-08-04 09:05
jedypjd
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:蒙面侠
威 望:9
帖 子:1096
专家分:4969
注 册:2009-7-27
收藏
得分:0 
private System.Windows.Forms.Timer timercount;
        private System.Windows.Forms.Button 确定;
        public System.Windows.Forms.DateTimePicker dateTimePicker1;//输入时间

        System.Media.SoundPlayer s1 = new System.Media.SoundPlayer();//播放器
        public string AlarmDate;//定时

        public Test()
        {
            InitializeComponent();
        }
      

        private void 确定_Click(object sender, EventArgs e)
        {
            AlarmDate = this.dateTimePicker1.Text;

            CheckTask();
        }

        /// <summary>
        /// 得到时间差,并启动任务定时器 
        /// </summary>
        private void CheckTask()
        {
            int sec = task.CheckAlarm();


            if (sec > 0)
            {
                this.timercount.Interval = sec * 1000;

                this.timercount.Start();
            }
        }

        /// <summary>
        /// 计算时间差
        /// </summary>
        /// <returns></returns>
        public int CheckAlarm()
        {

            if (AlarmDate == "")
            {
                return 0;
            }


            if (Convert.ToDateTime(AlarmDate) > DateTime.Now)
            {
                System.TimeSpan extime = Convert.ToDateTime(AlarmDate) - DateTime.Now;

                return (int)extime.TotalSeconds + 1;
            }
        }

        /// <summary>
        /// 执行任务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timercount_Tick(object sender, EventArgs e)
        {
            s1.SoundLocation = "Click_015.wav";//默认路径是在exe所在文件夹
            s1.Play();
            this.timercount.Stop();
        }

不知道是不是这个

天涯无岁月,歧路有风尘,百年浑似醉,是非一片云
2009-08-04 10:02
NTYLWJ
Rank: 7Rank: 7Rank: 7
等 级:黑侠
帖 子:191
专家分:698
注 册:2008-12-2
收藏
得分:0 
以下是引用jedypjd在2009-8-4 10:02的发言:

private System.Windows.Forms.Timer timercount;
        private System.Windows.Forms.Button 确定;
        public System.Windows.Forms.DateTimePicker dateTimePicker1;//输入时间

        System.Media ...

就这样了
2009-08-04 11:43
wangqiqi21
Rank: 1
等 级:新手上路
帖 子:48
专家分:2
注 册:2009-3-26
收藏
得分:0 
关调用实时(JIT)调试而不是此对话框的详细信息,
请参见此消息的结尾。

************** 异常文本 **************
System.InvalidOperationException: 声音 API 只支持播放 PCM 波形文件
   在 System.Media.SoundPlayer.ValidateSoundFile(String fileName)
   在 System.Media.SoundPlayer.LoadAndPlay(Int32 flags)
   在 System.Media.SoundPlayer.Play()
   在 闹钟.Form1.timer1_Tick(Object sender, EventArgs e) 位置 D:\My Documents\Visual Studio 2005\Projects\闹钟\闹钟\Form1.cs:行号 33
   在 System.Windows.Forms.Timer.OnTick(EventArgs e)
   在 System.Windows.Forms.Timer.TimerNativeWindow.WndProc(Message& m)
   在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


************** 已加载的程序集 **************
mscorlib
    程序集版本: 2.0.0.0
    Win32 版本: 2.0.50727.1433 (REDBITS.050727-1400)
    基本代码: file:///C:/WINDOWS/
----------------------------------------
闹钟
    程序集版本: 1.0.0.0
    Win32 版本: 1.0.0.0
    基本代码: file:///D:/My%20Documents/Visual%20Studio%202005/Projects/闹钟/闹钟/bin/Debug/闹钟.exe
----------------------------------------
System.Windows.Forms
    程序集版本: 2.0.0.0
    Win32 版本: 2.0.50727.1433 (REDBITS.050727-1400)
    基本代码: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System
    程序集版本: 2.0.0.0
    Win32 版本: 2.0.50727.1433 (REDBITS.050727-1400)
    基本代码: file:///C:/WINDOWS/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Drawing
    程序集版本: 2.0.0.0
    Win32 版本: 2.0.50727.1433 (REDBITS.050727-1400)
    基本代码: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System.resources
    程序集版本: 2.0.0.0
    Win32 版本: 2.0.50727.1433 (REDBITS.050727-1400)
    基本代码: file:///C:/WINDOWS/assembly/GAC_MSIL/System.resources/2.0.0.0_zh-CHS_b77a5c561934e089/System.resources.dll
----------------------------------------
System.Xml
    程序集版本: 2.0.0.0
    Win32 版本: 2.0.50727.1433 (REDBITS.050727-1400)
    基本代码: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Xml/2.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------
mscorlib.resources
    程序集版本: 2.0.0.0
    Win32 版本: 2.0.50727.1433 (REDBITS.050727-1400)
    基本代码: file:///C:/WINDOWS/
----------------------------------------
System.Configuration
    程序集版本: 2.0.0.0
    Win32 版本: 2.0.50727.1433 (REDBITS.050727-1400)
    基本代码: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Configuration/2.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------
System.Windows.Forms.resources
    程序集版本: 2.0.0.0
    Win32 版本: 2.0.50727.1433 (REDBITS.050727-1400)
    基本代码: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Windows.Forms.resources/2.0.0.0_zh-CHS_b77a5c561934e089/System.Windows.Forms.resources.dll
----------------------------------------

************** JIT 调试 **************
要启用实时(JIT)调试,
该应用程序或计算机的 .config 文件(machine.config)的 system.windows.forms 节中必须设置
jitDebugging 值。
编译应用程序时还必须启用
调试。

例如:

<configuration>
    <system.windows.forms jitDebugging="true" />
</configuration>

启用 JIT 调试后,任何无法处理的异常
都将被发送到在此计算机上注册的 JIT 调试器,
而不是由此对话框处理。


.....这是什么问题?

[[it] 本帖最后由 wangqiqi21 于 2009-8-4 12:02 编辑 [/it]]
2009-08-04 11:54
wangqiqi21
Rank: 1
等 级:新手上路
帖 子:48
专家分:2
注 册:2009-3-26
收藏
得分:0 
还有 谁知道鼠标移过图片会自动发出声音 也是这个soundplay吗?
具体怎么使用啊
2009-08-04 11:56
jedypjd
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:蒙面侠
威 望:9
帖 子:1096
专家分:4969
注 册:2009-7-27
收藏
得分:20 
C#中的System.Media.SoundPlayer只能播放波形文件.wav
播放mp3,wma等要用到APIClass.mciSendString()
namespace bofang
{
    /**//// <summary>
    /// 用Win32Api实现的媒体播放类.支持Mp3,wma,wmv
    /// </summary>
    class Mp3
    {
        public Mp3()
        {
        }
        //定义API函数使用的字符串变量
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
        private string Name = "";
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
        private string durLength = "";
        [MarshalAs(UnmanagedType.LPTStr, SizeConst = 128)]
        private string TemStr = "";
        int ilong;
        //定义播放状态枚举变量
        public enum State
        {
            mPlaying = 1,
            mPuase = 2,
            mStop = 3
        };
        //结构变量
        public struct structMCI
        {
            public bool bMut;
            public int iDur;
            public int iPos;
            public int iVol;
            public int iBal;
            public string iName;
            public State state;
        };

        public structMCI mc = new structMCI();

        //取得播放文件属性
        public string FileName
        {
            get
            {
                return mc.iName;
            }
            set
            {
                //ASCIIEncoding asc = new ASCIIEncoding();
                try
                {
                    TemStr = "";
                    TemStr = TemStr.PadLeft(127, Convert.ToChar(" "));
                    Name = Name.PadLeft(260, Convert.ToChar(" "));
                    mc.iName = value;
                    ilong = APIClass.GetShortPathName(mc.iName, Name, Name.Length);
                    Name = GetCurrPath(Name);
                    //Name = "open " + Convert.ToChar(34) + Name + Convert.ToChar(34) + " alias media";
                    Name = "open " + Convert.ToChar(34) + Name + Convert.ToChar(34) + " alias media";
                    ilong = APIClass.mciSendString("close all", TemStr, TemStr.Length, 0);
                    ilong = APIClass.mciSendString(Name, TemStr, TemStr.Length, 0);
                    ilong = APIClass.mciSendString("set media time format milliseconds", TemStr, TemStr.Length, 0);
                    mc.state = State.mStop;
                }
                catch
                {
                    MessageBox.Show("出错错误!");
                }
            }
        }
        //播放
        public void play()
        {
            TemStr = "";
            TemStr = TemStr.PadLeft(127, Convert.ToChar(" "));
            APIClass.mciSendString("play media", TemStr, TemStr.Length, 0);
            mc.state = State.mPlaying;
        }
        //停止
        public void StopT()
        {
            TemStr = "";
            TemStr = TemStr.PadLeft(128, Convert.ToChar(" "));
            ilong = APIClass.mciSendString("close media", TemStr, 128, 0);
            ilong = APIClass.mciSendString("close all", TemStr, 128, 0);
            mc.state = State.mStop;
        }

        public void Puase()
        {
            TemStr = "";
            TemStr = TemStr.PadLeft(128, Convert.ToChar(" "));
            ilong = APIClass.mciSendString("pause media", TemStr, TemStr.Length, 0);
            mc.state = State.mPuase;
        }
        private string GetCurrPath(string name)
        {
            if (name.Length < 1) return "";
            name = name.Trim();
            name = name.Substring(0, name.Length - 1);
            return name;
        }
        //总时间
        public int Duration
        {
            get
            {
                durLength = "";
                durLength = durLength.PadLeft(128, Convert.ToChar(" "));
                APIClass.mciSendString("status media length", durLength, durLength.Length, 0);
                durLength = durLength.Trim();
                if (durLength == "") return 0;
                return (int)(Convert.ToDouble(durLength) / 1000f);
            }
        }

        //当前时间
        public int CurrentPosition
        {
            get
            {
                durLength = "";
                durLength = durLength.PadLeft(128, Convert.ToChar(" "));
                APIClass.mciSendString("status media position", durLength, durLength.Length, 0);
                mc.iPos = (int)(Convert.ToDouble(durLength) / 1000f);
                return mc.iPos;
            }
        }
    }
}
 
 public class APIClass
 {
  [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
  public static extern int GetShortPathName (
   string lpszLongPath,
   string shortFile,
   int cchBuffer
  );

  [DllImport("winmm.dll", EntryPoint="mciSendString", CharSet = CharSet.Auto)]
  public static extern int mciSendString (
   string lpstrCommand,
   string lpstrReturnString,
   int uReturnLength,
   int hwndCallback
  );
}

天涯无岁月,歧路有风尘,百年浑似醉,是非一片云
2009-08-04 12:39
jedypjd
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:蒙面侠
威 望:9
帖 子:1096
专家分:4969
注 册:2009-7-27
收藏
得分:0 
道鼠标移过图片会自动发出声音,
编写图片控件的mousemove事件即可

天涯无岁月,歧路有风尘,百年浑似醉,是非一片云
2009-08-04 12:41
wangqiqi21
Rank: 1
等 级:新手上路
帖 子:48
专家分:2
注 册:2009-3-26
收藏
得分:0 
谢谢
2009-08-04 14:04
快速回复:后台播放音乐
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.029211 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved