|
-
Oct 5th, 2008, 05:15 PM
#1
Thread Starter
Junior Member
[2008] I have to pass strings through a hidden textbox before adding to listview
OK so I have some code which gets the title and artist from the ID3 tag of an mp3 file.
Now the problem is, in order for these to properly show up in my listview, I have to pass them through a textbox first.
Heres my code:
Code:
For Each filePath As String In IO.Directory.GetFiles(DirListBox1.Path, "*.mp3", IO.SearchOption.AllDirectories)
Dim title As String = ""
Dim artist As String = ""
Dim buffer(128) As Byte
Dim mp3File As New FileInfo(filePath)
If mp3File.Length > 128 Then
Dim i As Integer
Dim mp3Reader As Stream = mp3File.OpenRead()
mp3Reader.Seek(-128, SeekOrigin.End)
For i = 0 To 127
buffer(i) = mp3Reader.ReadByte
Next
mp3Reader.Close()
End If
If Encoding.Default.GetString(buffer, 0, 3).Equals("TAG") Then
title = Trim(Encoding.Default.GetString(buffer, 3, 30))
artist = Trim(Encoding.Default.GetString(buffer, 33, 30))
End If
TextBox1.Text = title
TextBox2.Text = artist
fMain.ListView1.Items.Add(TextBox2.Text + " - " + TextBox1.Text).SubItems.Add(filePath)
Next
If I do not pass the strings through the textbox, they do not show up properly in the listview. Why is this happening?
-
Oct 5th, 2008, 07:46 PM
#2
Re: [2008] I have to pass strings through a hidden textbox before adding to listview
try this:
vb Code:
title = Trim(System.Text.Encoding.Default.GetString(buffer, 3, 30))
artist = Trim(System.Text.Encoding.Default.GetString(buffer, 33, 30).Substring(0, System.Text.Encoding.Default.GetString(buffer, 33, 30).IndexOf(Chr(0))))
the artist string wasn't trimming correctly
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 5th, 2008, 08:02 PM
#3
Re: [2008] I have to pass strings through a hidden textbox before adding to listview
Hey,
I did a quick google, and I came up with this post, seems like a neater solution.
Just a thought.
Gary
-
Oct 5th, 2008, 08:05 PM
#4
Thread Starter
Junior Member
Re: [2008] I have to pass strings through a hidden textbox before adding to listview
 Originally Posted by .paul.
try this:
vb Code:
title = Trim(System.Text.Encoding.Default.GetString(buffer, 3, 30))
artist = Trim(System.Text.Encoding.Default.GetString(buffer, 33, 30).Substring(0, System.Text.Encoding.Default.GetString(buffer, 33, 30).IndexOf(Chr(0))))
the artist string wasn't trimming correctly
nah it still isn't working properly. i will just stick with the textboxes for now and hide them behind the dirlistbox i have on the same form.
-
Oct 5th, 2008, 08:06 PM
#5
Thread Starter
Junior Member
Re: [2008] I have to pass strings through a hidden textbox before adding to listview
 Originally Posted by gep13
Hey,
I did a quick google, and I came up with this post, seems like a neater solution.
Just a thought.
Gary
that looks like it does the exact same thing as what my current code does?
-
Oct 5th, 2008, 08:07 PM
#6
Re: [2008] I have to pass strings through a hidden textbox before adding to listview
Well yes, the difference being that the code I linked to works.
I adapted the code in that link, and incorporated it with the code the you posted, and I got this:
Code:
Imports System.IO
Imports System.Text
Public Class Form4
Private _Tag As String ' (3)
Private _Songname As String ' (30)
Private _Artist As String ' (30)
Private _Album As String ' (30)
Private _Year As String ' (4)
Private _Comment As String ' (30)
Private _Genre As String ' (1)
Private _DetailsRead As Boolean
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
For Each filePath As String In IO.Directory.GetFiles(DirListBox1.Text, "*.mp3", IO.SearchOption.AllDirectories)
Dim buffer(128) As Byte
Dim mp3File As New FileInfo(filePath)
Dim fs As New FileStream(filePath, FileMode.Open, FileAccess.Read)
Dim br As New BinaryReader(fs)
If mp3File.Length > 128 Then
br.BaseStream.Seek(-128, SeekOrigin.End)
_Tag = br.ReadChars(3)
If _Tag = "TAG" Then
' Read the rest of the data
_Songname = br.ReadChars(30)
_Artist = br.ReadChars(30)
_Album = br.ReadChars(30)
_Year = br.ReadChars(4)
_Comment = br.ReadChars(30)
_Genre = br.ReadChars(1)
_DetailsRead = True
Else
_DetailsRead = False
End If
' Cleanup
br.Close()
fs.Close()
br = Nothing
fs = Nothing
End If
ListView1.Items.Add(_Songname + " - " + _Artist).SubItems.Add(filePath)
Next
End Sub
End Class
And it works, without the need to go through textboxes.
Gary
-
Oct 5th, 2008, 08:08 PM
#7
Re: [2008] I have to pass strings through a hidden textbox before adding to listview
Bear in mind, that I wasn't sure what kind of control you were using for DirListBox1, so I just used a textbox in my sample code.
Gary
-
Oct 5th, 2008, 08:09 PM
#8
Re: [2008] I have to pass strings through a hidden textbox before adding to listview
 Originally Posted by gep13
Hey,
I did a quick google, and I came up with this post, seems like a neater solution.
Just a thought.
Gary
yep. that was neat
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 5th, 2008, 08:11 PM
#9
Re: [2008] I have to pass strings through a hidden textbox before adding to listview
 Originally Posted by KJDion
nah it still isn't working properly. i will just stick with the textboxes for now and hide them behind the dirlistbox i have on the same form.
What does "still isn't working" mean anyways? What are you getting that you shouldn't.... or what are you not getting that you should? Or are you getting an error?
-tg
-
Oct 5th, 2008, 08:11 PM
#10
Thread Starter
Junior Member
Re: [2008] I have to pass strings through a hidden textbox before adding to listview
oh i get it, it uses a binary reader
yeah i was looking into binary readers earlier too
-
Oct 5th, 2008, 08:12 PM
#11
Re: [2008] I have to pass strings through a hidden textbox before adding to listview
So, does that mean that you got it working?
If so, remember to mark your thread as resolved.
-
Oct 5th, 2008, 08:17 PM
#12
Thread Starter
Junior Member
Re: [2008] I have to pass strings through a hidden textbox before adding to listview
ok well that code you posted doesnt work either, gep13
i mean it is not displaying the songname at all in the list
code that does not work:
Code:
Private Sub bAddFolder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bAddFolder.Click
For Each filePath As String In IO.Directory.GetFiles(DirListBox1.Path, "*.mp3", IO.SearchOption.AllDirectories)
Dim tag As String ' (3)
Dim song As String = "" ' (30)
Dim artist As String = "" ' (30)
Dim buffer(128) As Byte
Dim mp3File As New FileInfo(filePath)
Dim fs As New FileStream(filePath, FileMode.Open, FileAccess.Read)
Dim br As New BinaryReader(fs)
If mp3File.Length > 128 Then
br.BaseStream.Seek(-128, SeekOrigin.End)
tag = br.ReadChars(3)
If tag = "TAG" Then
song = br.ReadChars(30)
artist = br.ReadChars(30)
End If
br.Close()
fs.Close()
br = Nothing
fs = Nothing
End If
If Not artist.Equals("") And Not song.Equals("") Then
fMain.ListView1.Items.Add(artist + " - " + song + "(yes)").SubItems.Add(filePath)
Else
fMain.ListView1.Items.Add(Path.GetFileNameWithoutExtension(filePath)).SubItems.Add(filePath)
End If
Next
End Sub
code that does work:
Code:
Private Sub bAddFolder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bAddFolder.Click
For Each filePath As String In IO.Directory.GetFiles(DirListBox1.Path, "*.mp3", IO.SearchOption.AllDirectories)
Dim title As String = ""
Dim artist As String = ""
Dim buffer(128) As Byte
Dim mp3File As New FileInfo(filePath)
If mp3File.Length > 128 Then
Dim i As Integer
Dim mp3Reader As Stream = mp3File.OpenRead()
mp3Reader.Seek(-128, SeekOrigin.End)
For i = 0 To 127
buffer(i) = mp3Reader.ReadByte
Next
mp3Reader.Close()
End If
If Encoding.Default.GetString(buffer, 0, 3).Equals("TAG") Then
title = Trim(Encoding.Default.GetString(buffer, 3, 30))
artist = Trim(Encoding.Default.GetString(buffer, 33, 30))
End If
TextBox1.Text = title
TextBox2.Text = artist
If Not TextBox1.Text.Equals("") And Not TextBox2.Text.Equals("") Then
fMain.ListView1.Items.Add(TextBox2.Text + " - " + TextBox1.Text).SubItems.Add(filePath)
Else
fMain.ListView1.Items.Add(Path.GetFileNameWithoutExtension(filePath)).SubItems.Add(filePath)
End If
Next
End Sub
-
Oct 5th, 2008, 08:33 PM
#13
Re: [2008] I have to pass strings through a hidden textbox before adding to listview
try this. the problem is again as i suggested earlier.
vb Code:
If tag = "TAG" Then
song = br.ReadChars(30)
artist = br.ReadChars(30)
If song.Contains(Chr(0)) Then song = song.Substring(0, song.IndexOf(Chr(0)))
If artist.Contains(Chr(0)) Then artist = artist.Substring(0, artist.IndexOf(Chr(0)))
End If
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 5th, 2008, 08:37 PM
#14
Re: [2008] I have to pass strings through a hidden textbox before adding to listview
Man, that was driving me crazy for a second there, was trying everything I could think of to get it to work 
Here is a completed version that uses Pauls code above, as well as the code in the linked posted:
Code:
Imports System.IO
Imports System.Text
Public Class Form4
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
For Each filePath As String In IO.Directory.GetFiles(DirListBox1.Text, "*.mp3", IO.SearchOption.AllDirectories)
Dim details As New MP3Details()
If details.ReadMP3Details(filePath) = True Then
Dim entry As String = String.Format("{0} - {1}", details.Artist, details.Songname)
ListView1.Items.Add(entry).SubItems.Add(filePath)
End If
Next
End Sub
End Class
Public Class MP3Details
#Region "Attributes"
Private _Tag As String ' (3)
Private _Songname As String ' (30)
Private _Artist As String ' (30)
Private _Album As String ' (30)
Private _Year As String ' (4)
Private _Comment As String ' (30)
Private _Genre As String ' (1)
Private _DetailsRead As Boolean
#End Region
Public Sub New()
End Sub
Public Function ReadMP3Details(ByVal Filename As String) As Boolean
' Initialise
_DetailsRead = False
' Does the file exist?
If Not New IO.FileInfo(Filename).Exists Then
_DetailsRead = False
Return False
End If
' Reads the MP3 tag details
Dim fs As New IO.FileStream(Filename, IO.FileMode.Open, IO.FileAccess.Read)
Dim br As New IO.BinaryReader(fs)
' Move to the end of the stream - 127 places from the end...
br.BaseStream.Seek(-128, IO.SeekOrigin.End)
' Begin reading the data
_Tag = br.ReadChars(3)
' Does a tag exist?
If _Tag = "TAG" Then
' Read the rest of the data
_Songname = br.ReadChars(30)
If _Songname.Contains(Chr(0)) Then
_Songname = _Songname.Substring(0, _Songname.IndexOf(Chr(0)))
End If
_Artist = br.ReadChars(30)
If _Artist.Contains(Chr(0)) Then
_Artist = _Artist.Substring(0, _Artist.IndexOf(Chr(0)))
End If
_Album = br.ReadChars(30)
_Year = br.ReadChars(4)
_Comment = br.ReadChars(30)
_Genre = br.ReadChars(1)
_DetailsRead = True
Else
_DetailsRead = False
End If
' Cleanup
br.Close()
fs.Close()
br = Nothing
fs = Nothing
' Return value
Return _DetailsRead
End Function
#Region "Properities"
Public ReadOnly Property Songname() As String
Get
If _DetailsRead Then Return Me._Songname.ToString
End Get
End Property
Public ReadOnly Property Artist() As String
Get
If _DetailsRead Then Return Me._Artist.ToString
End Get
End Property
Public ReadOnly Property Album() As String
Get
If _DetailsRead Then Return Me._Album.ToString
End Get
End Property
Public ReadOnly Property Year() As String
Get
If _DetailsRead Then Return Me._Year.ToString
End Get
End Property
Public ReadOnly Property Comment() As String
Get
If _DetailsRead Then Return Me._Comment.ToString
End Get
End Property
Public ReadOnly Property Genre() As String
Get
If _DetailsRead Then Return Me._Genre.ToString
End Get
End Property
#End Region
End Class
Gary
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|