vb.net读取歌词 vbs播放音乐-成都快上网建站

vb.net读取歌词 vbs播放音乐

VB.NET 怎么同步显示lrc歌词??

可以稍微改下,预读两句歌词的时间范围,时间大于第一句的起始点,小于第二句的起始点,那么显示第一句,

网站的建设成都创新互联专注网站定制,经验丰富,不做模板,主营网站定制开发.小程序定制开发,H5页面制作!给你焕然一新的设计体验!已为成都三维植被网等企业提供专业服务。

关于播放器中的功能(歌词同步),急求VB.NET代码!!

按照你的说法,应该要用到Ajax,对网页中播放器插件进行帧听,然后再对歌词部分进行处理,百度的Mp3就是这样做的。

下面就简单的说一下吧

材料:歌曲、歌曲对应的歌词(可以是XML,当然也可以是歌词专用格式lrc,随便啦,什么文件都可以)、几条简单的JS语句

处理:

1、JSP生成网页的时候,将音乐播放器插件的ID命名为WMA,当然,你可以随便命名,只是这里便于说明。

2、读取XML或LRC内容,利用Ajax加载到id为lrc的div中。

并将XML或LRC的每行的歌词存到数组testmp3中(此数组当然是二维数组,数据组分别播放时间与歌词内容)

好了,到此,准备工作就做好了,下面就是重点---歌词同步。

3、相关语句:

一、定义函数getWMAtime

二、利用Interval=setInterval("getWMAtime()",900),意思就是每隔900毫秒调用getWMAtime这个函数,并将其放入Interval中。

如果要用到“上一曲”“下一曲”,可以将其做为了一个函数,

将音乐的地址赋给WMA.URL,用WMA.controls.play();播放,用WMA.controls.stop();停止播放,用WMA.controls.pause();暂停播放。改变了音乐地址后,记得用Ajax载入新的歌词!

另:

getWMAtime函数的内容为读取当前词曲的播放时间

当前的播放时间:WMA.controls.currentPosition;

歌曲总时间:WMA.currentMedia.duration;

读取当前的时候后,与数组中时间项进行对比,相同(由于其它原因,可能会出现不相同的情况,因此则用“=”)则用Ajax对id为lrc的div进行更改,具体怎么改那就随便你咯,一般就是改变一下指定行的颜色。

如果前当播放时间==歌曲总时间,那么就用clearInterval(Interval),停止Interval继续调用。

好了,一个简单的JS的歌词同步播放器就完工啦

使用VB.NET制作带歌词显示的mp3播放器

Public Class Form1

Dim soundname As String()

Dim i As Integer

Dim j, l As Boolean

Dim k As Integer

Dim filename As String '取歌曲的名字

Dim path As String()

Dim s, m As Integer

Dim luj As String

Dim count As Integer '用来声明选择歌曲的数目

Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click

selectsound()

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim g As Graphics

g = Graphics.FromHwnd(Me.Handle)

Dim mfont As New Font("宋体", 20)

Dim mstringformat As New StringFormat

mstringformat.Alignment = StringAlignment.Center

mstringformat.LineAlignment = StringAlignment.Center

g.DrawString("X071616", mfont, Brushes.Black, 75, 328, mstringformat)

If ListBox1.Items.Count = 0 Then selectsound() : Exit Sub

playsound()

End Sub

Private Sub TrackBar1_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar1.Scroll

Me.AxWindowsMediaPlayer1.settings.volume = TrackBar1.Value

End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

If Timer1.Enabled = False Then

Timer1.Enabled = True

Else

Timer1.Enabled = False

End If

If i = 0 Then

Me.AxWindowsMediaPlayer1.Ctlcontrols.pause()

i = 1

Button3.Text = "继续"

Exit Sub

End If

If i = 1 Then

Me.AxWindowsMediaPlayer1.Ctlcontrols.play()

Button3.Text = "暂停"

i = 0

Exit Sub

End If

End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click

j = True

playsound()

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

Me.AxWindowsMediaPlayer1.Ctlcontrols.stop()

End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

Dim string1, ss, sss As String

string1 = ListBox1.SelectedItem.ToString.Substring(ListBox1.SelectedItem.ToString.LastIndexOf("\") + 1)

filename = string1.Substring(0, string1.LastIndexOf("."))

luj = ListBox1.SelectedItem.ToString.Substring(0, ListBox1.SelectedItem.ToString.LastIndexOf(".")) + ".lrc"

Me.HScrollBar1.Value = Me.AxWindowsMediaPlayer1.Ctlcontrols.currentPosition / (Me.AxWindowsMediaPlayer1.currentMedia.duration + 0.001) * 1000

ss = Me.AxWindowsMediaPlayer1.Ctlcontrols.currentPositionString

Me.Label1.Text = ss

If Dir(luj) Nothing Then

Dim fr As System.IO.StreamReader = New System.IO.StreamReader(luj, System.Text.Encoding.Default)

sss = fr.ReadLine '我们读取的第一行歌词

While (sss "")

If (sss.LastIndexOf(ss) -1) Then '时间匹配了歌词中的时间

Me.TextBox1.Text = sss.Substring(sss.LastIndexOf("]") + 1)

Form2.TextBox1.Text = Me.TextBox1.Text

End If

sss = fr.ReadLine

End While

Else

TextBox1.Text = "Not Find! 请从网上下载歌词!"

Form2.TextBox1.Text = "Not Find! 请从网上下载歌词!"

End If

Label1.Text = "当前进度:" + Me.AxWindowsMediaPlayer1.Ctlcontrols.currentPositionString

End Sub

Private Sub HScrollBar1_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles HScrollBar1.Scroll

Me.AxWindowsMediaPlayer1.Ctlcontrols.currentPosition = Me.HScrollBar1.Value / 1000 * Me.AxWindowsMediaPlayer1.currentMedia.duration

End Sub

Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click

Me.AxWindowsMediaPlayer1.Ctlcontrols.fastForward()

End Sub

Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click

Me.AxWindowsMediaPlayer1.Ctlcontrols.fastReverse()

End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

i = 0

j = False

l = False

Label2.Text = "歌曲名: "

Label3.Text = "歌曲演唱者: "

Label4.Text = "歌曲描述: "

Label5.Text = "歌曲类型: "

Label6.Text = "歌曲大小: "

Button8.Enabled = False

End Sub

Private Sub ListBox1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.DoubleClick

Me.AxWindowsMediaPlayer1.URL = ListBox1.Items(ListBox1.SelectedIndex)

Me.AxWindowsMediaPlayer1.Ctlcontrols.play()

Label2.Text = "歌曲名: " + Me.AxWindowsMediaPlayer1.currentMedia.getItemInfo("Title")

Label3.Text = "歌曲演唱者: " + Me.AxWindowsMediaPlayer1.currentMedia.getItemInfo("Author")

Label4.Text = "歌曲描述: " + Me.AxWindowsMediaPlayer1.currentMedia.getItemInfo("Description")

Label5.Text = "歌曲类型: " + Me.AxWindowsMediaPlayer1.currentMedia.getItemInfo("FileType")

Label6.Text = "歌曲大小: " + Me.AxWindowsMediaPlayer1.currentMedia.getItemInfo("FileSize")

Timer1.Start()

End Sub

Private Sub playsound()

Timer1.Stop()

TrackBar1.Value = Me.AxWindowsMediaPlayer1.settings.volume

If j = False Then

Me.AxWindowsMediaPlayer1.URL = ListBox1.Items(ListBox1.SelectedIndex)

ElseIf j = True Then

k = (ListBox1.SelectedIndex + 1) Mod soundname.Length

Me.AxWindowsMediaPlayer1.URL = ListBox1.Items(k)

j = False

End If

ListBox1.SelectedIndex = k

Me.AxWindowsMediaPlayer1.Ctlcontrols.play()

Label2.Text = "歌曲名: " + Me.AxWindowsMediaPlayer1.currentMedia.getItemInfo("Title")

Label3.Text = "歌曲演唱者: " + Me.AxWindowsMediaPlayer1.currentMedia.getItemInfo("Author")

Label4.Text = "歌曲描述: " + Me.AxWindowsMediaPlayer1.currentMedia.getItemInfo("Description")

Label5.Text = "歌曲类型: " + Me.AxWindowsMediaPlayer1.currentMedia.getItemInfo("FileType")

Label6.Text = "歌曲大小: " + Me.AxWindowsMediaPlayer1.currentMedia.getItemInfo("FileSize")

Timer1.Start()

End Sub

Private Sub selectsound()

Dim open As New OpenFileDialog

Dim i As Integer

With open

.Filter = "所有mp3文件(*.mp3)|*.mp3|所有wma文件(*.wma)|*.wma"

.Multiselect = True

.Title = "请选择歌曲"

End With

If (open.ShowDialog = Windows.Forms.DialogResult.OK) Then

soundname = open.FileNames

For i = 0 To soundname.GetUpperBound(0)

ListBox1.Items.Add(soundname(i))

Next

ListBox1.SelectedIndex = 0

End If

End Sub

Private Sub AxWindowsMediaPlayer1_PlayStateChange(ByVal sender As System.Object, ByVal e As AxWMPLib._WMPOCXEvents_PlayStateChangeEvent) Handles AxWindowsMediaPlayer1.PlayStateChange

If AxWindowsMediaPlayer1.playState = WMPLib.WMPPlayState.wmppsMediaEnded Then

j = True

Timer2.Start()

End If

End Sub

Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick

playsound()

Timer2.Stop()

End Sub

Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click

Timer1.Stop()

If ListBox1.SelectedIndex = 0 Then

Me.AxWindowsMediaPlayer1.URL = ListBox1.Items(soundname.Length - 1)

ListBox1.SelectedIndex = (soundname.Length - 1)

Else

Me.AxWindowsMediaPlayer1.URL = ListBox1.Items((ListBox1.SelectedIndex Mod soundname.Length) - 1)

ListBox1.SelectedIndex = (ListBox1.SelectedIndex Mod soundname.Length) - 1

End If

Me.AxWindowsMediaPlayer1.Ctlcontrols.play()

Label2.Text = "歌曲名: " + Me.AxWindowsMediaPlayer1.currentMedia.getItemInfo("Title")

Label3.Text = "歌曲演唱者: " + Me.AxWindowsMediaPlayer1.currentMedia.getItemInfo("Author")

Label4.Text = "歌曲描述: " + Me.AxWindowsMediaPlayer1.currentMedia.getItemInfo("Description")

Label5.Text = "歌曲类型: " + Me.AxWindowsMediaPlayer1.currentMedia.getItemInfo("FileType")

Label6.Text = "歌曲大小: " + Me.AxWindowsMediaPlayer1.currentMedia.getItemInfo("FileSize")

Timer1.Start()

End Sub

Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click

Form2.Show()

End Sub

End Class

vb.net获取mp3的歌曲名和其他属性

从MP3中提取歌曲信息

一首MP3歌曲除了音乐信息外,还包含了如歌名、演唱者等信息,当我们用winamp软件听音乐时,播放清单就自动将这些信息读出来。大部分人都喜欢从网上下载音乐,但下载下来的MP3文件名都是文件上传系统自动取名的,和歌曲本身根本不相符,所以,给用户带来了很大的麻烦。但是,懒人有懒人的做法,我们何不自己写一个程序,将歌曲信息自动读出来并为MP3文件自动更名呢?

下面以C#为工具,把开发过程写出来。

一首MP3的额外信息存放在文件的最后面,共占128个字节,其中包括以下的内容(我们定义一个结构说明):

public struct Mp3Info

{

public string identify;//TAG,三个字节

public string Title;//歌曲名,30个字节

public string Artist;//歌手名,30个字节

public string Album;//所属唱片,30个字节

public string Year;//年,4个字符

public string Comment;//注释,28个字节

public char reserved1;//保留位,一个字节

public char reserved2;//保留位,一个字节

public char reserved3;//保留位,一个字节

}

所以,我们只要把MP3文件的最后128个字节分段读出来并保存到该结构里就可以了。函数定义如下:

///

/// 获取MP3文件最后128个字节

///

/// 文件名

/// 返回字节数组

private byte[] getLast128(string FileName)

{

FileStream fs = new FileStream(FileName,FileMode.Open,FileAccess.Read);

Stream stream = fs;

stream.Seek(-128,SeekOrigin.End);

const int seekPos = 128;

int rl = 0;

byte[] Info = new byte[seekPos];

rl = stream.Read(Info,0,seekPos);

fs.Close();

stream.Close();

return Info;

}

再对上面返回的字节数组分段取出,并保存到Mp3Info结构中返回。

///

/// 获取MP3歌曲的相关信息

///

/// 从MP3文件中截取的二进制信息

/// 返回一个Mp3Info结构

private Mp3Info getMp3Info(byte[] Info)

{

Mp3Info mp3Info = new Mp3Info();

string str = null;

int i;

int position = 0;//循环的起始值

int currentIndex = 0;//Info的当前索引值

//获取TAG标识

for(i = currentIndex;i

{

str = str+(char)Info[i];

position++;

}

currentIndex = position;

mp3Info.identify = str;

//获取歌名

str = null;

byte[] bytTitle = new byte[30];//将歌名部分读到一个单独的数组中

int j = 0;

for(i = currentIndex;i

{

bytTitle[j] = Info[i];

position++;

j++;

}

currentIndex = position;

mp3Info.Title = this.byteToString(bytTitle);

//获取歌手名

str = null;

j = 0;

byte[] bytArtist = new byte[30];//将歌手名部分读到一个单独的数组中

for(i = currentIndex;i

{

bytArtist[j] = Info[i];

position++;

j++;

}

currentIndex = position;

mp3Info.Artist = this.byteToString(bytArtist);

//获取唱片名

str = null;

j = 0;

byte[] bytAlbum = new byte[30];//将唱片名部分读到一个单独的数组中

for(i = currentIndex;i

{

bytAlbum[j] = Info[i];

position++;

j++;

}

currentIndex = position;

mp3Info.Album = this.byteToString(bytAlbum);

//获取年

str = null;

j = 0;

byte[] bytYear = new byte[4];//将年部分读到一个单独的数组中

for(i = currentIndex;i

{

bytYear[j] = Info[i];

position++;

j++;

}

currentIndex = position;

mp3Info.Year = this.byteToString(bytYear);

//获取注释

str = null;

j = 0;

byte[] bytComment = new byte[28];//将注释部分读到一个单独的数组中

for(i = currentIndex;i

{

bytComment[j] = Info[i];

position++;

j++;

}

currentIndex = position;

mp3Info.Comment = this.byteToString(bytComment);

//以下获取保留位

mp3Info.reserved1 = (char)Info[++position];

mp3Info.reserved2 = (char)Info[++position];

mp3Info.reserved3 = (char)Info[++position];

return mp3Info;

}

上面程序用到下面的方法:

///

/// 将字节数组转换成字符串

///

/// 字节数组

/// 返回转换后的字符串

private string byteToString(byte[] b)

{

Encoding enc = Encoding.GetEncoding("GB2312");

string str = enc.GetString(b);

str = str.Substring(0,str.IndexOf('\0') = 0 ? str.IndexOf('\0') : str.Length);//去掉无用字符

return str;

}

改名怎么办呢?我们按(演唱者)歌名 的格式对歌曲进行改名,程序如下:

///

/// 更改文件名

///

/// 文件名

///

private bool ReName(string filePath)

{

if(File.Exists(filePath))

{

Mp3Info mp3Info = new Mp3Info();

mp3Info = this.getMp3Info(this.getLast128(filePath));//读出文件信息

mp3Info.Artist = this.DeleteNotValue(mp3Info.Artist);

mp3Info.Title = this.DeleteNotValue(mp3Info.Title);

if(mp3Info.Artist.Trim().Length==0)

{

mp3Info.Artist="未命名";

}

if(mp3Info.Title.Trim().Length==0)

{

mp3Info.Title="未知名歌曲";

}

try

{

//更名

File.Move(filePath,filePath.Substring(0,filePath.ToLower().LastIndexOf("\\")).Trim() + "\\" + "(" + mp3Info.Artist.Trim() + ")" +mp3Info.Title.Trim() + ".mp3");

return true;

}

catch(Exception)

{

return false;

}

}

else

{

return false;

}

}


网站栏目:vb.net读取歌词 vbs播放音乐
分享链接:http://kswjz.com/article/ddddidh.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流