利用API函数[MCISENDSTRING]可以轻松实现MP3音乐文件的....doc

上传人:文库蛋蛋多 文档编号:2389376 上传时间:2023-02-17 格式:DOC 页数:27 大小:66KB
返回 下载 相关 举报
利用API函数[MCISENDSTRING]可以轻松实现MP3音乐文件的....doc_第1页
第1页 / 共27页
利用API函数[MCISENDSTRING]可以轻松实现MP3音乐文件的....doc_第2页
第2页 / 共27页
利用API函数[MCISENDSTRING]可以轻松实现MP3音乐文件的....doc_第3页
第3页 / 共27页
利用API函数[MCISENDSTRING]可以轻松实现MP3音乐文件的....doc_第4页
第4页 / 共27页
利用API函数[MCISENDSTRING]可以轻松实现MP3音乐文件的....doc_第5页
第5页 / 共27页
点击查看更多>>
资源描述

《利用API函数[MCISENDSTRING]可以轻松实现MP3音乐文件的....doc》由会员分享,可在线阅读,更多相关《利用API函数[MCISENDSTRING]可以轻松实现MP3音乐文件的....doc(27页珍藏版)》请在三一办公上搜索。

1、:7122/kw%3D%20mp3利用API函数mciSendString可以轻松实现MP3音乐文件的播放。下面这段程序实现了MP3播放的大部分常规操作,对其稍加修改,做一个100KB大小的MP3播放器轻而易举 启动VB程序,在窗体上放置6个命令按钮,三个标签,一个公用对话框、一个进度条、一个状态栏和一个计时器,窗本的布置请参考附图按钮Open MP3 File是用来打开对话框选择MP3文件,其他5个按钮分别为Play(播放)、Pause(暂停)、Stopplay(停止播放)、Back(向后跳跃)、Prew(向前跳跃)。Label1用来表示歌曲当前时间;label2放在时度条的最左边,Capt

2、ion属性为00:00;lable3放在时度条的右边,用来表示歌曲总长。下面就可以编写代码了。首先在窗体的通用声明部分声明函数Private Declare Function mciSendString Lib winmm.dll Alias mciSendStringA(Byval lpstrCommand As String,ByVal lpstrRetumString As String,ByVal uReturnLength As long,ByVal hwndCallback as long) As longDim mfn As String下面就是各个对象的代码了:Private

3、Sub Form_load()mfn=play.Enabled=Falsepause.Enabled=FalseStopplay.Enabled=Falseback.Enabled=Falseprew.Enabled=FalseTimer1.Enabled=FalseTimer1.Interval=500End SubPrivate Sub open_Click()On Error Goto errWith CommonDialog1.CancelError=True.Filter=音乐文件|*.mp3;*.wav;*.mid.Flags=cdlOFNNoChangeDir And cdlOF

4、NPathMustExist.Action=1stopplay_Click 停止按钮同时可以设备初始化mfn=.FileName 这个程序中文件名一定不能带空格Form1.Caption=.FileNameplay.Enabled=TrueEnd Withplay_ClickExit Suberr:End SubPrivate Sub play_Click()On Error Resume NextDim t As Longt=mciSendString(open + mfn,0&,0,0) Open后边的空格一定不能丢Dim ret As String * 128t=mciSendStrin

5、g(status + mfn + length,ret,128,0)显示歌曲总长ret=Left(ret,8)If ret ThenProgressBar1.Min=0ProgressBar1.Max=Val(ret)Label3.Caption=gettime(Val(ret)End Ifplay.Enabled=Falsepause.Enabled=Truestopplay.Enabled=Trueback.Enabled=Trueprew.Enabled=Truet=mciSendString(status + mfn + mode,ret,128,0)得到设备的当前状态,是播放还是暂停

6、等等ret=Left(ret,8)StatusBar1.Panels(1).text=ret在状态栏显示播放状态t=mciSendString(play + mfn + form + Str(ProgressBar1.Value),0&,0,0)开始播放Timer1.Enabled=TrueEnd SubPrivate Sub pause_Click()t%=mciSendString(pause + mfn,0&,0,0)发出暂停的命令play.Enable=Truepause.Enabled=Falsestopplay.Enabled=Trueback.Enabled=Falseprew.

7、Enabled=FalseEnd SubPrivate Sub stopplay_Click()t%=mciSendString(stop + mfn,0&,0,0)t%=mciSendString(close + mfn,0&,0,0)停止播放play.Enabled=Truepause.Enabled=Falsestopplay.Enabled=Falseback.Enabled=Falseprew.Enabled=FalseEnd SubPrivate Sub back_Click()t%=mciSendString(play + mfn + from + Str(ProgressBar

8、1.Value-(ProgressBar1.Max10),0&,0,0) 向后跳一小段再播放End SubPrivate Sub prew_Click()t%=mciSendString(play + mfn + from + Stri(ProgressBar1.Value+(ProgressBar1.Max10),0&,0,0) 向前跳一小段再播放End SubPrivate Sub Timer1_Timer()Dim t As LongDim ret As String * 128t=mciSendString(status + mfn + position,ret,0,0)得到当前播放位

9、置ret=left(ret,8)ProgressBar1.Value=Val(ret)Label1.Caption=gettime(Val(ret)显示歌曲当前时间If ProgressBar1.Value=ProgressBar1.Max Thenstopplay_ClickEnd Ift=mciSendString(status + mfn + mode,ret,128,0)ret=Left(ret,8)StatusBar1.Panels(1).Text=retEnd SubPrivate Sub Form_Unload(Cacel As Integer)t%=mciSendString(

10、stop + mfn,0&,0,0)t%=mciSendString(close + mfn,0&,0,0)End SubPrivate Function Gettime(position As Long) As String这个函数的功能是把以长整型表示的时间转换为电子钟式的*:*Dim min,secmin=position/1000min=min/60sec=min-Int(min)min=Int(min)sec=60 * sec / 100sec=Int(sec * 100)gettime=Str(min) + : + Str(sec)End Function图中5个控制按钮,其实是把

11、字体设为Webdings,让它们的Caption属性分别为4 ; 7 8就可以了点击:281 发布日期:2007-4-19 8:07:04 进入论坛 添加三个button控件,写代码如下: Imports System.ComponentModel 引用 族名 System.ComponentModel Imports System.Drawing 引用族名 System.Drawing Imports System.WinForms 引用族名 System.WinForms 引用族名的目的是在调用他的子类时不需要写出族名,如system.drawing.color写成color Public

12、 Class Form1 Inherits System.WinForms.Form Inherits是从已有的一个类继承,得到这个类已定义的方法 Public Sub New() MyBase.New() mybase是指代父类 mybase.new是调用父类的new过程,new是新建一个对象,对象是存在内存的,类是并不存在内存的 Form1 = Me 这样,下面的me关键字 就被认定为form1 InitializeComponent() 这下面的代码是在新建一个form1的实例时执行的初始化动作 Call me_load() 这是自定义的一个子过程 End Sub Form overri

13、des dispose to clean up the component list. Overrides Public Sub Dispose() AxMMControl1.Command = close 关闭声音资源,这个代码一定要放在dispose过程的前面 MyBase.Dispose() dispose是清除对象 components.Dispose() 清空组件 End Sub 下面的代码是对属性的描述,是系统自已生成的,不要随意修改 #Region Windows Form Designer generated code Required by the Windows Form

14、Designer Private components As System.ComponentModel.Container Private WithEvents Button3 As System.WinForms.Button Private WithEvents Button2 As System.WinForms.Button Private WithEvents Button1 As System.WinForms.Button Private WithEvents AxMMControl1 As AxMCI.AxMMControl Dim WithEvents Form1 As S

15、ystem.WinForms.Form NOTE: The following procedure is required by the Windows Form Designer It can be modified using the Windows Form Designer. Do not modify it using the code editor. Private Sub InitializeComponent() Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceMa

16、nager(GetType(Form1) Mponents = New System.ComponentModel.Container() Me.Button1 = New System.WinForms.Button() Me.AxMMControl1 = New AxMCI.AxMMControl() Me.Button3 = New System.WinForms.Button() Me.Button2 = New System.WinForms.Button() AxMMControl1.BeginInit() design Me.TrayHeight = 0 design Me.Tr

17、ayLargeIcon = False design Me.TrayAutoArrange = True Button1.Location = New System.Drawing.Point(8, 8) Button1.Size = New System.Drawing.Size(80, 32) Button1.TabIndex = 1 Button1.Text = Button1 AxMMControl1.OcxState = CType(resources.GetObject(AxMMControl1.OcxState), System.WinForms.AxHost.State) Ax

18、MMControl1.TabIndex = 0 AxMMControl1.Size = New System.Drawing.Size(236, 33) AxMMControl1.Location = New System.Drawing.Point(32, 56) Button3.Location = New System.Drawing.Point(208, 8) Button3.Size = New System.Drawing.Size(80, 32) Button3.TabIndex = 3 Button3.Text = Button3 Button2.Location = New

19、System.Drawing.Point(104, 8) Button2.Size = New System.Drawing.Size(88, 32) Button2.TabIndex = 2 Button2.Text = Button2 Me.Text = Form1 Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14) Me.ClientSize = New System.Drawing.Size(296, 29) Me.Controls.Add(Button3) Me.Controls.Add(Button2) Me.Controls

20、.Add(Button1) Me.Controls.Add(AxMMControl1) AxMMControl1.EndInit() End Sub #End Region 上面的代码是对属性的描述,是系统自已生成的,不要随意修改 Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) AxMMControl1.From = 8815 从3000毫秒开始 AxMMControl1.To =15624 从6000毫秒结束 AxMMControl1.Command = play 播放 End

21、Sub Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Me.Dispose() 调用dispose退出程序,释放占用的资源 End Sub Protected Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs) End Sub Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) AxMM

22、Control1.From = 0 从 0毫秒开始 AxMMControl1.To = 8815 从3000毫秒结束 AxMMControl1.Command = play 播放 End Sub Protected Sub AxMMControl1_OLEDragDrop(ByVal sender As Object, ByVal e As AxMCI.DmciEvents_OLEDragDropEvent) End Sub Public Sub me_load() Me.BackColor = color.BlanchedAlmond 改变背景色为BlanchedAlmond Me.Bord

23、erStyle = FormBorderStyle.None 改变窗体边框为none,没有标题栏 button1.Text = 播放上段 : button2.Text = 播放下段 : button3.Text = 退出程序 AxMMControl1.TimeFormat = 0 AxMMControl1.DeviceType = waveaudio 定义播放*.wav格式 AxMMControl1.filename = System.WinForms.Application.StartUpPath & & muli.wav 载入文件, System.WinForms.Application.

24、StartUpPath为当前目录的意思 AxMMControl1.Command = open 打开载入的文件 AxMMControl1.Visible = False 使控件不可见,在后台工作 End Sub End Class 至于从几毫秒开始到几毫秒结束,是把不同的声音文件使用如acoustica.exe这样的声音编辑程序整理成一个文件,记住先使用windows的录音机录下来,这样生成的文件较小,把一些杂音,空白都剪切掉,这样就是很小了 本文来自: 中国自学编程网() 详细出处参考:VB.NET中声音的播放Montaque(原作)由VB6升级为.NET后,有些人不清楚声音的处理,比如程序

25、出错的时候,自定义一个声音播放,或者程序的背景音乐、包括游戏音乐等等。下面介绍几种在VB.NET中计较简单可以实现的方案:1Beep最简单的一种方法,通过计算机的扬声器发出声响, 声响的音高与持续时间取决于硬件和系统软件,从而随计算机不同而不同。Beepg跟Msgbox等方法位于Microsoft.VisualBasic.Interaction 中,一般默认系统会自动加载。调用很简单,看下面的例子。Dim I As IntegerFor I = 1 To 100 Loop 100 times.Beep()Sound a tone.Next I2 用Win32 API PlaySoundPlay

26、Sound位于winmm.dll中,可以根据输入参数的不同,播放WAV类型的音乐。在VB.NET中调用跟VB6中差不多:先声明:Private Declare Auto Function PlaySound Lib winmm.dll (ByVal lpszSoundName As String, ByVal hModule As Integer, ByVal dwFlags As Integer) As IntegerConst SND_FILENAME As Integer = &H20000Const SND_ALIAS As Integer = &H10000Const SND_SYN

27、C As Integer = &H0具体还有很多常量,不一一列举,下面是播放一个wav文件的例子Dim mstrfileName As String = c:eagle2.wavPlaySound(mstrfileName, 0, SND_FILENAME)注意上面的播放是非同步的,就是说它并不会播放完毕才结束。如果把上面的代码写在一个Button.Click的事件处理程序中,第二次点击的时候它会打断第一次的播放,重新开始。当然我们也可以调用系统的声音:PlaySound(SystemStart, 0, SND_ALIAS Or SND_SYNC) 以同步的方式调用系统启动时候的声音。3调用媒

28、体播放器控件VB.NET中并没有提供媒体播放器的.NET组件,没办法我们还是调用以前Com组件Windows Media Player。当然除了声音外,还可以播放视频文件。首先把媒体播放器控件加到工具栏中,调用还是简单的写一下吧:)Private Sub playMediaFile(ByVal mediaFileName As String)With MediaPlayer1.Stop().FileName = c:mp3爱不爱我.mp3.Play()End WithEnd Sub总结:虽然VB.NET比VB6改变了很多,包括真正的OOP,多线程等等,但是总归还是跟VB有很多联系,建议大家碰到

29、难于解决的问题的时候,试一下以前的处理方式,一般都能奏效JPrivate Sub Command1ClickDim fs As New FileSystemObject建立FileSystemObjectDim fd As Folder定义Folder对象Dim sfd As FolderSet fdfs.GetFolderCombo1.TextCommand1.EnabledFalseScreen.MousePointervbHourglassFindFile fd.mp3Text1.TextCommand1.EnabledTrueScreen.MousePointervbDefaultEn

30、d Sub以下过程实现查找指定分区下的所有MP3文件Sub FindFilefd As FolderFileName As StringDim sfd As Folderf As File第一部分:查找该文件夹的所有文件For Each f In fd.FilesLabel2f.PathIf UCasef.NameLike UCaseFileNameThenList1.AddItem f.Pathlabel3共找到List1.ListCount首MP3文件End IfDoEventsNext第二部分:循环查找所有子文件夹For Each sfd In fd.SubFoldersFindFile

31、 sfdFileName循环查找NextEnd Sub保存按钮的源代码:Private Sub Command2ClickCommonDialog1.Filterrealplay播放列表文件.ram.ramCommonDialog1.ShowSaveFor i0 To List1.ListCount - 1text1.Texttext1.Textfile/List1.ListivbCrLfNext以下代码将文件名中的替换为/Dim intCount As IntegerDim lngPos As LongDim intOptions As Integer设置初始信息intCount0lngPo

32、s0With text1DoIf text1.FindlngPosintOptions-1 Then如果没找到Exit DoElse如果文件名中有,则换为/lngPostext1.SelStarttext1.SelLengthintCountintCount1设置计数器text1.SelText/替换End IfLoopEnd With将TEXT1的内容保存为一个文件Open CommonDialog1.FileName For Output As1Print1text1.TextClose#1Msgbox“文件”&CommonDialogl.FileName&“已成功保存”,vbMsgBox

33、Set-Foreground,“提示”End Sub以上程序在Windows 98SE,Windows Me VB6.0中文企业版中运行通过。 这个程序正是利用了MediaPlayer控件可以播放MP3和M3U文件的特性来实现队列播放MP3的。我再说一下M3U文件,这种文件实际上是ASCII码文件,如果你用记事本打开它,就可以发现文件的内容实际上就是多媒体文件的地址列表,能够播放它的程序会顺序播放文件里列出的多媒体文件。下面就是程序的实现步骤:首先建立一个新窗体Form1,添加DriveListBox,DirListBox,FileListBox各一个,Caption属性分别设为Drive1,

34、Dir1和File1,再添加CommandButton以及MediaPlayer控件各一个。然后编写代码如下: Option EXPlicitPrivate Sub Command1_Click()Dim num As IntegerDim filename As StringDim filenum As IntegerDim i As Integernum=File1.ListCountfilenum=FreeFileOpen C:filelist.m3u For Output As filenumFor i=0 To num1If File1.Selected(i) Thenfilenam

35、e=File1.PathFile1.List(i)End IfPrint filenum,filenameNextClose filenumMediaPlayer1.filename=C:filelist.m3uEnd SubPrivate Sub Dir1_Change()File1.Path=Dir1.PathEnd Sub添加三个button控件,写代码如下: Imports System.ComponentModel 引用 族名 System.ComponentModel Imports System.Drawing 引用族名 System.Drawing Imports System

36、.WinForms 引用族名 System.WinForms 引用族名的目的是在调用他的子类时不需要写出族名,如system.drawing.color写成color Public Class Form1 Inherits System.WinForms.Form Inherits是从已有的一个类继承,得到这个类已定义的方法 Public Sub New() MyBase.New() mybase是指代父类 mybase.new是调用父类的new过程,new是新建一个对象,对象是存在内存的,类是并不存在内存的 Form1 = Me 这样,下面的me关键字 就被认定为form1 Initiali

37、zeComponent() 这下面的代码是在新建一个form1的实例时执行的初始化动作 Call me_load() 这是自定义的一个子过程 End Sub Form overrides dispose to clean up the component list. Overrides Public Sub Dispose() AxMMControl1.Command = close 关闭声音资源,这个代码一定要放在dispose过程的前面 MyBase.Dispose() dispose是清除对象 components.Dispose() 清空组件 End Sub 下面的代码是对属性的描述,

38、是系统自已生成的,不要随意修改 #Region Windows Form Designer generated code Required by the Windows Form Designer Private components As System.ComponentModel.Container Private WithEvents Button3 As System.WinForms.Button Private WithEvents Button2 As System.WinForms.Button Private WithEvents Button1 As System.WinF

39、orms.Button Private WithEvents AxMMControl1 As AxMCI.AxMMControl Dim WithEvents Form1 As System.WinForms.Form NOTE: The following procedure is required by the Windows Form Designer It can be modified using the Windows Form Designer. Do not modify it using the code editor. Private Sub InitializeCompo

40、nent() Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1) Mponents = New System.ComponentModel.Container() Me.Button1 = New System.WinForms.Button() Me.AxMMControl1 = New AxMCI.AxMMControl() Me.Button3 = New System.WinForms.Button() Me.Button2 = N

41、ew System.WinForms.Button() AxMMControl1.BeginInit() design Me.TrayHeight = 0 design Me.TrayLargeIcon = False design Me.TrayAutoArrange = True Button1.Location = New System.Drawing.Point(8, 8) Button1.Size = New System.Drawing.Size(80, 32) Button1.TabIndex = 1 Button1.Text = Button1 AxMMControl1.Ocx

42、State = CType(resources.GetObject(AxMMControl1.OcxState), System.WinForms.AxHost.State) AxMMControl1.TabIndex = 0 AxMMControl1.Size = New System.Drawing.Size(236, 33) AxMMControl1.Location = New System.Drawing.Point(32, 56) Button3.Location = New System.Drawing.Point(208, 8) Button3.Size = New System.Drawing.Size(80, 32) Button3.TabIndex = 3 Button3.Text = Button3 Button2.Location = New System.Drawing.Point(104, 8) Button2.Size = New System.Drawing.Size(88, 32) Button2.TabIndex = 2 Button2.Text = Button2 Me.Text = Form1 Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14) Me.ClientSize = N

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 建筑/施工/环境 > 项目建议


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号