下载mp3显示进度条以及非常简单的directx播放音乐

ContractedBlock.gif ExpandedBlockStart.gif View Code
 
      
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Threading;
using Microsoft.DirectX;
using Microsoft.DirectX.AudioVideoPlayback;
using Microsoft.DirectX.DirectSound;
using Shell32;

namespace ProgressBarExample
{
public partial class DownloadMp3Progress : Form
{
#region 定义全局变量
public string filepath = @" C:\Downloads\1.mp3 " ;
public static Audio b;
#endregion

#region 定义正在播放事件,作用是获取当前播放进度值给trackbar显示
public class Evtargs : EventArgs
{
public int currentvalue = 1 ;
}
public delegate void Del( object sender, Evtargs e);
public event Del EvtPlaying;

public void OnEvtPlaying( object sender, Evtargs e) // 定义触发函数
{
if (EvtPlaying != null )
{
EvtPlaying(
this , e);
}

}


#endregion

#region 构造函数
public DownloadMp3Progress()
{
InitializeComponent();
// 减少闪烁
SetStyle(ControlStyles.UserPaint, true );
SetStyle(ControlStyles.AllPaintingInWmPaint,
true );
SetStyle(ControlStyles.OptimizedDoubleBuffer,
true );

}
#endregion

#region 开个线程执行下载任务,避免界面卡死
private void button1_Click( object sender, EventArgs e)
{
Thread th
= new Thread( new ThreadStart(ThdMethod)); // 想清楚多线程要做的是什么?
th.Start();
}
#endregion

#region 下载文件函数
private void downloadfile()
{

// FileStream fs = new FileStream(@" http://218.5.78.142/fj-music/ 世曲唱佛/《西游记》插曲集/17-15-03大圣歌.mp3",FileMode.Open);
string url = textBox1.Text.Trim().ToString();
string file_ext = textBox1.Text.Trim().ToString().Substring(textBox1.Text.Trim().ToString().LastIndexOf( ' . ' )); // 取得下载文件后缀名
string file_name = textBox1.Text.Trim().ToString().Substring(textBox1.Text.Trim().ToString().LastIndexOf( ' / ' ), textBox1.Text.Trim().ToString().LastIndexOf( ' . ' ) - textBox1.Text.Trim().ToString().LastIndexOf( ' / ' )); // 取得下载文件名字

HttpWebRequest wr
= (HttpWebRequest)HttpWebRequest.Create(url);
HttpWebResponse rp
= (HttpWebResponse)wr.GetResponse();
long file_length = rp.ContentLength;
label2.Text
= " 文件大小: " + file_length.ToString();
progressBar1.Maximum
= ( int )file_length;

#region 开始下载
Stream st
= rp.GetResponseStream();
filepath
= @" C:\Downloads\100.mp3 " ; // 文件不存在的话先建个文件,否则找不到,有时候也不报错,怎么回事呢!
// if (File.Exists(filepath))
// {
// File.Delete(filepath);
// File.Create(filepath);
// }
// else
// {
// File.Create(filepath);
// }

FileStream fs
= new FileStream(filepath, FileMode.Create, FileAccess.Write);
long i = 0 ;
while (i < file_length)
{
byte [] buff = new byte [ 1024 ];
i
+= st.Read(buff, 0 , buff.Length);
Application.DoEvents();//没这一句界面会卡,让无关的消息自动处理
fs.Write(buff,
0 , buff.Length);
if (progressBar1 != null )
{
progressBar1.Value
= ( int )i;
label4.Text
= i.ToString();
Thread.Sleep(
20 );
}
}
st.Close();
fs.Close();
button2.Enabled
= true ;
#endregion


}
#endregion

#region 异步调用下载函数
public void ThdMethod()
{
MethodInvoker mi
= new MethodInvoker(downloadfile);
if (InvokeRequired)
{
this .BeginInvoke(mi);
}
}
#endregion

#region 下载路径全选
private void textBox1_Click( object sender, EventArgs e)
{
this .textBox1.SelectAll();
}
#endregion

#region 播放
private void button2_Click( object sender, EventArgs e)
{
#region 播放视频
// Video v = new Video(filepath);
// Audio a = v.Audio;
// v.Owner = panel1;
// v.Play();
#endregion

#region 读取文件备注属性 方法一 ,不完美,信息不全。
// byte[] c = new byte[128];
// string sTitle, sSinger, sAlbum, sYear, sComm;
// FileStream fs = new FileStream(filepath, FileMode.Open);
// fs.Seek(-128, SeekOrigin.End);
// fs.Read(c, 0, 128);
// bool isSet = false;
// String sFlag = System.Text.Encoding.Default.GetString(c, 0, 3);
// if (sFlag.CompareTo("TAG") == 0)
// {
// System.Console.WriteLine("Tag is setted!Replica Watches");
// isSet = true;
// }
// if (isSet)
// {
// sTitle = System.Text.Encoding.Default.GetString(c, 3, 30) + "\n";
// sSinger = System.Text.Encoding.Default.GetString(c, 33, 30) + "\n";
// sAlbum = System.Text.Encoding.Default.GetString(c, 63, 30) + "\n";
// sYear = System.Text.Encoding.Default.GetString(c, 93, 4) + "\n";
// sComm = System.Text.Encoding.Default.GetString(c, 97, 30) + "\n";

// label3.Text = sTitle + sSinger + sAlbum + sYear + sComm;
// // label3.Text = System.Text.Encoding.Default.GetString(c, 0, 128) + "\n";
// label3.ForeColor = Color.Green;
// }
// fs.Close();
// fs.Dispose();
#endregion

#region 方法二 用微软核心模块Shell32.dll
// ShellClass sh = new ShellClass();
// Folder dir = sh.NameSpace(Path.GetDirectoryName(filepath));
// FolderItem item = dir.ParseName(Path.GetFileName(filepath));
// StringBuilder sb = new StringBuilder();
// for (int i = -1; i < 50; i++)
// {
// // 0 Retrieves the name of the item. 12. // 1 Retrieves the size of the item. 13. // 2 Retrieves the type of the item. 14. // 3 Retrieves the date and time that the item was last modified. 15. // 4 Retrieves the attributes of the item. 16. // -1 Retrieves the info tip information for the item. 17. sb.Append(i.ToString());
// // sb.Append(":");
// sb.Append(dir.GetDetailsOf(item, i));
// sb.Append("\r\n");
// }
// this.richTextBox1.Text = sb.ToString();
#endregion

#region Directx播放音乐
b
= new Audio( @" C:\Downloads\1.mp3 " );
// MessageBox.Show(File.GetAttributes(filepath).ToString());
b.Play();

// 如何让滚动条滚动并和播放速度保持同步?
// 用委托试试,只要播放这个事件在进行,事件委托的方法就吧Trackbar的value和播放进度同步
trackBar1.Maximum = ( int )b.Duration;
this .EvtPlaying += new Del(DownloadMp3Progress_EvtPlaying); // 先要注册事件,否则执行不到委托方法
Evtargs ev = new Evtargs();
while (ev.currentvalue < ( int )(b.Duration)) // 持续性事件判断条件一般用while循环语句,一次性的用if等
{
ev.currentvalue
= ( int )b.CurrentPosition;
OnEvtPlaying(
this , ev);
Application.DoEvents();
// Thread.Sleep(20);

}

#endregion

}
#endregion

#region 让trackbar显示播放进度的委托方法
private void DownloadMp3Progress_EvtPlaying( object sender, Evtargs ev)
{
trackBar1.Value
= ev.currentvalue;

}
#endregion

}
}
posted on 2011-02-15 02:30  人的本质是什么? 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/PirateCaptain/articles/1954793.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值