You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I want to use headphones to play audio but it doesn't work, below is the code:
`
[Test]
[Category("IntegrationTest")]
public void PlayFileAudioToDevice_Headphones()
{
string ID = "{0.0.0.00000000}.{5776c80a-d3d3-428f-92b8-c2ae4311a78c}";
string filePath = @"C:\audio.wav";
var deviceInfo = DirectSoundOut.Devices.First(x => x.ModuleName == ID);
var reader = new WaveFileReader(filePath);
using (var dso = new DirectSoundOut(deviceInfo.Guid))
{
Exception ex = null;
dso.PlaybackStopped += (s, e) => { ex = e.Exception; };
dso.Init(reader);
dso.Play();
while (dso.PlaybackState == PlaybackState.Playing)
{
Thread.Sleep(1000);
}
if (ex != null)
{
Debug.WriteLine(ex.Message);
throw ex;
}
}
}
`
This can be fixed by making a small tweak to [Create SecondaryBuffer]:
`
bufferDesc2.dwReserved = 0;
//GCHandle handleOnWaveFormat = GCHandle.Alloc(waveFormat, GCHandleType.Pinned); // Ptr to waveFormat
//bufferDesc2.lpwfxFormat = handleOnWaveFormat.AddrOfPinnedObject(); // set Ptr to waveFormat
bufferDesc2.lpwfxFormat = WaveFormat.MarshalToPtr(waveFormat);
bufferDesc2.guidAlgo = Guid.Empty;
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I want to use headphones to play audio but it doesn't work, below is the code:
`
[Test]
[Category("IntegrationTest")]
public void PlayFileAudioToDevice_Headphones()
{
string ID = "{0.0.0.00000000}.{5776c80a-d3d3-428f-92b8-c2ae4311a78c}";
string filePath = @"C:\audio.wav";
}
`
This can be fixed by making a small tweak to [Create SecondaryBuffer]:
`
bufferDesc2.dwReserved = 0;
//GCHandle handleOnWaveFormat = GCHandle.Alloc(waveFormat, GCHandleType.Pinned); // Ptr to waveFormat
//bufferDesc2.lpwfxFormat = handleOnWaveFormat.AddrOfPinnedObject(); // set Ptr to waveFormat
bufferDesc2.lpwfxFormat = WaveFormat.MarshalToPtr(waveFormat);
bufferDesc2.guidAlgo = Guid.Empty;
// Create SecondaryBuffer
directSound.CreateSoundBuffer(bufferDesc2, out soundBufferObj, IntPtr.Zero);
secondaryBuffer = (IDirectSoundBuffer)soundBufferObj;
//handleOnWaveFormat.Free();
`
Beta Was this translation helpful? Give feedback.
All reactions