Link to home
Start Free TrialLog in
Avatar of Trancedified
TrancedifiedFlag for United States of America

asked on

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
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

What information are you trying to get?

Bob
Avatar of Trancedified

ASKER

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?
Avatar of wguerram
wguerram

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
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
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
SOLUTION
Avatar of wguerram
wguerram

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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.

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
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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
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
 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