Parsing .wav files using Visual Basic .net and Windows Media Player.

Posted on 2005-04-26
Last Modified: 2012-06-22
I have Windows XP professional and Visual Professional version 2003 software.
Using Visual Basic .net I added a form and embedded the Windows Media Player version 9 control.
I am able to play back .wav files but what I'd like to do is be able to parse a .wav (or any media file) by time.  

For example:  If I had a .wav file that was 4 seconds long and I wanted to parse out 2-3 seconds in the middle and make that a separate file (without damaging the orignal .wav file).  How would I do that?

Thank you for any help you can give me.
Question by:tjgilbert
    1 Comment
    LVL 27

    Accepted Solution

    Here is a sample on playing parts of a file, might help in separating the file....

    'FORM 1

    Public Class frmPlayApart
        Inherits System.Windows.Forms.Form
        Private Declare Function mciGetErrorString Lib "MMSYSTEM.DLL" (ByVal errornum As Integer, ByVal ErrorString As String, ByVal errorsize As Short) As Short
        Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Integer, ByVal hwndCallback As Integer) As Integer
        Private voicefile As String = "c:\waves\MYWAVE.wav"  '<---Wave file used for sound

    #Region " Windows Form Designer generated code "

        Public Sub New()

            'This call is required by the Windows Form Designer.

            'Add any initialization after the InitializeComponent() call

        End Sub

        'Form overrides dispose to clean up the component list.
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If Not (components Is Nothing) Then
                End If
            End If
        End Sub

        'Required by the Windows Form Designer
        Private components As System.ComponentModel.IContainer

        'NOTE: The following procedure is required by the Windows Form Designer
        'It can be modified using the Windows Form Designer.  
        'Do not modify it using the code editor.
        Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
        Friend WithEvents rbn1 As System.Windows.Forms.RadioButton
        Friend WithEvents rbn2 As System.Windows.Forms.RadioButton
        Friend WithEvents rbn3 As System.Windows.Forms.RadioButton
        Friend WithEvents rbn4 As System.Windows.Forms.RadioButton
        Friend WithEvents Button1 As System.Windows.Forms.Button
        Friend WithEvents Button2 As System.Windows.Forms.Button
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
            Me.GroupBox1 = New System.Windows.Forms.GroupBox
            Me.rbn4 = New System.Windows.Forms.RadioButton
            Me.rbn3 = New System.Windows.Forms.RadioButton
            Me.rbn2 = New System.Windows.Forms.RadioButton
            Me.rbn1 = New System.Windows.Forms.RadioButton
            Me.Button1 = New System.Windows.Forms.Button
            Me.Button2 = New System.Windows.Forms.Button
            Me.GroupBox1.Location = New System.Drawing.Point(24, 20)
            Me.GroupBox1.Name = "GroupBox1"
            Me.GroupBox1.Size = New System.Drawing.Size(184, 160)
            Me.GroupBox1.TabIndex = 0
            Me.GroupBox1.TabStop = False
            Me.GroupBox1.Text = "GroupBox1"
            Me.rbn4.Location = New System.Drawing.Point(12, 108)
            Me.rbn4.Name = "rbn4"
            Me.rbn4.Size = New System.Drawing.Size(116, 24)
            Me.rbn4.TabIndex = 3
            Me.rbn4.Text = "Play 700 to 1000"
            Me.rbn3.Location = New System.Drawing.Point(12, 80)
            Me.rbn3.Name = "rbn3"
            Me.rbn3.TabIndex = 2
            Me.rbn3.Text = "Play 400 to 600"
            Me.rbn2.Location = New System.Drawing.Point(12, 52)
            Me.rbn2.Name = "rbn2"
            Me.rbn2.TabIndex = 1
            Me.rbn2.Text = "Play 100 to 200"
            Me.rbn1.Location = New System.Drawing.Point(12, 20)
            Me.rbn1.Name = "rbn1"
            Me.rbn1.TabIndex = 0
            Me.rbn1.Text = "Play 0 to 100"
            Me.Button1.Location = New System.Drawing.Point(32, 204)
            Me.Button1.Name = "Button1"
            Me.Button1.TabIndex = 4
            Me.Button1.Text = "Start"
            Me.Button2.Location = New System.Drawing.Point(136, 204)
            Me.Button2.Name = "Button2"
            Me.Button2.TabIndex = 4
            Me.Button2.Text = "End"
            Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
            Me.ClientSize = New System.Drawing.Size(252, 273)
            Me.Name = "Form2"
            Me.Text = "Form2"

        End Sub

    #End Region
        Private Function ReadWaveArea(ByVal digit As Short) As Short
            Dim x As Integer

            Select Case digit
                Case 0 : x = mciSendString("play voice from 0 to 100 wait", CStr(0), 0, 0)
                Case 1 : x = mciSendString("play voice from 100 to 200 wait", CStr(0), 0, 0)
                Case 2 : x = mciSendString("play voice from 400 to 600 wait", CStr(0), 0, 0)
                Case 3 : x = mciSendString("play voice from 700 to 1000 wait", CStr(0), 0, 0)
            End Select
        End Function
        Private Sub InitializeWave()
            Dim sCmd As String
            Dim x, r As Integer
            Dim StrLen As Short
            Dim ErrorString As String

            FileOpen(1, voicefile, OpenMode.Binary)
            On Error GoTo 0
            sCmd = "open " & voicefile & " type waveaudio alias voice"
            x = mciSendString(sCmd, CStr(0), 0, 0)
            If x <> 0 Then
                StrLen = 255
                r = mciGetErrorString(x, ErrorString, StrLen)
            End If

        End Sub

        Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim res As Short
            Dim x As Integer

            If rbn1.Checked = True Then
                res = ReadWaveArea(0)
            ElseIf rbn2.Checked = True Then
                res = ReadWaveArea(1)
            ElseIf rbn3.Checked = True Then
                res = ReadWaveArea(2)
            ElseIf rbn4.Checked = True Then
                res = ReadWaveArea(3)
            End If
            x = mciSendString("close voice", CStr(0), 0, 0)
        End Sub

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Article by: jpaulino
    XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String ( Literal, only instead of starting and ending with w…
    It’s quite interesting for me as I worked with Excel using for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now