Playing sounds in WPF as a resource [duplicate] - c#

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).

Related

How to fix get variables from swf file or flash player in C# WinForms

Hi I am a novice programmer I was trying to play a swf file in WinForm and language is C#
private AxShockwaveFlash Player;
private void CreatePlayer()
{
Player = new AxShockwaveFlash();
Player.Location = new Point(30, 10);
Player.Size = new Size(1024, 640);
Controls.Add(Player);
Player.Menu = false;
Player.Loop = false;
Player.WMode = "Direct";
Player.AllowScriptAccess = "Always";
Player.Quality2 = "High";
Player.LoadMovie(0, path);
}
here I created my player and I added a simple button and its click event
private void button1_Click(object sender, EventArgs e)
{
try
{
Console.WriteLine(Player.GetVariable("_root.wildPokemon.name"));
}
catch(Exception ex) { Console.WriteLine(ex.Message);
}
}
but when I try to player.GetVariable("_root.wildPokemon.name") the variable called _root.wildPokemon.nameits giving me this error Error HRESULT E_FAIL has been returned from a call to a COM component. Is there anything I can do to fix this error? I don't know I gave you guys all information or not about this project of mine. If I missed some information please let me know and here is the action script where I found those variables in the scripts/Frame 3 after decompiling swf file and I don't know is this Action Script 3 or not I think it can be Action Script. Here is the script and I am also giving the swf file Here . One more thing unfortunately I can't edit the swf file coz I want a variable of an online swf game and I can't edit that one. The swf file requires or uses http connection at port 8080 and another one at port 9339. And because of that it becomes a problem coz without the websites swf I can't login here is the link which I used as movie but it looks like it is encrypted or something.

C#, No video, audio only, VLC using multiple forms, Black Screen?

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

Youtube Video Player in C#

How do I embed a youtube video into my C# project?
I've looked at so many others that "answer" this question but, I can't find what I'm looking for.
I've tried many different ways to embed them but, they don't work.
I'm looking to use a web browser to just play the video but, with two options: loop and pause/play.
Reason I'm doing this is because I'm making a twitch chat bot and I'm trying to add song requests.
Latest attempt (because it was there so I decided on trying it out):
bool MUrlSpecify = true;
private void btnMSetCurrent_Click(object sender, EventArgs e)
{
const string page = "<html><head><title></title></head><body>{0}</body></html>";
string YTUrl;
if(MUrlSpecify == true)
{
YTUrl = "https://www.youtube.com/embed/" + txtMSetCurrent.Text;
wbMPlayer.DocumentText = string.Format(page, "");
}
}
(MUrlSpecify is a variable I added because later I'm going to have a search function)
If anyone can tell me how I should do this I'll try their method because I'm all
out of ideas.
Try to use webbrowser.NavigateToString("player code");
Get the code from share button in youtube. Work perfectly.
Example: wb.NavigateToString("<iframe width=\"420\" height=\"315\" src=\"https://www.youtube.com/embed/dSrozYNxAA4\" frameborder=\"0\" allowfullscreen></iframe>");

Cannot display webcam Using Aforge.NET in C#

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.

Selecting sounds from Windows and playing them

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();
}

Categories