Hi,

I have a little project to do in C#, building an audio player. I tought the best way is to use Microsoft.DirectX.AudioVideoPlayback. I tried to code so that when I push the play button again, the audio file restarts playing(otherwise it plays over the current song). so tha't my play function:

private void play_s_Click(object sender, EventArgs e)
        {
            if (song_path == "none") MessageBox.Show("Please choose an audio file to play.");
            else
            {
                song.CurrentPosition = 0;// I HAVE AN UNHANDLED EXCEPTION HERE !!
                    song = new Audio(song_path, false);
                    try
                    {
                        song.Play();
                        playing = true;
                        paused = false;
                    }
                    catch
                    {
                        MessageBox.Show("Error. The file could not be loaded.");
                    }
                
            }
        }

The problem is that I have an exception when I try to set the current position of song to 0;
I get this exception: 'NullReferenceException was unhandled'. I also tried to use SeekCurrentPosition. Same result, exception.

Any help will be appreciated. :)

Recommended Answers

All 2 Replies

Thanks for your interest Antenka(didn't help, no problem). In fact I would use that when I'll try to move my audio file to a different position. I found other way to RESTART my audio file when pressing the play button again. I simply used a boolean variable. That's not a problem, the problem is when I have to SEEK a position I want in my song(a complete audio player should have this capability).

However, thanks again!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.