Play wav / mp3 with Windows 7

I need to play a sound file (wav or mp3 in order) from a service in Windows 7/2008 ... just like we did in WinXP, it no longer works. I have completed this question Play a wave file from a Windows service (C #) without any success in various combinations. The sound plays perfectly through the debugger, but as soon as I install it as a service in which it does not play, and the event logs confirm that the call is being made. I also followed the link http://bresleveloper.blogspot.co.il/2012/06/c-service-play-sound-with-naudio.html with the same result.

The following is a snippet of code. This is a C # service since VS 2010, .NET 4.0, NAudio 1.5.4.0. I am using InstallUtil to install / uninstall a service.

WRT code, I comment on Wav or MP3 material and one of the methods every time ... they all reproduce sound well.

static class Program { [DllImport("kernel32.dll")] public static extern Boolean AllocConsole(); static void Main(string[] args) { m_eventLog = new EventLog(); m_eventLog.Source = "EventLoggerSource"; if(args.Length > 0 && args[0].ToLower() == "/console") { AllocConsole(); EventLoggerApp app = new EventLoggerApp(); app.Start(); m_eventLog.WriteEntry("INFO (Calling Player)"); string fileFullPath=@ "c:\aaasound\bunny_troubles.wav", fileFullPath2=@ "c:\aaasound\dreams.mp3"; // Wav file IWavePlayer wavePlayer=new WaveOutEvent(); // Method 1 IWavePlayer wavePlayer=new WasapiOut(NAudio.CoreAudioApi.AudioClientShareMode.Shared,false,100); // Method 2 AudioFileReader audioFile=new AudioFileReader(fileFullPath); audioFile.Volume = (float)1.0; wavePlayer.Init(audioFile); // MP3 file IWavePlayer wavePlayer=new WasapiOut(NAudio.CoreAudioApi.AudioClientShareMode.Shared,true,100); // Method 1- EventSync/not both work IWavePlayer wavePlayer=new WasapiOut(NAudio.CoreAudioApi.AudioClientShareMode.Exclusive,false,100); // Method 2- EventSync must be false or throws an exception WaveStream mp3Reader = new Mp3FileReader(fileFullPath2); WaveChannel32 inputStream=new WaveChannel32(mp3Reader); WaveStream wavStream=new WaveChannel32(inputStream); wavePlayer.Init(wavStream); //this.wavePlayer.PlaybackStopped += new EventHandler(wavePlayer_PlaybackStopped); wavePlayer.Play(); while(wavePlayer.PlaybackState == PlaybackState.Playing) { Thread.Sleep(100); } 
+4
source share

Source: https://habr.com/ru/post/1434880/


All Articles