| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1246 人关注过本帖
标题:[求助]奇怪,这个为什么要点两次才能播放?
取消只看楼主 加入收藏
csharp
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2005-7-22
收藏
 问题点数:0 回复次数:4 
[求助]奇怪,这个为什么要点两次才能播放?
选定音乐后,得点两次“播放”才能播放出声音,单步运行第一次ThreadState已经是running了,却听不到声音,再点一次就行了,不解啊!!WHY?? 这个是源程序:
teifX1w8.rar (8.84 KB) [求助]奇怪,这个为什么要点两次才能播放?



tvmwImaK.jpg (8.28 KB)
图片附件: 游客没有浏览图片的权限,请 登录注册
搜索更多相关主题的帖子: 要点 播放 音乐 running 
2005-08-15 02:49
csharp
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2005-7-22
收藏
得分:0 

这么难吗?唉 我把代码贴出来吧,直观点…… using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Runtime.InteropServices; using System.Text; using System.Threading;

namespace PlayMusic { /// <summary> /// Form1 的摘要说明。 /// </summary> public class Form1 : System.Windows.Forms.Form { private string filePath = null; private Audio audio = new Audio(); private Thread music = null; private System.Windows.Forms.Button button1; private System.Windows.Forms.Button button2; private System.Windows.Forms.Button button3; private System.Windows.Forms.Button button4; private System.Windows.Forms.Button button5; private System.Windows.Forms.OpenFileDialog openFileDialog1; private System.Windows.Forms.StatusBar statusBar1; private System.Windows.Forms.StatusBarPanel SBP_FN; /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.Container components = null;

public Form1() { // // Windows 窗体设计器支持所必需的 // InitializeComponent();

// // TOD 在 InitializeComponent 调用后添加任何构造函数代码 // }

/// <summary> /// 清理所有正在使用的资源。 /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); }

#region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { this.button1 = new System.Windows.Forms.Button(); this.button2 = new System.Windows.Forms.Button(); this.button3 = new System.Windows.Forms.Button(); this.button4 = new System.Windows.Forms.Button(); this.button5 = new System.Windows.Forms.Button(); this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog(); this.statusBar1 = new System.Windows.Forms.StatusBar(); this.SBP_FN = new System.Windows.Forms.StatusBarPanel(); ((System.ComponentModel.ISupportInitialize)(this.SBP_FN)).BeginInit(); this.SuspendLayout(); // // button1 // this.button1.FlatStyle = System.Windows.Forms.FlatStyle.System; this.button1.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134))); this.button1.Location = new System.Drawing.Point(30, 16); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(100, 32); this.button1.TabIndex = 0; this.button1.Text = "打开"; this.button1.Click += new System.EventHandler(this.button1_Click); // // button2 // this.button2.FlatStyle = System.Windows.Forms.FlatStyle.System; this.button2.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134))); this.button2.Location = new System.Drawing.Point(30, 56); this.button2.Name = "button2"; this.button2.Size = new System.Drawing.Size(100, 32); this.button2.TabIndex = 1; this.button2.Text = "播放"; this.button2.Click += new System.EventHandler(this.button2_Click); // // button3 // this.button3.FlatStyle = System.Windows.Forms.FlatStyle.System; this.button3.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134))); this.button3.Location = new System.Drawing.Point(30, 96); this.button3.Name = "button3"; this.button3.Size = new System.Drawing.Size(100, 32); this.button3.TabIndex = 2; this.button3.Text = "暂停"; // // button4 // this.button4.FlatStyle = System.Windows.Forms.FlatStyle.System; this.button4.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134))); this.button4.Location = new System.Drawing.Point(30, 136); this.button4.Name = "button4"; this.button4.Size = new System.Drawing.Size(100, 32); this.button4.TabIndex = 3; this.button4.Text = "停止"; // // button5 // this.button5.FlatStyle = System.Windows.Forms.FlatStyle.System; this.button5.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134))); this.button5.Location = new System.Drawing.Point(30, 176); this.button5.Name = "button5"; this.button5.Size = new System.Drawing.Size(100, 32); this.button5.TabIndex = 4; this.button5.Text = "退出"; this.button5.Click += new System.EventHandler(this.button5_Click); // // openFileDialog1 // this.openFileDialog1.Filter = "wma文件 | *.wma"; this.openFileDialog1.Title = "打开文件..."; // // statusBar1 // this.statusBar1.Location = new System.Drawing.Point(0, 228); this.statusBar1.Name = "statusBar1"; this.statusBar1.Panels.AddRange(new System.Windows.Forms.StatusBarPanel[] { this.SBP_FN}); this.statusBar1.ShowPanels = true; this.statusBar1.Size = new System.Drawing.Size(152, 22); this.statusBar1.TabIndex = 5; this.statusBar1.Text = "statusBar1"; // // SBP_FN // this.SBP_FN.AutoSize = System.Windows.Forms.StatusBarPanelAutoSize.Spring; this.SBP_FN.Width = 136; // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(152, 250); this.Controls.Add(this.statusBar1); this.Controls.Add(this.button5); this.Controls.Add(this.button4); this.Controls.Add(this.button3); this.Controls.Add(this.button2); this.Controls.Add(this.button1); this.MinimumSize = new System.Drawing.Size(160, 280); this.Name = "Form1"; this.Text = "Form1"; this.Resize += new System.EventHandler(this.Centralization); ((System.ComponentModel.ISupportInitialize)(this.SBP_FN)).EndInit(); this.ResumeLayout(false);

} #endregion

/// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.Run(new Form1()); }

private void button5_Click(object sender, System.EventArgs e) { Application.Exit(); }

private void Centralization(object sender, System.EventArgs e) { this.Height = 280; button1.Left = button2.Left = button3.Left = button4.Left = button5.Left = this.Width/2 - this.button1.Width/2; }

private void play() { audio.Play(filePath); }

private void button1_Click(object sender, System.EventArgs e) { if(this.openFileDialog1.ShowDialog() == DialogResult.OK) { filePath = this.openFileDialog1.FileName; }

SBP_FN.Text = filePath.Substring(filePath.LastIndexOf("\\")+1, filePath.Length - filePath.LastIndexOf("\\") - 1); }

public void ThreadPlay() { music = new Thread(new ThreadStart(this.play)); music.Start();; }

private void button2_Click(object sender, System.EventArgs e) { ThreadPlay(); }

#region class Audio public class Audio { [DllImport("winmm.dll")] private static extern int mciSendString( string lpstrCommand, string lpstrReturnString, int uReturnLength, int hwndCallback ); [DllImport("kernel32.dll", CharSet = CharSet.Auto)] public static extern int GetShortPathName( string path, StringBuilder shortPath, int shortPathLength );

public void Play(string FileName) { StringBuilder shortPathTemp = new StringBuilder(255); int result = GetShortPathName(FileName, shortPathTemp, shortPathTemp.Capacity); string ShortPath = shortPathTemp.ToString();

mciSendString("open "+ShortPath+" alias song","",0,0); mciSendString("play song","",0,0); }

public void Stop() { mciSendString("stop song","",0,0); }

public void Pause() { mciSendString("pause song","",0,0); }

public void Close() { mciSendString("close song","",0,0); } } #endregion } }

[此贴子已经被作者于2005-8-15 18:53:19编辑过]


喝杯汽酒,交个朋友
2005-08-15 15:36
csharp
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2005-7-22
收藏
得分:0 
以下是引用yichen在2005-8-16 10:55:28的发言: 楼主 ,你看看我写的行不行! public void ThreadPlay() { music = new Thread(new ThreadStart(this.play)); music.Start();; } 上面的两句代码可以 删除阿 可以用 这一句实现: this.play();
我知道啊,我是特意要用线程写的,直接写成audio.Play(filePath)也行 还有上一层楼的也是,不过也要谢谢你 还是不行……

[此贴子已经被作者于2005-8-16 14:17:42编辑过]


喝杯汽酒,交个朋友
2005-08-16 14:10
csharp
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2005-7-22
收藏
得分:0 
以下是引用live41在2005-8-24 8:36:48的发言: thread超越了参数的,所以第一次没有参数,第二次用的参数是第一次传。
这句……是什么意思………… 还有,new完就suspend会出错,说“线程还没有启动”之类…… 在打开按钮里new完,然后start,再suspend,然后在播放按钮中resume还是得两次……郁闷

喝杯汽酒,交个朋友
2005-08-26 23:55
csharp
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2005-7-22
收藏
得分:0 
天啊——————
这回更奇怪了,按这个顺序执行后:
music = new …………) //省略
music.Start();
music.Suspend();
运行到这会提示:线程尚未运行……

实在不行只好不用它了,那点分先分了吧。偶新手分不多,各位见谅

喝杯汽酒,交个朋友
2005-08-27 11:00
快速回复:[求助]奇怪,这个为什么要点两次才能播放?
数据加载中...
 
   



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

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