Advertisement
Taximaniac

Audio Class for Playing Audio Files in vb.net

Dec 3rd, 2016
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 7.54 KB | None | 0 0
  1. ''' <summary>
  2. ''' This class is a wrapper for the Windows API calls to play wave, midi or mp3 files.
  3. ''' </summary>
  4. ''' <remarks>
  5. ''' </remarks>
  6. Public Class AudioFile
  7.     '***********************************************************************************************************
  8.     '        Class:  PlayFile
  9.     '   Written By:  Blake Pell (bpell@indiana.edu)
  10.     ' Initial Date:  03/31/2007
  11.     ' Last Updated:  02/04/2009
  12.     '***********************************************************************************************************
  13.  
  14.     ' Windows API Declarations
  15.     Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Int32, ByVal hwndCallback As Int32) As Int32
  16.  
  17.     ''' <summary>
  18.     ''' Constructor:  Location is the filename of the media to play.  Wave files and Mp3 files are the supported formats.
  19.     ''' </summary>
  20.     ''' <param name="Location"></param>
  21.     ''' <remarks></remarks>
  22.     Public Sub New(ByVal location As String)
  23.         Me.Filename = location
  24.     End Sub
  25.  
  26.     ''' <summary>
  27.     ''' Plays the file that is specified as the filename.
  28.     ''' </summary>
  29.     ''' <remarks></remarks>
  30.     Public Sub Play()
  31.  
  32.         If _filename = "" Or Filename.Length <= 4 Then Exit Sub
  33.  
  34.         Select Case Right(Filename, 3).ToLower
  35.             Case "mp3"
  36.                 mciSendString("open """ & _filename & """ type mpegvideo alias audiofile", Nothing, 0, IntPtr.Zero)
  37.  
  38.                 Dim playCommand As String = "play audiofile from 0"
  39.  
  40.                 If _wait = True Then playCommand += " wait"
  41.  
  42.                 mciSendString(playCommand, Nothing, 0, IntPtr.Zero)
  43.             Case "wav"
  44.                 mciSendString("open """ & _filename & """ type waveaudio alias audiofile", Nothing, 0, IntPtr.Zero)
  45.                 mciSendString("play audiofile from 0", Nothing, 0, IntPtr.Zero)
  46.             Case "mid", "idi"
  47.                 mciSendString("stop midi", "", 0, 0)
  48.                 mciSendString("close midi", "", 0, 0)
  49.                 mciSendString("open sequencer!" & _filename & " alias midi", "", 0, 0)
  50.                 mciSendString("play midi", "", 0, 0)
  51.             Case Else
  52.                 Throw New Exception("File type not supported.")
  53.                 Call Close()
  54.         End Select
  55.  
  56.         IsPaused = False
  57.  
  58.     End Sub
  59.  
  60.     ''' <summary>
  61.     ''' Pause the current play back.
  62.     ''' </summary>
  63.     ''' <remarks></remarks>
  64.     Public Sub Pause()
  65.         mciSendString("pause audiofile", Nothing, 0, IntPtr.Zero)
  66.         IsPaused = True
  67.     End Sub
  68.  
  69.     ''' <summary>
  70.     ''' Resume the current play back if it is currently paused.
  71.     ''' </summary>
  72.     ''' <remarks></remarks>
  73.     Public Sub [Resume]()
  74.         mciSendString("resume audiofile", Nothing, 0, IntPtr.Zero)
  75.         IsPaused = False
  76.     End Sub
  77.  
  78.     ''' <summary>
  79.     ''' Stop the current file if it's playing.
  80.     ''' </summary>
  81.     ''' <remarks></remarks>
  82.     Public Sub [Stop]()
  83.         mciSendString("stop audiofile", Nothing, 0, IntPtr.Zero)
  84.     End Sub
  85.  
  86.     ''' <summary>
  87.     ''' Close the file.
  88.     ''' </summary>
  89.     ''' <remarks></remarks>
  90.     Public Sub Close()
  91.         mciSendString("close audiofile", Nothing, 0, IntPtr.Zero)
  92.     End Sub
  93.  
  94.     Private _wait As Boolean = False
  95.     ''' <summary>
  96.     ''' Halt the program until the .wav file is done playing.  Be careful, this will lock the entire program up until the
  97.     ''' file is done playing.  It behaves as if the Windows Sleep API is called while the file is playing (and maybe it is, I don't
  98.     ''' actually know, I'm just theorizing).  :P
  99.     ''' </summary>
  100.     ''' <value></value>
  101.     ''' <returns></returns>
  102.     ''' <remarks></remarks>
  103.     Public Property Wait() As Boolean
  104.         Get
  105.             Return _wait
  106.         End Get
  107.         Set(ByVal value As Boolean)
  108.             _wait = value
  109.         End Set
  110.     End Property
  111.  
  112.     ''' <summary>
  113.     ''' Sets the audio file's time format via the mciSendString API.
  114.     ''' </summary>
  115.     ''' <value></value>
  116.     ''' <returns></returns>
  117.     ''' <remarks></remarks>
  118.     ReadOnly Property Milleseconds() As Integer
  119.         Get
  120.             Dim buf As String = Space(255)
  121.             mciSendString("set audiofile time format milliseconds", Nothing, 0, IntPtr.Zero)
  122.             mciSendString("status audiofile length", buf, 255, IntPtr.Zero)
  123.  
  124.             buf = Replace(buf, Chr(0), "") ' Get rid of the nulls, they muck things up
  125.  
  126.             If buf = "" Then
  127.                 Return 0
  128.             Else
  129.                 Return CInt(buf)
  130.             End If
  131.         End Get
  132.     End Property
  133.  
  134.     ''' <summary>
  135.     ''' Gets the status of the current playback file via the mciSendString API.
  136.     ''' </summary>
  137.     ''' <value></value>
  138.     ''' <returns></returns>
  139.     ''' <remarks></remarks>
  140.     ReadOnly Property Status() As String
  141.         Get
  142.             Dim buf As String = Space(255)
  143.             mciSendString("status audiofile mode", buf, 255, IntPtr.Zero)
  144.             buf = Replace(buf, Chr(0), "")  ' Get rid of the nulls, they muck things up
  145.             Return buf
  146.         End Get
  147.     End Property
  148.  
  149.     ''' <summary>
  150.     ''' Gets the file size of the current audio file.
  151.     ''' </summary>
  152.     ''' <value></value>
  153.     ''' <returns></returns>
  154.     ''' <remarks></remarks>
  155.     ReadOnly Property FileSize() As Integer
  156.         Get
  157.             Try
  158.                 Return My.Computer.FileSystem.GetFileInfo(_filename).Length
  159.             Catch ex As Exception
  160.                 Return 0
  161.             End Try
  162.         End Get
  163.     End Property
  164.  
  165.     ''' <summary>
  166.     ''' Gets the channels of the file via the mciSendString API.
  167.     ''' </summary>
  168.     ''' <value></value>
  169.     ''' <returns></returns>
  170.     ''' <remarks></remarks>
  171.     ReadOnly Property Channels() As Integer
  172.         Get
  173.             Dim buf As String = Space(255)
  174.             mciSendString("status audiofile channels", buf, 255, IntPtr.Zero)
  175.  
  176.             If IsNumeric(buf) = True Then
  177.                 Return CInt(buf)
  178.             Else
  179.                 Return -1
  180.             End If
  181.         End Get
  182.     End Property
  183.  
  184.     ''' <summary>
  185.     ''' Used for debugging purposes.
  186.     ''' </summary>
  187.     ''' <value></value>
  188.     ''' <returns></returns>
  189.     ''' <remarks></remarks>
  190.     ReadOnly Property Debug() As String
  191.         Get
  192.             Dim buf As String = Space(255)
  193.             mciSendString("status audiofile channels", buf, 255, IntPtr.Zero)
  194.  
  195.             Return Str(buf)
  196.         End Get
  197.     End Property
  198.  
  199.     Private _isPaused As Boolean = False
  200.     ''' <summary>
  201.     ''' Whether or not the current playback is paused.
  202.     ''' </summary>
  203.     ''' <value></value>
  204.     ''' <returns></returns>
  205.     ''' <remarks></remarks>
  206.     Public Property IsPaused() As Boolean
  207.         Get
  208.             Return _isPaused
  209.         End Get
  210.         Set(ByVal value As Boolean)
  211.             _isPaused = value
  212.         End Set
  213.     End Property
  214.  
  215.     Private _filename As String
  216.     ''' <summary>
  217.     ''' The current filename of the file that is to be played back.
  218.     ''' </summary>
  219.     ''' <value></value>
  220.     ''' <returns></returns>
  221.     ''' <remarks></remarks>
  222.     Public Property Filename() As String
  223.         Get
  224.             Return _filename
  225.         End Get
  226.         Set(ByVal value As String)
  227.  
  228.             If My.Computer.FileSystem.FileExists(value) = False Then
  229.                 Throw New System.IO.FileNotFoundException
  230.                 Exit Property
  231.             End If
  232.  
  233.             _filename = value
  234.         End Set
  235.     End Property
  236. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement