| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1246 人关注过本帖
标题:[求助]奇怪,这个为什么要点两次才能播放?
只看楼主 加入收藏
csharp
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2005-7-22
收藏
 问题点数:0 回复次数:13 
[求助]奇怪,这个为什么要点两次才能播放?
选定音乐后,得点两次“播放”才能播放出声音,单步运行第一次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
zgy0209zgy
Rank: 1
等 级:新手上路
帖 子:71
专家分:0
注 册:2005-8-15
收藏
得分:0 
我也是搞不明白,上面的好象也的用两次才能播放????????
2005-08-16 09:29
yichen
Rank: 1
等 级:新手上路
帖 子:303
专家分:0
注 册:2005-3-9
收藏
得分:0 
楼主 ,你看看我写的行不行! public void ThreadPlay() { music = new Thread(new ThreadStart(this.play)); music.Start();; } 上面的两句代码可以 删除阿 可以用 这一句实现: this.play();

衣带渐宽终不悔, 为伊消得人憔悴。 纸上得来终觉浅, 绝知此事要躬行。
2005-08-16 10:55
yichen
Rank: 1
等 级:新手上路
帖 子:303
专家分:0
注 册:2005-3-9
收藏
得分: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 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 = "暂停"; this.button3.Click += new System.EventHandler(this.button3_Click); // // 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 = "停止"; this.button4.Click += new System.EventHandler(this.button4_Click); // // 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, 231); 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.BackColor = System.Drawing.Color.OliveDrab; this.ClientSize = new System.Drawing.Size(152, 253); this.ControlBox = false; 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.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; this.MinimumSize = new System.Drawing.Size(160, 280); this.Name = "Form1"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "播放器"; 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); } public void pause() { this.audio.Pause(); } public void stop() { this.audio.Stop(); } public void close() { this.audio.Close(); }

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); }

private void button2_Click(object sender, System.EventArgs e) { if(this.SBP_FN.Text!=null) { this.audio.Close(); filePath = this.openFileDialog1.FileName; this.play(); } else { this.play(); } }

private void button3_Click(object sender, System.EventArgs e) { this.pause(); }

private void button4_Click(object sender, System.EventArgs e) { this.stop(); }

#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 ( [MarshalAs(UnmanagedType.LPTStr)] string path,   [MarshalAs(UnmanagedType.LPTStr)] 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-08-16 10:57
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
yichen
Rank: 1
等 级:新手上路
帖 子:303
专家分:0
注 册:2005-3-9
收藏
得分:0 
不好意思啊
那我理解错误了,我在看看!

衣带渐宽终不悔, 为伊消得人憔悴。 纸上得来终觉浅, 绝知此事要躬行。
2005-08-16 16:18
live41
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:67
帖 子:12442
专家分:0
注 册:2004-7-22
收藏
得分:0 
顶上去。。。
2005-08-24 07:27
live41
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:67
帖 子:12442
专家分:0
注 册:2004-7-22
收藏
得分:0 
我估计是,thread的运行顺序的问题。

thread超越了参数的,所以第一次没有参数,第二次用的参数是第一次传。

我也不知道具体,记得线程的运行顺序很难理解。
2005-08-24 08:36
live41
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:67
帖 子:12442
专家分:0
注 册:2004-7-22
收藏
得分:0 
你试试在“打开”按钮加入一段代码,先new线程music,然后suspend,在执行完原来打开按钮的代码后再start线程。唯有这样反复调试得到线程运行的顺序。
2005-08-24 08:39
快速回复:[求助]奇怪,这个为什么要点两次才能播放?
数据加载中...
 
   



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

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