Foros del Web » Programación para mayores de 30 ;) » .NET »

[SOLUCIONADO] NAudio: Dispositivo de salida

Estas en el tema de NAudio: Dispositivo de salida en el foro de .NET en Foros del Web. Y aquí va mi última pregunta sobre NAudio en este foro durante (espero) bastante tiempo :P http://naudio.codeplex.com/ El programa en cuestión puede reproducir y detener ...
  #1 (permalink)  
Antiguo 09/06/2013, 15:14
 
Fecha de Ingreso: noviembre-2009
Mensajes: 67
Antigüedad: 14 años, 5 meses
Puntos: 2
NAudio: Dispositivo de salida

Y aquí va mi última pregunta sobre NAudio en este foro durante (espero) bastante tiempo :P

http://naudio.codeplex.com/

El programa en cuestión puede reproducir y detener varios sonidos simultáneamente, en bucle y con distinto volumen para cada uno, y ya lo único que me falta, es que pueda reproducir cada uno por un dispositivo diferente.

La documentación sobre este asunto se encuentra aquí: http://mark-dot-net.blogspot.com.es/2011/05/naudio-audio-output-devices.html, pero como siempre, no encuentro códigos de ejemplo para copiar y probar.

Código fuente de una versión reducida de mi programa:

Código vb:
Ver original
  1. Imports NAudio.Wave
  2.  
  3. Public Class Form1
  4.     Public Class LoopStream
  5.         Inherits WaveStream
  6.         Private sourceStream As WaveStream
  7.  
  8.         Public Sub New(sourceStream As WaveStream)
  9.             Me.sourceStream = sourceStream
  10.             Me.EnableLooping = True
  11.         End Sub
  12.  
  13.         Public Property EnableLooping() As Boolean
  14.             Get
  15.                 Return m_EnableLooping
  16.             End Get
  17.             Set(value As Boolean)
  18.                 m_EnableLooping = value
  19.             End Set
  20.         End Property
  21.  
  22.         Private m_EnableLooping As Boolean
  23.  
  24.         Public Overrides ReadOnly Property WaveFormat() As WaveFormat
  25.             Get
  26.                 Return sourceStream.WaveFormat
  27.             End Get
  28.         End Property
  29.  
  30.         Public Overrides ReadOnly Property Length() As Long
  31.             Get
  32.                 Return sourceStream.Length
  33.             End Get
  34.         End Property
  35.  
  36.         Public Overrides Property Position() As Long
  37.             Get
  38.                 Return sourceStream.Position
  39.             End Get
  40.             Set(value As Long)
  41.                 sourceStream.Position = value
  42.             End Set
  43.         End Property
  44.  
  45.         Public Overrides Function Read(buffer As Byte(), offset As Integer, count As Integer) As Integer
  46.             Dim totalBytesRead As Integer = 0
  47.  
  48.             While totalBytesRead < count
  49.                 Dim bytesRead As Integer = sourceStream.Read(buffer, offset + totalBytesRead, count - totalBytesRead)
  50.                 If bytesRead = 0 Then
  51.                     If sourceStream.Position = 0 OrElse Not EnableLooping Then
  52.                         ' something wrong with the source stream
  53.                        Exit While
  54.                     End If
  55.                     ' loop
  56.                    sourceStream.Position = 0
  57.                 End If
  58.                 totalBytesRead += bytesRead
  59.             End While
  60.             Return totalBytesRead
  61.         End Function
  62.     End Class
  63.  
  64.     Dim Noise As WaveOut
  65.     Dim NoiseReader As AudioFileReader
  66.     Dim Hum2010 As WaveOut
  67.     Dim Hum2010Reader As AudioFileReader
  68.  
  69.     Private Sub PlayButton1_Click(sender As Object, e As EventArgs) Handles PlayButton1.Click
  70.         If PlayButton1.Text = "Reproducir" Then
  71.             Hum2010Reader = New AudioFileReader(Application.StartupPath & "\media\2010Hum.wav")
  72.             Dim looping As New LoopStream(Hum2010Reader)        '  
  73.            Hum2010 = New WaveOut()
  74.             Hum2010.Init(looping)
  75.             Hum2010Reader.Volume = Val(T2010Volume.Value) / 10
  76.             Hum2010.Play()
  77.             PlayButton1.Text = "Detener"
  78.  
  79.         Else
  80.             PlayButton1.Text = "Reproducir"
  81.             If Hum2010 IsNot Nothing Then
  82.                 Hum2010.Stop()
  83.             End If
  84.         End If
  85.     End Sub
  86.  
  87.     Private Sub PlayButton2_Click(sender As Object, e As EventArgs) Handles PlayButton2.Click
  88.         If PlayButton2.Text = "Reproducir" Then
  89.             NoiseReader = New AudioFileReader(Application.StartupPath & "\media\Noise.wav")
  90.             Dim looping As New LoopStream(NoiseReader)        '  
  91.            Noise = New WaveOut()
  92.             Noise.Init(looping)
  93.             NoiseReader.Volume = Val(NoiseVolume.Value) / 10
  94.             Noise.Play()
  95.             PlayButton2.Text = "Detener"
  96.         Else
  97.             PlayButton2.Text = "Reproducir"
  98.             If Noise IsNot Nothing Then
  99.                 Noise.Stop()
  100.             End If
  101.         End If
  102.     End Sub
  103.  
  104.     Private Sub T2010Volume_Scroll(sender As Object, e As EventArgs) Handles T2010Volume.Scroll
  105.         Hum2010Reader.Volume = Val(T2010Volume.Value) / 10
  106.     End Sub
  107.  
  108.     Private Sub NoiseVolume_Scroll(sender As Object, e As EventArgs) Handles NoiseVolume.Scroll
  109.         NoiseReader.Volume = Val(NoiseVolume.Value) / 10
  110.     End Sub
  111.  
  112. End Class

Enlace al proyecto de ejemplo: https://www.dropbox.com/s/weo3oxera2trk9n/Pruebas2.zip

Edito: Por lo que veo, poniendo Noise.DeviceNumber = 2 antes de Noise.Init(looping), usa mi segunda salida de audio, que se llama Audio digital (S/PFID). Por lo que mi pregunta pasa a ser, ¿cómo hago un combobox que liste todas las salidas de audio y que se aplique la seleccionada?

Edito2: Solucionado de esta forma:

Código vb:
Ver original
  1. Private Sub InitialiseDeviceCombo()
  2.         For deviceId As Integer = 0 To WaveOut.DeviceCount - 1
  3.             Dim capabilities = WaveOut.GetCapabilities(deviceId)
  4.             Sonido2OutDevice.Items.Add([String].Format("Device {0} ({1})", deviceId, capabilities.ProductName))
  5.         Next
  6.         If Sonido2OutDevice.Items.Count > 0 Then
  7.             Sonido2OutDevice.SelectedIndex = 0
  8.         End If
  9.     End Sub

Con ese código, le añado a un combobox la lista de salidas de audio, y al botón de reproducir, le añado Noise.DeviceNumber = Sonido2OutDevice.SelectedIndex justo antes de Noise.Init(looping) y ya suena exactamente por donde debería sonar.

Última edición por vistaero; 09/06/2013 a las 23:43

Etiquetas: dispositivo, salida
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 22:06.