Play .wav file without crashing the program - c#

I'm currently using this method to play a .wav file:
private void button5_Click(object sender, System.EventArgs e)
{
System.Media.SoundPlayer player = new System.Media.SoundPlayer(Properties.Resources.Song1);
player.PlaySync();
}
It's playing the .wav file for like a second, then it stops and a few seconds later after stopping, the program freezes and closes itself without showing any errors.
Did anyone experience this too and if so, (how) did you solve it?

Related

Problem in seeking video in C# using vlc player

I am using the following code to jump to a particular time location in the video
private void button1_Click(object sender, EventArgs e)
{
var replay = new Uri(#"Test.wmv");
var converted_url = replay.AbsoluteUri;
axVLCPlugin21.playlist.add(converted_url);
axVLCPlugin21.playlist.play();
axVLCPlugin21.input.time = 10000; // Jump to 10th second
}
Which works fine. But when the video is being written simultaneously while being displayed the code doesn't work and the video starts from zeroth second without throwing an error. Can someone suggest what's wrong or how to debug the code?

How to play background music in C# Form FROM properties.resources simultaneously with other sounds

I'm new to C# and I'm making a mini-game. Playing sound effects is no problem, I can just use the System.Media.SoundPlayer class object to play a wav file streaming from, for example Properties.Resources.attack.wav, this is good but the background music will stop after I play the sound effect. Yes, I know there's Windows Media Player, but that uses URI, which I can't seem to add the resources directory there. I don't want to use local directory like #"D:\MyGame\backgroundmusic.wav" because I want to send it to a friend and can still hear the music. Is there any class or external sdk's that allow sound to be played simultaneously with other sounds FROM Properties.Resources? If none, what is the URI of the Resources folder inside a solution? Any help or advice would be awesome! Thank you.
edit: I already opened those links and tried them all several times. My main question is "How to play sound FROM the Resources Folder of the solution without other sounds interrupting it"
This is the concept:
private void Form_Load(object sender, EventArgs e)
{
var player = new System.Windows.Media.MediaPlayer();
player.Open(Properties.Resources.sfx_background);
player.Play();
}
private void attack()
{
SoundPlayer p1 = new SoundPlayer();
p1.Stream = Properties.Resources.sfx_sword;
p1.Play();
}
Where p1 plays synchronous with player, in which player gets the sound file from the Resources of the solution, not from a local storage of a computer.

axWindowsMediaPlayer playlist pause (c#)

I'm using axWindowsMediaPlayer playlist, and have some problem - after one video end's - next playing without any pause. I cant pause or stop it even with
private void axWindowsMediaPlayer1_MediaChange(object sender, AxWMPLib._WMPOCXEvents_MediaChangeEvent e)
{
if (axWindowsMediaPlayer1.playState == WMPLib.WMPPlayState.wmppsMediaEnded)
{axWindowsMediaPlayer1.Ctlcontrols.pause(); }}
It doesn't do anything, but must stop the player. What can I do with this? Or even disable AUTOplaying next playlist file, that's would be even better.
Thx a lot for help!
I believe you should be working with the object passed into the method and additionally the PlayStateChange event like so:
private void axWindowsMediaPlayer1_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
if (e.newState == 8)
{
e.Ctlcontrols.pause();
}
}
}
UPDATE: Also I would suggest that you use the axWindowsMediaPlayer1_PlayStateChanged() event rather than Media change as this may be where the issue is occurring.
The '8' is to signify "Media Ended" which if you know how to access it another way then you of course can. If you want to use WMPLib.WMPPlayState.wmppsMediaEnded I'm sure it would yeild the same result.

Pause and Resume feature in an Audio Recorder Windows Universal App

I'm developing an audio recorder app on Universal Windows Platform on Windows 10 and the problem I'm facing is that when I resume the audio recording after it has been paused it only captures noise(like when the transformers shape shift kind of noise) and not the actual sound. Only the part when the recording is resumed after pause has this problem. I'm using MediaCapture class for this. Following is the code I've used to create the various functionalities.
//Start the recording
private async void recordBtn_Click(object sender, RoutedEventArgs e)
{
await CaptureMedia.StartRecordToStorageFileAsync(encodingProfile, file); //Records the audio to the specified file
}
//Pause the audio recording
private async void pauseBtn_Click(object sender, RoutedEventArgs e)
{
await CaptureMedia.PauseRecordAsync(Windows.Media.Devices.MediaCapturePauseBehavior.RetainHardwareResources); // Pauses the recording
}
//Resume the audio recording
private async void resumeBtn_Click(object sender, RoutedEventArgs e)
{
await CaptureMedia.ResumeRecordAsync(); // Everyting captured after this is just noise not the actual voice or sound
}
encodingProfile is a class field and it is initialized in the constructor to record the audio as a high quality mp3.
public Recorder()
{
this.InitializeComponent();
encodingProfile = MediaEncodingProfile.CreateMp3(AudioEncodingQuality.High);
}
The device I'm running this app on is a Windows 10 desktop and I'm using an external USB Microphone for recording.

Play sound in background

I have timer with an interval of 20 ms in Visual C#.
public class Global
{
//Global Vars
public static System.Media.SoundPlayer sound = new System.Media.SoundPlayer(Project.Properties.Resources.tock);
}
private void timer1_Tick(object sender, EventArgs e)
{
Global.sound.PlaySync();
}
My tock.wav plays 70 ms. My Problem is that timer1_tick is waiting till the sound is played and I don't want to short my wav file. Is there any option to play the sound in the background? It might sounds not good, but I will give it a try.
To start the SoundPlayer in a separate thread, simply call Play() instead of PlaySync().
See the documentation for reference.

Categories

Resources