[Sample] Playing with music file

Getting into My Music

If you decide to have your own interface for playing music, one of the fundamental decisions is how to manage your library of MP3/WMA files. Do you use the existing library of another program (such as Microsoft?Windows Media?Player), or do you write your own? This is not a particularly easy decision, and I am still not completely sure which is the best choice, but I had to make a decision or I would never be able to move on in my development. I decided that using a database to create my own library/catalog of music files would be the most flexible option, although this path would likely require more work at the beginning. I set up a new database in MSDE on my development machine, and created the following schema (actually it is a fair bit more complicated, but these are the key tables):

Click here for larger image.

Figure 1. A simple database schema for holding my music library

Of course, after building my own system, along came what is arguably a much better one in the form of Windows XP Media Center Edition, which created a bit of a conflict in my mind. Why continue building my own if I could go get a better one that is completely done for me? The confusion only lasted a few moments though; I am a programmer, so I often build things when it would be much more logical to buy them.

Reading the Tags

Although I didn't have any formal requirements or architectural documentation (maybe it's just me, but having to produce all the same documentation as a work project would take some of the fun out of it), I knew that moving to the next stage would require code that could read all of the attributes from an audio file梐rtist, song, and album names, for example.

I originally assumed my collection would be in MP3 files, which store attributes using various flavors of a system called "ID3," so using the resources available at www.id3.org, I started creating a Microsoft Windows .NET Framework-based library that could handle the more common versions of ID3 tags. In parallel, I began ripping my CD collection onto my hard drive (for my own personal use, of course). For this, I used Windows Media Player, and I spent some time investigating the various options available in terms of WMA (Windows Media Audio) versus MP3 and different encoding rates.

Note    Choosing a format and encoding quality requires some research, especially before you rip several hundred CDs to your hard drive. (That is not a task I would like to redo.) I won't spent a lot of time explaining why I chose WMA. I am not really an objective reviewer of audio file formats anyway, but it appears to produce higher quality at lower file sizes, which seems like a good thing to me. Starting with version 9 of Windows Media Player, a variable bit rate (VBR) encoding format is available, which might turn out to be the best choice, but I didn't have access to that format when I was ripping my collection to disk.

I ended up choosing WMA at around 160 kilobits per second (Kbps). Now I needed to make sure my attribute reading code could handle both WMA and MP3 files. Well, to make a long story somewhat shorter, I decided that since Windows Media Player could handle both file formats, I should take a look at the Windows Media SDK. Sure enough, the SDK even provided a sample to do exactly what I wanted. Instead of doing all my work for me and writing the sample as a COM component, however, it was a regular old console application.

I started trying to modify their wonderful C++ code to create a nice friendly component, but it was 3 A.M. and my patience was not up to the challenge. So (in a trend that continues across most of my hobby programming), I cheated and took the quick and dirty way out. I modified their code slightly to handle a larger list of MP3 and WMA tags, and to output the information (to the console still) as XML. With these changes in place, I wrapped the component up into a couple of Framework functions, essentially just calling it with a command-line parameter of the path to my audio file, and retrieving its output back as a nice XML document. Very cheesy, but at 3 A.M. I just wanted to get something working, and this works just fine.

Note    The code for the XML generating console application is almost 100 percent identical to the sample in the Windows Media SDK, but the exe is provided for you as part of the code download for this article. I've also included a sample bit of Framework code to call the console application and process its output, so that you can play around with your own MP3 and WMA files.

Using this program from the command line is fairly simple. You just specify the name of the file you want to have it scan. There are no options for multiple files, although that would likely be an excellent addition. Running this program against the WMA file of "Sophia's Pipes" by Ashley MacIssac produces this output to the console:

<Value>Hi How Are You Today?</Value></Tag>
<Value>Ashley MacIsaac</Value></Tag>
<Value>Sophia's Pipes (Walkin' the Floor/Murdo MacKenzie of Torridon)</Value></Tag>
<Value>Ashley MacIsaac, Pete Prilesnik, trad</Value></Tag>

Although this isn't the prettiest XML, it is valid, and therefore it is easy to read using the System.XML classes in the Framework. Running the console application and grabbing the output is made possible through the System.Diagnostics namespace and the Process and ProcessStartInfo classes.

Public Function RunID3Tags(ByVal filename As String) As String
    Dim psi As New ProcessStartInfo()
    psi.FileName = ID3Tag_Path
    psi.Arguments = String.Format( _
        " " & """" & "{0}" & """" & " show", filename)

    psi.UseShellExecute = False
    psi.RedirectStandardOutput = True
    psi.CreateNoWindow = True

    Dim p As Process
    Dim xmlOutput As String
    p = Process.Start(psi)
        xmlOutput = p.StandardOutput.ReadToEnd()
        p.WaitForExit() ' wait a 1/10th of a second
        Return xmlOutput
        ' should never happen, but let's play it safe here
        If Not p.HasExited Then
        End If
    End Try
End Function

Loading the XML

I do a quick little replace to make sure that any "&" characters in the music file tags are interpreted correctly by the XML classes. Then I load this XML in as a new XMLDocument.

Dim sXML As String
sXML = RunID3Tags(fileName)
sXML = sXML.Replace("&", "&amp;")
Dim myXML As New XmlDocument()

Once I have the XMLDocument, I create a new instance of a class (MusicFileInfo) to hold the attributes of the file and fill in the appropriate properties. The complete function that takes a filename and returns an instance of MusicFileInfo is provided below.

Private Function ScanFile(ByVal fileName As String) _
        As MusicFileInfo
    Dim mfi As New MusicFileInfo()

    Dim sXML As String
        sXML = RunID3Tags(fileName)
        sXML = sXML.Replace("&", "&amp;")
        Dim myXML As New XmlDocument()
        Dim myNode As XmlNode
        Dim sAttribute, sValue As String

        With mfi
            For Each myNode In _
                sAttribute = myNode.ChildNodes(0).InnerText
                sValue = myNode.ChildNodes(1).InnerText

                If sValue.Trim() <> String.Empty Then
                    Select Case sAttribute.ToLower
                        Case "bitrate"
                            .Bitrate = CLng(sValue)
                        Case "wm/albumtitle"
                            .AlbumTitle = sValue.Trim
                        Case "wm/genre"
                            .Genre = sValue.Trim
                        Case "author"
                            .Authors = sValue.Trim
                        Case "wm/track"
                            .Track = CInt(sValue)
                        Case "title"
                            .Title = sValue.Trim
                        Case "wm/year"
                            .Year = CInt(sValue)
                        Case "wm/composer"
                            .Composers = sValue.Trim
                        Case "duration"
                            .Duration = CDec(sValue.Trim)
                        Case "wm/mcdi"
                            .TOC = sValue.Trim
                    End Select
                End If

            If .AlbumTitle = "" Then
                .AlbumTitle = "No Specific Album"
                .TOC = ""
            End If
            'Track is zero based...
            'want it to be 1 based, 
            'to fit in with CD Players
            .Track += 1
        End With
    Catch e As Exception
        sXML = ""
    End Try

    Return mfi
End Function

In addition to creating the MusicFileInfo class, I also used the Collection Generator tool from GotDotNet to create a strongly typed collection of MusicFileInfo objects. As I scan in music files, I add their information to this collection and then, because it is a strongly typed collection, I can use it to data bind a grid control.

Public Function ScanFiles(ByVal fileNames() As String) _
        As MusicFileInfoCollection
    Dim fileName As String
    Dim mfis As New MusicFileInfoCollection()

    For Each fileName In fileNames
    Return mfis
End Function

The final sample application (included in the download for this column) produces the results shown in Figure 2.

Figure 2. The demo MusicFileTags application displays the tags of any WMA/MP3 files you select.

Obsolete Already

It is nice to have code finished and working, but things keep changing in the world of computers, and new options often present themselves just after you have finished your solution. Early this month, January 2003, Microsoft released a new version of Windows Media Player (available from http://www.windowsmedia.com/9series/download/download.asp) and a new version of the Windows Media Format SDK (available from http://msdn.microsoft.com/downloads/list/winmedia.asp). Once I downloaded the new player and the new SDK, I found something wonderful: managed code samples for reading music file attributes. My current solution, reading XML output from a console application, works for me (and in fact I haven't replaced it yet) but the managed code option is much more appealing. The nature of strings, special characters (&, <, >, and so on), and XML means that the original system works for all my current music. Nevertheless, I expect it will eventually find some artist, album, or song name it cannot process successfully. Using a managed-code solution avoids those issues, and also allows for structured error handling (among other benefits).

Included in the SDK's managed code samples is a simple wrapper library, which encapsulates the underlying Windows Media calls in C# code. This wrapper makes it much easier to use those calls from a Microsoft Windows .NET Framework-based application. I have included only the compiled version of that library (in the bin directory of my sample project), but you can get the complete source code by downloading the Format SDK for yourself. Now that all of the Windows Media calls are handled by this wrapper library, the next step is to create a simple class that accepts the file path for a WMA file and retrieves all of the available attributes.

Public Shared Function GetAttributes( _
        ByVal filename As String) As MusicFileInfo
    Dim mfi As New MusicFileInfo()

    Dim editor As IWMMetadataEditor
    Dim headerInfo As IWMHeaderInfo3
    Dim uHR As UInt32
    Dim hr As Int32
    uHR = WMFSDKFunctions.WMCreateEditor(editor)
    hr = Convert.ToInt32(uHR)
    If hr = 0 Then
        uHR = editor.Open(filename)
        hr = Convert.ToInt32(uHR)
        If hr = 0 Then
            headerInfo = DirectCast(editor, IWMHeaderInfo3)
            Dim value As Byte()
            Dim pType As WMT_ATTR_DATATYPE
            value = GetAttributeByName(headerInfo, _
                "bitrate", pType)
            If pType = WMT_ATTR_DATATYPE.WMT_TYPE_DWORD Then
                mfi.Bitrate = Convert.ToInt64( _
                    BitConverter.ToUInt32(value, 0))
            End If
            value = GetAttributeByName(headerInfo, _
                "WM/AlbumTitle", pType)
                'ConvertToString is a function to convert 
                'from byte array to String, taking 
                'unicode encoding into account
                mfi.AlbumTitle = ConvertToString(value)
            End If
            value = GetAttributeByName(headerInfo, _
                "WM/Genre", pType)
                mfi.Genre = ConvertToString(value)
            End If
            value = GetAttributeByName(headerInfo, _
                "Author", pType)
                mfi.Authors = ConvertToString(value)
            End If
            value = GetAttributeByName(headerInfo, _
                "WM/Track", pType)
                mfi.Track = CInt(ConvertToString(value))
            End If
            value = GetAttributeByName(headerInfo, _
                "Title", pType)
                mfi.Title = ConvertToString(value)
            End If
            value = GetAttributeByName(headerInfo, _
                "WM/Year", pType)
                mfi.Year = CInt(ConvertToString(value))
            End If
            value = GetAttributeByName(headerInfo, _
                "WM/Composer", pType)
                mfi.Composers = ConvertToString(value)
            End If
            value = GetAttributeByName(headerInfo, _
                "Duration", pType)
            If pType = WMT_ATTR_DATATYPE.WMT_TYPE_QWORD Then
                mfi.Duration = Convert.ToDecimal( _
                    BitConverter.ToUInt64(value, 0))
            End If
            value = GetAttributeByName(headerInfo, _
                "WM/MCDI", pType)
                mfi.TOC = BitConverter.ToString(value, 0)
            End If
        End If
    End If
    Return mfi
End Function

Private Shared Function GetAttributeByName( _
        ByVal headerInfo As IWMHeaderInfo3, _
        ByVal name As String, _
        ByRef pType As WMT_ATTR_DATATYPE) As Byte()
    Dim streamNum As UInt16 = Convert.ToUInt16(0)
    Dim uHR As UInt32
    Dim valueLength As UInt16
    Dim arrLength As Int32
    Dim hr As Int32
    uHR = headerInfo.GetAttributeByName( _
        streamNum, name, pType, Nothing, valueLength)

    hr = Convert.ToInt32(uHR)
    If hr = 0 Then
        arrLength = Convert.ToInt32(valueLength)
        Dim value(arrLength) As Byte
        uHR = headerInfo.GetAttributeByName( _
            streamNum, name, pType, value, valueLength)
        hr = Convert.ToInt32(uHR)
        If hr = 0 Then
            Return value
        End If
    End If
End Function

Note    Reading through this code (and the full code available in the download), you will find quite a few conversions between unsigned integers (UInt16, UInt32, and UInt64) and signed integers (Integer/Int32, Long/Int64). The wrapper supplied with the SDK is not CLS-compliant, which means it is not easy to use from outside of C#. I could have just used C# to write my new application, but I almost always use Microsoft?Visual Basic?.NET, and it was worth a little bit of extra code to stay within my language of choice. Note that, in most cases, these conversions could be done within the wrapper itself, ensuring that it was CLS-compliant and easy to use from any Framework language.

As you can see, the availability of the managed code wrapper makes it very easy to use the Windows Media libraries from the Framework. In time I will rewrite my music system to use this wrapper for reading file attributes, but for now I have just created a version of the first sample that uses the new managed wrapper. Other than the actual attribute reading code, the rest of the application is unchanged.

Coding Challenge

At the end of some of my Coding4Fun columns, I will have a little coding challenge梥omething for you to work on if you are interested. For this first article, the challenge is to build your own application that loads and uses MP3/WMA attributes (using my sample as a starting point if you wish). Try to create an application that uses these attributes in some interesting and/or useful way, or create your own code for loading the attributes as an alternative to using the Windows Media libraries. Of course, Framework code is preferred (Visual Basic .NET, C#, or Managed C++ please), but an unmanaged component that exposes a COM interface would also be good.

Just post whatever you produce to the User Samples area of GotDotNet and send me an e-mail message (at duncanma@microsoft.com) with an explanation of what you have done and why you feel it is interesting. I'd appreciate it if you would include a quick blurb on yourself as well, so that I can tell the world about you if I pick your code to mention in an article. Sorry, there are no prizes, but I will respond to the most interesting code I receive, and you will gain the respect and admiration of your peers by posting onto GotDotNet. As far as other submission guidelines, I will follow the rules set forth on the GotDotNet site as our terms of use for your code. (You will need to logon with your Microsoft?Passport credentials to view the upload agreement.) You can send in submissions whenever you like (but always through GotDotNet's upload system please; I don't want my inbox to fill up with code samples!). I am always interested in seeing what code programmers create when they aren't being paid. On that note, check out this cool Framework game Miner Arena, written by some programmers at the Budapest University of Technology and Economics in Hungary. The setup of Miner Arena isn't exactly simple, but the depth of code and architecture explored in this game makes it worth digging into.


If you want to build your own music playing system, then you need to be able to read the attributes of your music files, as demonstrated in the code for this article. In future articles, I will continue with the music player examples, but I will also cover a few other topics, such as games, graphics, network communication, and more. Have your own ideas for hobbyist content? Let me know at duncanma@microsoft.com, and happy coding!



Duncan Mackenzie is the Microsoft Visual Basic .NET Content Strategist for MSDN during the day, and a dedicated coder late at night. It has been suggested that he wouldn't be able to do any work at all without his Earl Grey tea, but let's hope we never have to find out. For more on Duncan, see his profile on GotDotNet.

  • 0
  • 0
    觉得还不错? 一键收藏
  • 打赏
  • 0
Certainly, here's an example program that generates an RSA key pair, creates an X.509 certificate with the public key, and writes the key pair and certificate to files: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include "mbedtls/pk.h" #include "mbedtls/entropy.h" #include "mbedtls/ctr_drbg.h" #include "mbedtls/x509_crt.h" #include "mbedtls/x509_csr.h" #include "mbedtls/x509write_crt.h" #define KEY_SIZE 2048 #define CERT_DAYS 365 int main(int argc, char *argv[]) { int ret = 1; mbedtls_pk_context key; mbedtls_entropy_context entropy; mbedtls_ctr_drbg_context ctr_drbg; mbedtls_x509write_crt crt; mbedtls_x509_crt cacert; char key_file[] = "key.pem"; char cert_file[] = "cert.pem"; char cacert_file[] = "cacert.pem"; // Initialize contexts mbedtls_pk_init(&key); mbedtls_entropy_init(&entropy); mbedtls_ctr_drbg_init(&ctr_drbg); mbedtls_x509write_crt_init(&crt); mbedtls_x509_crt_init(&cacert); // Seed the random number generator ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, NULL, 0); if (ret != 0) { printf("error: mbedtls_ctr_drbg_seed returned %d\n", ret); goto exit; } // Generate an RSA key pair ret = mbedtls_pk_setup(&key, mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)); if (ret != 0) { printf("error: mbedtls_pk_setup returned %d\n", ret); goto exit; } ret = mbedtls_rsa_gen_key(mbedtls_pk_rsa(key), mbedtls_ctr_drbg_random, &ctr_drbg, KEY_SIZE, 65537); if (ret != 0) { printf("error: mbedtls_rsa_gen_key returned %d\n", ret); goto exit; } // Write the key pair to a file ret = mbedtls_pk_write_key_pem(&key, key_file, NULL, NULL, 0); if (ret != 0) { printf("error: mbedtls_pk_write_key_pem returned %d\n", ret); goto exit; } // Set up the X.509 certificate mbedtls_x509write_crt_set_version(&crt, MBEDTLS_X509_CRT_VERSION_3); mbedtls_x509write_crt_set_md_alg(&crt, MBEDTLS_MD_SHA256); mbedtls_x509write_crt_set_subject_name(&crt, "CN=example.com,O=Example Organization,C=US"); mbedtls_x509write_crt_set_issuer_name(&crt, "CN=example.com,O=Example Organization,C=US"); mbedtls_x509write_crt_set_validity(&crt, "20200101000000", "20301231235959"); mbedtls_x509write_crt_set_basic_constraints(&crt, 0, -1); mbedtls_x509write_crt_set_key_usage(&crt, MBEDTLS_X509_KU_DIGITAL_SIGNATURE | MBEDTLS_X509_KU_KEY_CERT_SIGN); mbedtls_x509write_crt_set_ns_cert_type(&crt, MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT | MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER | MBEDTLS_X509_NS_CERT_TYPE_EMAIL); mbedtls_x509write_crt_set_subject_key(&crt, &key); mbedtls_x509write_crt_set_issuer_key(&crt, &key); // Write the X.509 certificate to a file ret = mbedtls_x509write_crt_pem(&crt, cert_file, NULL, NULL, 0); if (ret != 0) { printf("error: mbedtls_x509write_crt_pem returned %d\n", ret); goto exit; } // Load the trusted CA certificate ret = mbedtls_x509_crt_parse_file(&cacert, cacert_file); if (ret != 0) { printf("error: mbedtls_x509_crt_parse_file returned %d\n", ret); goto exit; } // Verify the X.509 certificate against the trusted CA certificate ret = mbedtls_x509_crt_verify(&crt, &cacert, NULL, NULL, NULL); if (ret != 0) { printf("error: mbedtls_x509_crt_verify returned %d\n", ret); goto exit; } printf("RSA key pair and X.509 certificate generated successfully!\n"); exit: mbedtls_x509_crt_free(&cacert); mbedtls_x509write_crt_free(&crt); mbedtls_ctr_drbg_free(&ctr_drbg); mbedtls_entropy_free(&entropy); mbedtls_pk_free(&key); return ret; } ``` This program uses the mbedtls `pk` module to generate an RSA key pair, the `ctr_drbg` module to generate random numbers for the key pair, and the `x509write_crt` module to create an X.509 certificate with the public key. It also loads a trusted CA certificate and verifies the generated certificate against it. The key pair and certificate are written to files `key.pem` and `cert.pem`, respectively.


  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助




当前余额3.43前往充值 >
领取后你会自动成为博主和红包主的粉丝 规则




¥1 ¥2 ¥4 ¥6 ¥10 ¥20



钱包余额 0