I have a WinForms app. This app has a Preferences section where the user will be able to select which sounds are played when an alert is being displayed.
Is it possible to have a combobox where the user can select from the Windows stored sounds such as "critical stop", "critical beep" and so on. These are found in the "Control Panel" >> "Sounds and Alerts" section.
Is it also possible to have a play button to test the sounds out?
You do not require any API to play system sounds just write code like this:
// Plays the sound associated with the Asterisk system event.
System.Media.SystemSounds.Asterisk.Play();
The SystemSounds class contains the following predefined system sounds:
Asterisk
Beep
Exclamation
Hand
Question
All other sounds require you read the desired sound from the registry and play it with code like this:
SoundPlayer simpleSound = new SoundPlayer(#"c:\Path\To\Your\Wave\File.wav");
Try this:
private void Form1_Load(object sender, EventArgs e)
{
var systemSounds = new[]
{
System.Media.SystemSounds.Asterisk,
System.Media.SystemSounds.Beep,
System.Media.SystemSounds.Exclamation,
System.Media.SystemSounds.Hand,
System.Media.SystemSounds.Question
};
comboBox1.DataSource = systemSounds;
comboBox1.SelectedIndexChanged += new EventHandler(comboBox1_SelectedIndexChanged);
}
void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
((System.Media.SystemSound)comboBox1.SelectedItem).Play();
}
Sure! All the sounds you're looking for are available through the System.Media.SystemSounds class, where they are exposed as public properties corresponding to the event types that trigger the sounds.
Additionally, objects of the SystemSound class provide a Play method that you can call to play that sound asynchronously.
So for example, to play the "Critical Stop" sound, you would simply write the following code:
System.Media.SystemSounds.Hand.Play();
SystemSounds only cover a few sounds. For playing the others, you need to read the registry :
const string key = #"AppEvents\Schemes\Apps\.Default\Notification.Default\.Default";
using (var reg = Registry.CurrentUser.OpenSubKey(key))
using (var player = new SoundPlayer((string)reg.GetValue(string.Empty)))
{
player.Play();
}
Related
I use a proprietary IDE that is part of a large software package. At its core, however, C# is used as the language. Most .Net components and classes can be used. I can use it to create my own functions / WinForms, which are then integrated into an ERP system. This means that you can expand this system yourself. This is quite well implemented but there is no documentation and the support is also poor at this point. That's why I'm trying my luck here. Maybe someone can help.
I am currently programming a WinForms window that contains a WebBrowser control (wb). I'm pretty sure this is IE. I can programmatically create and include HTML code via MemoryStream (ms):
ms.Write(bytes, 0, bytes.Length);
wb.DocumentStream = ms;
It all works very well. Now I want to get events from the browser and have had the IDE create event handlers for this:
try {
wb.Click += new EventHandler(wb_Click);
...} etc.
public void wb_Click (object sender, EventArgs e)
{ }
The compiler shows no errors. During the execution I get the error message: The Click event is invalid for this ActiveX control. What could trigger this error? W. Wolf
Thanks for your tips! I was able to solve this with the following code:
...
HtmlElementCollection elements = wb.Document.GetElementsByTagName("span");
for (int n = 0; n < elements.Count;n++)
{
elements[n].Click += new HtmlElementEventHandler(el_Click);
}
...
public void el_Click (object sender, HtmlElementEventArgs e)
{
HtmlElement el = (HtmlElement) sender;
ShowInfo(el.Id);
}
I can continue to work with it.
Can someone please tell me whey I am getting a black screen with no video, only sound?
private void screen1btnPlay_Click(object sender, EventArgs e)
{
ScreenOne playScreen1 = new ScreenOne();
playScreen1.PlayScreenOne();
}
... and the other form is like this:
public partial class ScreenOne : Form
{
public ScreenOne()
{
InitializeComponent();
}
public void PlayScreenOne()
{
axVLCPlugin21.playlist.add("file:///" + #"Filepath", null);
axVLCPlugin21.playlist.play();
}
}
Sound works fine, but no video. All the properties of the VLC are left to default, is there something I need to change when using this plugin across multiple forms? Anyone know what's wrong?
Update:: I rebuilt the program in WPF and I am having the same problem. When I have a button on the second form (same form as player) it works fine, as soon as I call it from the main form, sound only. ugh!
I dont know but i can give some solution suggestions,
Make sure the VLC program is installed as 32-bit. I dont know, I've solved a problem that way.
I think high probabilty your problem is based on about "C:\Program Files (x86)\VideoLAN\VLC\plugins" Check your plugins. maybe your audio_filter, audio_mixer, audio_output plugins are missing.
you can remove Vlc, then download and install last VLC 32 bit.
I think that will solve your problem. Dont forget AxAXVLC works with vlc plugins.
I figured out my problem on my own!
When I was creating this instance,
ScreenOne playScreen1 = new ScreenOne();
I was actually creating a redundant instance of what I was trying to do, I'm not sure if that's the right way to put it but I basically already had an instance of the second form and was making another separate instance of the form that was named differently.
I already had in my code to open the second form
Screen2 Screen2 = new Screen2();
private void openScreen2Button_Click(object sender, EventArgs e)
{
Screen2.Show();
}
Then later was doing this which is WRONG, I was adding playscreen1 when I should still have been using Screen2.
Screen2 playScreen1 = new Screen2();
playScreen1.PlayScreenOne();
So when I wanted to use the method to play the media player on the second form from the first one, I just needed to use the same instance of Screen2 that I had created to open the form to begin with instead of created a new instance for what method I wanted to use.
IDK if my explanation makes sense, or maybe its basics to most people (I'm a noob), but if anyone comes across this problem, message me and I'll try to help.
o7
This question already has answers here:
How to play a WPF Sound File resource
(4 answers)
Closed 7 years ago.
First of all, I've searched the site and looked at this but unfortunately it did not help me much.
Whenever I click a frog image that I made, I want to play a .wav file as a resource. Here's my code:
void newFrog_MouseUp(object sender, MouseButtonEventArgs e)
{
System.Media.SoundPlayer player = new System.Media.SoundPlayer(#"G:\COSR3S\FrogsAndLilyPads\FrogsAndLilyPads\Sounds\FrogCroak.wav");
player.Play();
}
Of course this worked but my problem is the directory changes from computer to computer, so I thought I'd have to play it as a resource. I don't know how to do that. I am a fairly new programmer, just to keep in mind.
I tried a few variations based on the aforementioned link and played around with it. Examples:
void newFrog_MouseUp(object sender, MouseButtonEventArgs e)
{
Uri uri = new Uri(#"pack://application:,,,/Sounds/FrogCroak.wav");
var player = new MediaPlayer();
player.Open(uri);
player.Play();
}
I also tried doing this, although I thought this one wouldn't work, but I tried it anyway:
void newFrog_MouseUp(object sender, MouseButtonEventArgs e)
{
Uri uri = new Uri(#"pack://application:,,,/Sounds/FrogCroak.wav");
System.Media.SoundPlayer player = new System.Media.SoundPlayer(uri);
player.Play();
}
I was not sure if the "pack://application:,,," was something that would refer to the file being played.
I also tried to make it as if it was an image, like so:
void newFrog_MouseUp(object sender, MouseButtonEventArgs e)
{
System.Media.SoundPlayer player = new System.Media.SoundPlayer(new Uri(#"pack://application:,,,/Sounds/FrogCroak.wav", UriKind.RelativeOrAbsolute));
player.Play();
}
So instead of new BitmapImage I did new System.Media.SoundPlayer and only to find out that it wouldn't work that way.
EDIT: I forgot to mention that this worked, but it did not play any sound.
void newFrog_MouseUp(object sender, MouseButtonEventArgs e)
{
Uri uri = new Uri(#"pack://application:,,,/Sounds/FrogCroak.wav");
var player = new MediaPlayer();
player.Open(uri);
player.Play();
}
Edit2: The possible duplicate technically didn't solve my problem, but according to the accepted answer it is not possible. Is there any other way I could play the audio using a different method?
This works for me using Visual Studio 2013 with a command line application:
Embed the .wav file as a resource (Resources.resx) in the project
I used Add Resource from Existing file
Set the persistence for the file as 'Linked at compile time'
Reference the file from code using something like this. Timer is the namespace this code is from and NotificationSound is what I called the embedded sound:
var notificationSound = new SoundPlayer(Timer.Properties.Resources.NotificationSound);
notificationSound.PlaySync();
This allowed me to play the sound from the application after moving the executable and renaming the original sound file (just to check that it was actually embedded).
I am currently working on a project where I need to simply display the users webcam on a
asp.image object. I am using the Aforge frame work and have gotten it to work on a windows app. In the windows app I would set up the video source variable equal to the image property of a picture box
In other words:
samplepicturebox1.image = videosource
The problem is, for asp there is only a asp:image object and the only property is .imageurl
imgSource.imageurl = ???
How would I go forth and link a video stream object to the image url or what other object would I use to display the stream? I have looked into putting an output on a seperate aspx.cs file so I could just use that as a imageurl but had no luck.
Here is my code to specify:
//using AForge.Video;
//using AForge.Video.DirectShow;
//using System.Drawing.Imaging;
public partial class WebForm1 : System.Web.UI.Page
{
private FilterInfoCollection VideoCaptureDevices;
private VideoCaptureDevice FinalVideo;
protected void Page_Load(object sender, EventArgs e)
{
drpSource.Items.Clear();
VideoCaptureDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
foreach (FilterInfo VideoCaptureDevice in VideoCaptureDevices)
{
drpSource.Items.Add(VideoCaptureDevice.Name);
}
drpSource.SelectedIndex = 0;
}
protected void btnStart_Click(object sender, EventArgs e)
{
FinalVideo = new VideoCaptureDevice(VideoCaptureDevices[drpSource.SelectedIndex].MonikerString);
FinalVideo.NewFrame +=new NewFrameEventHandler(FinalVideo_NewFrame);
FinalVideo.Start();
}
void FinalVideo_NewFrame(object sender, NewFrameEventArgs deventArgs)
{
imgSource.ImageUrl=(FinalVideo.ToString());
}
}
I have also populated a combo box with the user's different video source. That also displays correctly
I really appreciate any help. This will translate into communications. I want to be able to stream between users just like Omegle and Chatroulette. If anyone would recommend a better framework to look into I'm open, I've only looked into Aforge and Touchless as two C# frameworks that support video streaming.
I've seen many use flash and I do know a bit of ActionScript, but to be completely honest, I'd rather not mess too much with flash as ActionScript is quite the pain and from my opinion in some aspects, flash slowly withering and dieing.
#KeithNicholas is correct. A web application is not the same as a client/winforms application. A web app is run in the webserver and not in the web browser.
It got no access to the user's webcam. You need to use a client side technology like flash or silverlight to be able to use the webcamera from the server.
What am I doing:
My main intent is to enable user friendly text to speech for personal use on Win 7. Approach should work in Google Chrome, VS and Eclipse.
Code example:
Following code creates global keyboard hook for ctrl + alt + space, called hookEvent. If event fires, it starts/stops speaking clipboard contents ( that can be updated with ctrl + c ).
/// <summary>
/// KeyboardHook from: http://www.liensberger.it/web/blog/?p=207
/// </summary>
private readonly KeyboardHook hook = new KeyboardHook();
private readonly SpeechSynthesizer speaker = //
new SpeechSynthesizer { Rate = 3, Volume = 100 };
private void doSpeaking(string text)
{
// starts / stops speaking, while not blocking UI
if (speaker.State != SynthesizerState.Speaking)
speaker.SpeakAsync(text);
else
speaker.SpeakAsyncCancelAll();
}
private void hookEvent(object sender, KeyPressedEventArgs e)
{
this.doSpeaking(Convert.ToString(Clipboard.GetText()));
}
public Form1()
{
InitializeComponent();
hook.KeyPressed += new EventHandler<KeyPressedEventArgs>(hookEvent);
hook.RegisterHotKey(ModifierKeysx.Control|ModifierKeysx.Alt, Keys.Space);
}
Question:
I would prefer not using the clipboard. Or at least, restoring the value after, something like:
[MethodImpl(MethodImplOptions.Synchronized)]
private string getSelectedTextHACK()
{
object restorePoint = Clipboard.GetData(DataFormats.UnicodeText);
SendKeys.SendWait("^c");
string result = Convert.ToString(Clipboard.GetText());
Clipboard.SetData(DataFormats.UnicodeText, restorePoint);
return result;
}
What are my options?
Edit:
To my surprise, I found that my clipboard reader is the best way to go. I created a notification area app, that responds to left click (speaking clipboard) and right click (menu opens up). In menu the user can chance speed, speak or create a audio file.
MS provide accessibility tools that do cover what you're trying to do. If you take a look at documents about screen scraping. In short, every component is accessible in some manner, if you use some of the windows debugging tools you can get to see the component names/structures within. You can then use that, however, its complicated as most times you would need to be very specific for each application you intend to scrape from.
If you manage to scrape you dont need to use the clipboard, as you can access the text property of the apps direct. Its not something I've had to do, hence, Ive no code to offer off the top of my head, but the term "screen scraping" should point you in the right direction.
If to expand a little on what Bugfinder said, Microsoft provider a UI Automation Framework to solve problems like the one you mentioned:
In particular you can use the TextSelectionChangedEvent of TextPattern:
The problem with this solution is that it only works on supported operating systems and applications - and not all support this.
Your clipboard solution is acceptable for applications that do not provide a good automation interface.
But for many applications the UI Automation Framework will work well and will provide you with a far better solution.