locked
How to play wav files with SharpDX XAudio2 RRS feed

  • Question

  • Hi

    I have a problem with playing wav files with SharpDX XAudio2 on Visual Studio 2019

    The following chord worked well on Visual Studio 2016. But it does not on Visual Studio 2019

            
                           
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    
    
    using SharpDX.IO;
    using SharpDX.Multimedia;
    using SharpDX.XAudio2;
    
    
    namespace WpfApp9
    {
    
        public partial class MainWindow : Window
        {
    
            private WavePlayer _wavePlayer = new WavePlayer();
            public MainWindow()
            {
                InitializeComponent();
    
                _wavePlayer.AddWave("piano-c", "piano-c.wav");
            }
    
                public class WavePlayer : IDisposable
                {
                    public bool IsSoundOn { get; set; }
    
    
    
                    private readonly XAudio2 xAudio;
                    readonly Dictionary<string, MyWave> sounds;
    
                    public WavePlayer()
                    {
                        xAudio = new XAudio2();
                        xAudio.StartEngine();
                        var masteringVoice = new MasteringVoice(xAudio);
                        masteringVoice.SetVolume(1);
                        sounds = new Dictionary<string, MyWave>();
                    }
    
                    // Reads a sound and puts it in the dictionary
                    public void AddWave(string key, string filepath)
                    {
                        var wave = new MyWave();
    
    
                        sounds.Add(key, wave);
                    }
    
                    // Plays the sound
                    public void PlayWave(string key)
                    {
                        if (!sounds.ContainsKey(key)) return;
                        var w = sounds[key];
    
                        var sourceVoice = new SourceVoice(xAudio, w.WaveFormat);
                        sourceVoice.SubmitSourceBuffer(w.Buffer, w.DecodedPacketsInfo);
                        sourceVoice.Start();
                    }
    
                    public void Dispose()
                    {
                        xAudio.Dispose();
                    }
                }
    
    
                public class MyWave
                {
                    public AudioBuffer Buffer { get; set; }
                    public uint[] DecodedPacketsInfo { get; set; }
                    public WaveFormat WaveFormat { get; set; }
                }
    
    
            private void button_Click(object sender, RoutedEventArgs e)
            {
    
           _wavePlayer.PlayWave("piano-c");
    
    
            }
        }
    }

    Incidentally, this is the error I got when I played a wav file.                                                                                                                


    • Edited by hirohiro3750 Tuesday, September 15, 2020 11:04 AM
    Tuesday, September 15, 2020 11:03 AM

All replies

  • SharpDX is a third party product. We only provide support for C#-specific questions in this forum. For questions related to third party products please post in their forums.

    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, September 15, 2020 2:06 PM
  • Did you strip your code for the forum or did you forget to implement AddWave(). Since this function doesn't do anything with the filepath provided nor setting any properties of wave instance.

    So from this I would think WaveFormat is invalid since it was never set.

    Tuesday, September 15, 2020 2:23 PM
  • Hi,

    >Did you strip your code for the forum or did you forget to implement AddWave(). 

    I copied this code from here.

    https://www.loekvandenouweland.com/content/playing-wav-files-in-a-xamlc-windows-store-app-using-sharpdx.html

    What I am perplexed most is the fact that the above code used to work very well on Visual Studio 2016 but this so far does not produce any sound at all on Visual Studio 2019.

    It seems that unlike Visual Studio 2016, Visual Studio 2019  does not allow those codes-

    _wavePlayer.AddWave("piano-c", @"C:\Users\水\source\repos\WpfApp9\WpfApp9\piano-c.wav"); or

     _wavePlayer.AddWave("piano-c", "piano-c.wav");  to get sound.

    So, I believe my problem has something to do with Visual Studio 2019 itself, rather than SharpDX.





    Wednesday, September 16, 2020 2:47 AM
  • What about this part:

                var nativeFileStream = new NativeFileStream(filepath, NativeFileMode.Open, NativeFileAccess.Read);
                var soundStream = new SoundStream(nativeFileStream);
                var buffer = new AudioBuffer { Stream = soundStream, AudioBytes = (int)soundStream.Length, Flags = BufferFlags.EndOfStream };
    
                wave.Buffer = buffer;
                wave.DecodedPacketsInfo = soundStream.DecodedPacketsInfo;
                wave.WaveFormat = soundStream.Format;
    

    Also in your debug window you can see that WaveFormat is null. So the above code isn't executed.

    Wednesday, September 16, 2020 10:45 AM
  • Hello,

    I added what you mentioned and put WaveFormat. It looks like this.

     public void AddWave(string key, string filepath)
                    {
                        var wave = new MyWave();
    
    
                    key =  @"C: \Users\水\source\repos\WpfApp9\WpfApp9\piano-c"; // Here is the thing I added.
                    filepath = @"C:\Users\水\source\repos\WpfApp9\WpfApp9\piano-c.wav";
    
                    var nativeFileStream = new NativeFileStream(filepath, NativeFileMode.Open, NativeFileAccess.Read);
                           var soundStream = new SoundStream(nativeFileStream);
                           var buffer = new AudioBuffer { Stream = soundStream, AudioBytes = (int)soundStream.Length, Flags = BufferFlags.EndOfStream };
    
                           wave.Buffer = buffer;
                           wave.DecodedPacketsInfo = soundStream.DecodedPacketsInfo;
                           wave.WaveFormat = soundStream.Format;
    
                        sounds.Add(key, wave);
                    }

    It does not cause any error but the problem is that it produces no sound. I am so sorry but I am just an amateur. 

    Thursday, September 17, 2020 10:21 AM