0

I want to play a .wav file on a mobile device. I guess my trouble lies in actually accessing the file correctly. The following is the code I currently have:

string path = "\\Windows\\badRead.wav";
System.Media.SoundPlayer player = new System.Media.SoundPlayer(path);
player.PlaySync();
player.PlaySync();

What am I missing? The error I'm receiving is "Please be sure a sound file exists at the specified location. IS it that it's searching for the wav file on the server? Thank you in advance.

Eric
  • 7,930
  • 17
  • 96
  • 128
  • This should work if the path is correct. Are you getting a FileNotFound exception? Have you verified that the file exists on the intermec device? – ScottieMc Jun 27 '12 at 18:24
  • It does exists. I think the problem lies with the unspecified Driver. It isn't mapped, and I'm not familiar with accessing a file that way. – Eric Jun 27 '12 at 18:50
  • Are you not able to browse to the file and click on it to play? If it fails there then maybe it is a driver issue. – ScottieMc Jun 27 '12 at 19:06
  • I'm able to browse to it and play it, but i can't fire it off programmatically. – Eric Jun 27 '12 at 19:10
  • Have you tried copying the .wav to another folder? Maybe try the same folder as your app `SoundPlayer player = new SoundPlayer(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + "\\badRead.wav")` – ScottieMc Jun 27 '12 at 19:49
  • I just tried it, I'm still getting the error. And this is a web app by the way. I'm thinking it may be searching on the server for the file. Thank you for your suggestions. – Eric Jun 27 '12 at 19:58
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/13129/discussion-between-eric-and-scottiemc) – Eric Jun 27 '12 at 20:00

2 Answers2

2

Have you read this: http://peterfoot.net/SystemMediaSoundPlayerVersusThePlaySoundAPI.aspx

Possibly you have to issue a player.Load() first before using player.PlaySync:

    private SoundPlayer Player = new SoundPlayer();
    private void loadSoundAsync()
    {
        // Note: You may need to change the location specified based on
        // the location of the sound to be played.
        this.Player.SoundLocation = "http://www.tailspintoys.com/sounds/stop.wav";
        this.Player.LoadAsync();
    }

    private void Player_LoadCompleted (
        object sender, 
        System.ComponentModel.AsyncCompletedEventArgs e)
    {
        if (this.Player.IsLoadCompleted)
        {
            this.Player.PlaySync();
        }
    }

Another approach would be to use the Play>Sound API natively:

http://msdn.microsoft.com/en-us/library/ms228710%28v=vs.90%29.aspx

josef
  • 5,951
  • 1
  • 13
  • 24
  • I'm not sure I fully understand. The Player_LoadCompleted is never fired off. – Eric Jul 02 '12 at 17:19
  • This actually did not work. It works on my local machine, but when i put it in production to the server the sound is not played. – Eric Jul 02 '12 at 21:22
0

I used javascript instead of trying to play the sound server side.

I found my answer here

Eric
  • 7,930
  • 17
  • 96
  • 128