I'm building a WPF project with Visual Studio 2012 and I'm finding a problem with relative paths.
In the specific, I want a sound to be played when a ToggleButton is checked.
This is the code for my button:
<ToggleButton Name="Circles" Margin="88,104,86,172" Background="#FFFFD0CD" Foreground="Black" Checked="Circles_Checked">Circles</ToggleButton>
and if I write like this:
private void Circles_Checked(object sender, System.Windows.RoutedEventArgs e)
{
MediaPlayer mplayer = new MediaPlayer();
mplayer.Open(new Uri("C:\\Users\\user1\\Documents\\Visual Studio 2012\\Projects\\RehabilitationGUIProject\\RehabilitationGUIProject\\circles.mp3", UriKind.Relative));
mplayer.Play();
}
everything works as espected. But I want this code to work also onto other machines, not only mine. And what If I have to move the sounds to another location? I would like to use relative paths and move every sound inside my project folder.
So I created a new folder named Sound inside my root project, the structure is like this:
RehabilitationGuiProject
\Properties
\References
\Bin
\Object
\Sounds
\circles.mp3
... other files .xaml
So I wrote the same lines of code above, changing this one:
mplayer.Open(new Uri(#"/Sounds/circles.mp3", UriKind.Relative));
But no sound is played. Which is the correct relative path for playing sounds from a project folder?
The path must be relative to the folder where your application is executed.
mplayer.Open(new Uri(#"../../Sounds/circles.mp3", UriKind.Relative));
(Assuming your exe is run from the Bin/Debug folder)
Related
I download and use CommonOpenFileDialog from NuGet to open the folder selection window.
Since the functions are generally similar, you can think of using OpenFileDialog.
I saved the path of the folder that opened last in a text file and loaded it at the next run to run the folder selection window through CommonOpenFileDialog.Initial Directory.
It works well so far, but what I want to is that from the moment the folder selection window opens, the folder is already selected.
Since I have to check before opening the folder, it is possible to implement it right away without a folder selection window through the path, but it is an unwanted way.
I would appreciate it if you could give me advice.
I upload a part of the code I wrote for reference.
Thank you.
string readTexts = null;
if (fi.Exists)
readTexts = File.ReadAllText(fi.FullName);
folder_dialog.InitialDirectory = readTexts;
if (folder_dialog.ShowDialog() == CommonFileDialogResult.Ok)
{
OpenTextBox.Text = selected_path = folder_dialog.FileName;
WritePath(fi, System.IO.Path.GetDirectoryName(selected_path));
}
I am very new at c# and visual studio and now i am learning all the tools.
At first i tried adding background sound to my form. I added a wav file trhough resources and i changed it's build action to embedded resources so that i can "play" the .exe file without problems at any other pc.Then i wrote this and it worked.
namespace audio
{
public partial class Form1 : Form
{
SoundPlayer sp = new SoundPlayer(Properties.Resources.back_sound);
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
sp.Play();
}
}
}
Now i want to play a video file with windows media player.I added it through COM components and i added it to my form.I can make it play any video just by adding the video file at the debug folder and write this.
private void button1_Click(object sender, EventArgs e)
{
axWindowsMediaPlayer1.URL = "video.AVI";
}
But as before i want to add it to my resources so that i can execute the .exe file to aby other pc and by this way, i cant
I tried with ( following the same pattern with the sound file)
AxWindowsMediaPlayer player = new AxWindowsMediaPlayer(Properties.Resources.video);
( and inside the button player.play();)
and
WindowsMediaPlayer player = new WindowsMediaPlayer(Properties.Resources.video);
( and inside the button player.play();)
but no luck.
Can anyone help me, please?
Thanks in advance
UPDATE:
I tried a lot of things and nothing worked, so i thought that it would better to stream the videos directly from youtube. I went to COM components and i added to my form a SHOCKWAVE FLASH OBJECT. I took the url i wanted, i transformed it ( from www.youtube.com/watch?v=xxxxxxxxxxxx to www.youtube.com/v/xxxxxxxxxxxx) i added it to the movie section of the flash object. It is working but only on my computer. If i execute the .exe in any other pc it doesnt work. It says that System.IO.FileNotFoundException.
What can i do to make it work?
I am trying to play a sound whenever an object is touched/moved in my WPF Surface application, to depict it has been selected or its movement. Here is what I tried but it doesn't seem to work.
SoundPlayerAction soundPlayerAction = new SoundPlayerAction();
soundPlayerAction.Source = new Uri(#"C:\Resources\Selection.wav", UriKind.RelativeOrAbsolute);
EventTrigger eventTrigger = new EventTrigger(TouchEnterEvent); // this is the event you want to trigger the sound effect.
eventTrigger.Actions.Add(soundPlayerAction);
Any feedback or ideas will be much appreciated.
Create a new folder in your project and rename it to SOUNDS then insert your sound files in it and try this:
SoundPlayerAction soundPlayerAction = new SoundPlayerAction();
soundPlayerAction.Source = new Uri(#"SOUNDS\Selection.wav", UriKind.RelativeOrAbsolute);
EventTrigger eventTrigger = new EventTrigger(TouchEnterEvent); // this is the event you want to trigger the sound effect.
eventTrigger.Actions.Add(soundPlayerAction);
Triggers.Add(eventTrigger); // Add this event trigger to Window.Triggers collection.
C:\Resources\Selection.wav is obviously not the correct path of the sound file.
You should create a folder named "Resources" in your Visual Studio project, and add the sound file to that folder. Then go to the properties of the sound file and set its Build Action to Resource and also set Copy to Output Directory to Copy aways or Copy if newer.
Now you can access the file by a relative Uri like this:
soundPlayerAction.Source = new Uri("Resources/Selection.wav", UriKind.Relative);
I came across this link https://www.youtube.com/watch?v=nF-HYoTurc8 and somewhat changed my code and the sound worked :D
In my PreviewTouchDown method, I added:
SoundPlayer soundPlayerAction = new SoundPlayer(TangramsTool.Properties.Resources.Selection);
soundPlayerAction.Play();
i already create a picturebox and have image inside it. But when i run the program, the image didn't show anything, just sign "x" (didn't load) when i run the program.
Where do i did wrong?
Here is the code:
private void Images(object sender, EventArgs e)
{
PictureBox pb1 = new PictureBox();
pb1.ImageLocation = "SamuderaJayaMotor.png";
pb1.Location = new Point(100, 100);
pb1.SizeMode = PictureBoxSizeMode.StretchImage;
pb1.Size = new Size(200, 200);
this.Controls.Add(pb1);
}
Here is the screenshot when i run the program:
You need to make sure that the image is in the same directory as the executable. That is, whilst you are creating your application, it will live in the /bin/Debug folder. If you are running in Release mode, it will be in the /bin/Release folder.
By supplying just a file name, your application is attempting to load the image from the current working directory. If you run your application in C:\MyApp.. then your image must also be inside C:\MyApp. If you are running it from D:\Projects\MyApp, then your image must also be in D:\Projects\MyApp.
What you can do.. is set your image to always copy to where your executable is. Right click on the image in the Solution Explorer and go to Properties. Then set the following property to "Copy always":
This will make sure the image is placed into the directory that your application is built in.
I have a Windows application written in C#/.NET.
How can I play a specific sound when a button is clicked?
You could use:
System.Media.SoundPlayer player = new System.Media.SoundPlayer(#"c:\mywavfile.wav");
player.Play();
You can use SystemSound, for example, System.Media.SystemSounds.Asterisk.Play();.
For Windows Forms one way is to use the SoundPlayer
private void Button_Click(object sender, EventArgs e)
{
using (var soundPlayer = new SoundPlayer(#"c:\Windows\Media\chimes.wav")) {
soundPlayer.Play(); // can also use soundPlayer.PlaySync()
}
}
MSDN page
This will also work with WPF, but you have other options like using MediaPlayer MSDN page
Additional Information.
This is a bit high-level answer for applications which want to seamlessly fit into the Windows environment. Technical details of playing particular sound were provided in other answers. Besides that, always note these two points:
Use five standard system sounds in typical scenarios, i.e.
Asterisk - play when you want to highlight current event
Question - play with questions (system message box window plays this one)
Exclamation - play with excalamation icon (system message box window plays this one)
Beep (default system sound)
Critical stop ("Hand") - play with error (system message box window plays this one)
Methods of class System.Media.SystemSounds will play them for you.
Implement any other sounds as customizable by your users in Sound control panel
This way users can easily change or remove sounds from your application and you do not need to write any user interface for this – it is already there
Each user profile can override these sounds in own way
How-to:
Create sound profile of your application in the Windows Registry (Hint: no need of programming, just add the keys into installer of your application.)
In your application, read sound file path or DLL resource from your registry keys and play it. (How to play sounds you can see in other answers.)
Code bellow allows to play mp3-files and in-memory wave-files too
player.FileName = "123.mp3";
player.Play();
from http://alvas.net/alvas.audio,samples.aspx#sample6 or
Player pl = new Player();
byte[] arr = File.ReadAllBytes(#"in.wav");
pl.Play(arr);
from http://alvas.net/alvas.audio,samples.aspx#sample7
To play an Audio file in the Windows form using C# let's check simple example as follows :
1.Go Visual Studio(VS-2008/2010/2012) --> File Menu --> click New Project.
2.In the New Project --> click Windows Forms Application --> Give Name and then click OK.
A new "Windows Forms" project will opens.
3.Drag-and-Drop a Button control from the Toolbox to the Windows Form.
4.Double-click the button to create automatically the default Click event handler, and add the following code.
This code displays the File Open dialog box and passes the results to a method named "playSound" that you will create in the next step.
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = "Audio Files (.wav)|*.wav";
if(dialog.ShowDialog() == DialogResult.OK)
{
string path = dialog.FileName;
playSound(path);
}
5.Add the following method code under the button1_Click event hander.
private void playSound(string path)
{
System.Media.SoundPlayer player = new System.Media.SoundPlayer();
player.SoundLocation = path;
player.Load();
player.Play();
}
6.Now let's run the application just by Pressing the F5 to run the code.
7.Click the button and select an audio file. After the file loads, the sound will play.
I hope this is useful example to beginners...
I think you must firstly add a .wav file to Resources. For example you have sound file named Sound.wav. After you added the Sound.wav file to Resources, you can use this code:
System.Media.SoundPlayer player = new System.Media.SoundPlayer(Properties.Resources.Sound);
player.Play();
This is another way to play sound.