0

I'm using Visual Studio 2017; and, I'm fairly new to coding. I'm trying to add a .wav audio file to a Clickable Button in my program. I first created a "Resource.resx" file and added the audio resource, with the changed 'properties' to "Embedded in .resx." Then added the method

playAudio();

...

private void playAudio()
{
SoundPlayer audio = new SoundPlayer(@"\Audio\Slots.wav");
}

in the button event of the code. The program runs fine as is; there's just no volume/sound. And yes, if I click on the .wav file in the Solution Explorer audio folder it plays just fine. Here is the code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Media;


namespace Casino
{
public partial class Default : System.Web.UI.Page
{
    Random random = new Random();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            string[] reels = new string[] { spinReel(), spinReel(), spinReel() };
            displayImages(reels);
            ViewState.Add("PlayersMoney", 100);
            displayPlayersMoney();
        }
    }

    protected void pullButton_Click(object sender, EventArgs e)
    {
        playAudio();

        int bet = 0;
        if (!int.TryParse(betTextBox.Text, out bet)) return;

        int winnings = pullLever(bet);
        displayResult(bet, winnings);
        adjustPlayersMoney(bet, winnings);
        displayPlayersMoney();
    }

    private void playAudio()
    {
        SoundPlayer audio = new SoundPlayer(@"\Audio\Slots.wav");
    }

...

An option of the audio playing with PageLoad would be fine with me as well. Also, I've been told that .wav files only can be played. If so, why am I not able to use .mp3 files?

Thanks for any help/direction you can provide!

CharithJ
  • 46,289
  • 20
  • 116
  • 131
jaxdavio
  • 19
  • 5
  • Try putting `audio.Play();` after the `Soundplayer` declaration, and [here](https://stackoverflow.com/a/5673109/6741868) is an example (you could try using Properties.Resources to find the file or use the path, both should be fine) – Keyur PATEL Aug 24 '17 at 01:45

1 Answers1

0

In playAudio() function you are just instantiating the SoundPlayer class, but you need to call the appropriate method to play the .wav file:

private void playAudio()
{
    SoundPlayer audio = new SoundPlayer(@"\Audio\Slots.wav");
    audio.Play();
}
Ferus7
  • 707
  • 1
  • 10
  • 23