• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 568
  • Last Modified:

Importing .WMA file into VB

Hello,

I have a .WMA file and if you open it with notepad you can see some header information like the date, time started, time ended etc.
Is there a way to import this file into VB kind of like notepad and copy it to a listbox? The text looks kinda like this:

0&²uŽfϦ٠ª bÎlÎ         @¤ÐÒãÒ—ð  É^¨P        O L Y M P U S    âdss     4692                þÿÿÿÿÿ040629115015040629115019000004ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ, ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿj  ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ  

I need to grab this:

040629115015040629115019000004

Thanks for your tips!

Chris
0
Trancedified
Asked:
Trancedified
  • 6
  • 5
  • 4
2 Solutions
 
Bob LearnedCommented:
What information are you trying to get?

Bob
0
 
TrancedifiedAuthor Commented:
Hey Bob,

Nice to hear from you again!
This piece:

O L Y M P U S    âdss     4692    
Has an ID number I want to extract (4692)

Ok this piece has the year, month day, plus the time this .wma file was created, and when it ended. Here's the format:
040629115015040629115019000004
Translated:
04 (year)
06 (month)
29 (day)

Created: 11:50:15
Duration: 040629 (seconds? milliseconds?)
Ended: 11:50:19 (so this audio file lasted for 4 seconds)

000004 (ignore it)

I tried using the below code to load the text into a textbox but it only gave me:
0&uf

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Declare any variables
        Dim sFileName As String = "C:\Documents and Settings\Chris\My Documents\Chris\Work\WMA\Samples\46920032.wma"
        Dim myFileStream As New System.IO.FileStream(sFileName, _
            FileMode.OpenOrCreate, FileAccess.Read, FileShare.Read)

        'Create the StreamReader and associate the filestream with it
        Dim myReader As New System.IO.StreamReader(myFileStream)

        'Read the entire text, and set it to a string
        Dim sFileContents As String = myReader.ReadToEnd

        'Print it to the textbox
        txtFileDisplay.text = sFileContents
End Sub

I was hoping to get exactly what I had above if I opened it up in notepad:

0&²uŽfϦ٠ª bÎlÎ         @¤ÐÒãÒ—ð  É^¨P        O L Y M P U S    âdss     4692                þÿÿÿÿÿ040629115015040629115019000004ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ, ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿj  ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ  

But it didn't work, any ideas?
0
 
wguerramCommented:
You have to import
Imports System.io
Imports System.Text

'This code reads the first file 30 lines of the file and converts the lines to ASCII chars in order to be easy to read them


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
         Dim sFileName As String = "C:\Documents and Settings\Chris\My Documents\Chris\Work\WMA\Samples\46920032.wma"

        Dim ascii As Encoding = Encoding.ASCII
        Dim [unicode] As Encoding = Encoding.Unicode

        ' Convert the string into a byte[].
        Dim unicodeBytes As Byte()

        ' Perform the conversion from one encoding to the other.
        Dim asciiBytes As Byte()        

        ' Convert the new byte[] into a char[] and then into a string.
        ' This is a slightly different approach to converting to illustrate
        ' the use of GetCharCount/GetChars.
        Dim asciiChars() As Char        

        Dim fr As StreamReader
        Dim sbr As New StringBuilder
        Dim FileString As String
        FileString = ""

            Try
                Dim i As Integer
                fr = New StreamReader(sFileName)
                For i = 0 To 30
                    FileString = fr.ReadLine
                    If Not IsNothing(FileString) Then
                        unicodeBytes = [unicode].GetBytes(FileString)
                        asciiBytes = Encoding.Convert([unicode], ascii, unicodeBytes)
                        ReDim asciiChars(ascii.GetCharCount(asciiBytes, 0, asciiBytes.Length))
                        ascii.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0)

                        sbr.Append(asciiChars)                        
                    Else
                        Exit For
                    End If
                Next
            Finally
                fr.Close()
            End Try

             txtFileDisplay.Text = sbr.ToString
End Sub
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
TrancedifiedAuthor Commented:
wguerram,

Thanks for trying, it gave me the same result:
0&uf

Check this out though:

1) If I converted the .wma file to a .txt, opened it up in notepad
2) Deleted the first line in notepad
3) Save it (keep notepad open)

4) Run the code again, it reads into the textbox perfectly
5) Go back to notepad undo the delete
6) Run the code again, it reads into the textbox

VERY VERY VERY weird huh? Do you know why?

Chris
0
 
wguerramCommented:
This shoul do it

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
         Dim sFileName As String = "C:\Documents and Settings\Chris\My Documents\Chris\Work\WMA\Samples\46920032.wma"

        'Dim ascii As Encoding = Encoding.ASCII
        Dim [unicode] As Encoding = Encoding.Unicode

        ' Convert the string into a byte[].
        Dim unicodeBytes As Byte()

        ' Perform the conversion from one encoding to the other.
        'Dim asciiBytes As Byte()        

        ' Convert the new byte[] into a char[] and then into a string.
        ' This is a slightly different approach to converting to illustrate
        ' the use of GetCharCount/GetChars.
        'Dim asciiChars() As Char        

        Dim fr As StreamReader
        Dim sbr As New StringBuilder
        Dim FileString As String
        FileString = ""

        Try
            Dim i As Integer
            fr = New StreamReader(sFileName)
            For i = 0 To 30
                FileString = fr.ReadLine
                If Not IsNothing(FileString) Then
                    unicodeBytes = [unicode].GetBytes(FileString)
                    'asciiBytes = Encoding.Convert([unicode], ascii, unicodeBytes)
                    'ReDim asciiChars(ascii.GetCharCount(asciiBytes, 0, asciiBytes.Length))
                    'ascii.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0)
                    sbr.Append([unicode].GetChars(unicodeBytes))
                Else
                    Exit For
                End If
            Next
        Finally
            fr.Close()
        End Try

        txtFileDisplay.Text = sbr.ToString
End sub
0
 
Bob LearnedCommented:
So, what is the most important information here?  

You can get a lot of information very easily using the Windows Media Player v.9 COM control:

WM/Year
WM/TrackNumber
WM/Lyrics
WM/Genre
WM/Composer
WM/AlbumTitle
Title
SourceURL
Is_Protected
FileSize
Duration
CurrentBitrate
Author

durationstring

Bob
0
 
TrancedifiedAuthor Commented:
Bob,

I'm trying the Windows Media Player approach I got to this page:
http://www.msdn.microsoft.com/library/default.asp?url=/downloads/list/winmedia.asp

But which one do I download? I got the Platform SDK and it seems it's more for the server side for your website to play media files?

This one is for C++ or C#, is there anything for VB.NET?
http://www.microsoft.com/downloads/details.aspx?FamilyID=e43cbe59-678a-458a-86a7-ff1716fad02f&DisplayLang=en

Thanks for the tip!

wguerram,
The code produced the same results.

0
 
wguerramCommented:
0
 
TrancedifiedAuthor Commented:
OK after installing it, going to Programs > Microsoft Visual Studio.NET 2003 > Visual Studio.NET Tools > Visual Studio.NET 2003 Command Prompt

Paste this in:
regasm C:\WMSDK\WMPSDK9\redist\wmppia.dll
<Successful>

Paste this in:
Gacutil /i C:\WMSDK\WMPSDK9\redist\wmppia.dll
<Successful>

Create a new C# windows app, go to the toolbox > COM components tab > Windows Media Player (click the checkbox) > OK
Drag the mediaplayer to the form then I get the error message:

"One or more of the types in the assembly unable to load."
What can I do now?

Chris
0
 
Bob LearnedCommented:
I do this in VB6 all the time.  Download the Windows Media Player 9 from Micro$oft.  Add a COM reference to the Windows Media Player type library (wmp.dll).  

A COM wrapper will be created, called WMPLib.  

Add the following code:

  Public Structure WMAInformation
    Public Duration As String
    Public Author As String
    Public CurrentBitRate As String
    Public FileSize As String
    Public Is_Protected As Boolean
    Public SourceURL As String
    Public Title As String
    Public WM_AlbumTitle As String
    Public WM_Composer As String
    Public WM_Genre As String
    Public WM_Lyrics As String
    Public WM_TrackNumber As Integer
    Public WM_Year As Integer
  End Structure 'WMAInformation'


  Public Sub ReadWMAInformation(ByVal pathMedia As String, ByRef fileInfo As WMAInformation)

    Try

      Dim playerOpen As WMPLib.WindowsMediaPlayer = New WMPLib.WindowsMediaPlayer
      Dim mediaFile As WMPLib.IWMPMedia = playerOpen.newMedia(pathMedia)

      fileInfo.Duration = mediaFile.durationString

      fileInfo.Author = mediaFile.getItemInfo("Author")
      fileInfo.CurrentBitRate = mediaFile.getItemInfo("CurrentBitRate")
      fileInfo.FileSize = mediaFile.getItemInfo("FileSize")
      fileInfo.Is_Protected = mediaFile.getItemInfo("Is_Protected")
      fileInfo.SourceURL = mediaFile.getItemInfo("SourceURL")
      fileInfo.Title = mediaFile.getItemInfo("Title")
      fileInfo.WM_AlbumTitle = mediaFile.getItemInfo("WM/AlbumTitle")
      fileInfo.WM_Composer = mediaFile.getItemInfo("WM/Composer")
      fileInfo.WM_Genre = mediaFile.getItemInfo("WM/Genre")
      fileInfo.WM_Lyrics = mediaFile.getItemInfo("WM/Lyrics")
      fileInfo.WM_TrackNumber = mediaFile.getItemInfo("WM/TrackNumber")
      fileInfo.WM_Year = mediaFile.getItemInfo("WM/Year")

    Catch ex As Exception

      MessageBox.Show(ex.ToString)

    End Try

  End Sub 'ReadWMAInformation'

Bob
0
 
TrancedifiedAuthor Commented:
Wow getting closer:

So if I was to use your sub, ReadWMAInformation:
ReadWMAInformation("C:\Documents and Settings\Chris\My Documents\Chris\Work\WMA.NET\Samples\46920033.wma", ) <-----

What is inputted in the second parameter?

Chris
0
 
Bob LearnedCommented:
This is what I have as a test:

    Dim fileInfo As WMAInformation = New WMAInformation

    ReadWMAInformation("C:\Documents and Settings\All Users\Documents\My Music\Sample Music\New Stories (Highway Blues).wma", fileInfo)

Bob
0
 
TrancedifiedAuthor Commented:
Bob, I got it working.

Is there a way to retrieve the Date of Creation & Time of creation?
(easiest way is right clicking the file > properties, but I want to do it through code)

Chris
0
 
Bob LearnedCommented:
 Public Function GetFileCreationTime(ByVal pathFile As String) As Date

    Dim dateCreation As Date

    Try

      Dim infoSelectedFile As System.IO.FileInfo = New System.IO.FileInfo(pathFile)

      dateCreation = infoSelectedFile.CreationTime

    Catch ex As Exception

      MessageBox.Show(ex.ToString)

    End Try

    Return dateCreation

  End Function 'GetFileCreationTime'

Bob
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

  • 6
  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now