Ver Mensaje Individual
  #2 (permalink)  
Antiguo 25/05/2013, 20:30
alexg88
 
Fecha de Ingreso: abril-2011
Mensajes: 1.342
Antigüedad: 13 años, 7 meses
Puntos: 344
Respuesta: NAudio en Visual Basic .NET

Hola de nuevo,

He conseguido reproducir tal y como lo quieres y parece que no se producen cortes.

Lo primero es crear una clase para soportar la reproducción en bucle (sacado de la documentación de NAudio).

Código vb:
Ver original
  1. Imports NAudio.Wave
  2.  
  3. Public Class LoopStream
  4.     Inherits WaveStream
  5.     Private sourceStream As WaveStream
  6.  
  7.     ''' <summary>
  8.    ''' Creates a new Loop stream
  9.    ''' </summary>
  10.    ''' <param name="sourceStream">The stream to read from. Note: the Read method of this stream should return 0 when it reaches the end
  11.    ''' or else we will not loop to the start again.</param>
  12.    Public Sub New(sourceStream As WaveStream)
  13.         Me.sourceStream = sourceStream
  14.         Me.EnableLooping = True
  15.     End Sub
  16.  
  17.     ''' <summary>
  18.    ''' Use this to turn looping on or off
  19.    ''' </summary>
  20.    Public Property EnableLooping() As Boolean
  21.         Get
  22.             Return m_EnableLooping
  23.         End Get
  24.         Set(value As Boolean)
  25.             m_EnableLooping = value
  26.         End Set
  27.     End Property
  28.     Private m_EnableLooping As Boolean
  29.  
  30.     ''' <summary>
  31.    ''' Return source stream's wave format
  32.    ''' </summary>
  33.    Public Overrides ReadOnly Property WaveFormat() As WaveFormat
  34.         Get
  35.             Return sourceStream.WaveFormat
  36.         End Get
  37.     End Property
  38.  
  39.     ''' <summary>
  40.    ''' LoopStream simply returns
  41.    ''' </summary>
  42.    Public Overrides ReadOnly Property Length() As Long
  43.         Get
  44.             Return sourceStream.Length
  45.         End Get
  46.     End Property
  47.  
  48.     ''' <summary>
  49.    ''' LoopStream simply passes on positioning to source stream
  50.    ''' </summary>
  51.    Public Overrides Property Position() As Long
  52.         Get
  53.             Return sourceStream.Position
  54.         End Get
  55.         Set(value As Long)
  56.             sourceStream.Position = value
  57.         End Set
  58.     End Property
  59.  
  60.     Public Overrides Function Read(buffer As Byte(), offset As Integer, count As Integer) As Integer
  61.         Dim totalBytesRead As Integer = 0
  62.  
  63.         While totalBytesRead < count
  64.             Dim bytesRead As Integer = sourceStream.Read(buffer, offset + totalBytesRead, count - totalBytesRead)
  65.             If bytesRead = 0 Then
  66.                 If sourceStream.Position = 0 OrElse Not EnableLooping Then
  67.                     ' something wrong with the source stream
  68.                    Exit While
  69.                 End If
  70.                 ' loop
  71.                sourceStream.Position = 0
  72.             End If
  73.             totalBytesRead += bytesRead
  74.         End While
  75.         Return totalBytesRead
  76.     End Function
  77. End Class


Lo segundo es usar el código que reproduce los dos archivos.
Código vb:
Ver original
  1. Imports NAudio.Wave
  2.  
  3.  
  4. Public Class Form1
  5.  
  6.     Private sound1 As WaveOut
  7.     Private sound2 As WaveOut
  8.  
  9.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button2.Click
  10.         If sound1 Is Nothing Then
  11.             Dim reader As New WaveFileReader("media/test.wav")
  12.             Dim looping As New LoopStream(reader)
  13.             sound1 = New WaveOut()
  14.             sound1.Init(looping)
  15.             sound1.Play()
  16.     End Sub
  17.  
  18.     Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button3.Click
  19.         If sound2 Is Nothing Then
  20.             Dim reader As New WaveFileReader("media/test1.wav")
  21.             Dim looping As New LoopStream(reader)
  22.             sound2 = New WaveOut()
  23.             sound2.Init(looping)
  24.             sound2.Play()
  25.     End Sub
  26. End Class

Espero que te sirva.

Un saludo.