I want to make 2 button. 1 of them to play music and another one to stop the music. But i make just the first one. How i make the stop music button ?
private void Play()
{
string soundfile = #"D:\song.wav";
var sound = new System.Media.SoundPlayer(soundfile);
sound.Play();
}
private void button1_Click(object sender, EventArgs e)
{
Play();
}
The SoundPlayer class you're using has a method to stop playback, just like it has Play. The problem is that you're not storing the instance of SoundPlayer that's playing, so you don't have a reference to it.
Try storing the sound variable as a class-level member. Then you can call it from a different method as well:
private SoundPlayer _player;
private void Play()
{
string soundFile = #"D:\Song.wav";
_player = new System.Media.SoundPlayer(soundFile);
_player.Play();
}
private void Stop()
{
if (_player != null)
{
_player.Stop();
}
}
This way the SoundPlayer is a shared member for all class methods. Note, though, the != null check, which makes sure that calling Stop before Play won't crash.
Related
I want to use MediaPlayer class to play .flv file int UWP app. Here are some test code is't very easy, but it doesn't work. If I play .mp4 file, it's OK, what have to do to play .flv file?
namespace mediaPlayer
{
public sealed partial class MainPage : Page
{
private MediaPlayer player = null;
public MainPage()
{
this.InitializeComponent();
}
private void Start_Click(object sender, RoutedEventArgs e)
{
mediaPlayer.Source = MediaSource.CreateFromUri(new Uri("http://10.160.72.72/vod/1987.flv "));
player = mediaPlayer.MediaPlayer;
player.Play();
}
private void Pause_Click(object sender, RoutedEventArgs e)
{
player.Pause();
}
private void Stop_Click(object sender, RoutedEventArgs e)
{
player.Dispose();
}
}
}
I don't think it's possible. MediaPlayer can't play .flv format. Read this link:
I would suggest you convert it to different format: https://msdn.microsoft.com/en-us/library/windows/apps/hh986969.aspx
One possible way is this Player Framework. Haven't tried it out, but it should play .flv format.
Hope it helps!
You could use FFMpegInterop. It isn't easy to set up but the Github page for it and articles online could help you get it up and running, I used it for a project in the past and it has worked for me.
I've stomped with a problem I've spent some hours trying to solve, with my very limited knowledge.
I have a listview in my form1 called listMachine
And I have a method in form1.cs such as
private void máquinaToolStripMenuItem_Click(object sender, EventArgs e)
{
machinename open = new machinename();
open.Show();
}
machinename.cs is another form, and I use that method to open my other form, with an object called open.
the machinename button is a simple form which just serves as an input receiver, it asks a name, we have to type it into the textbox, press a button and it receives the input.
This is the code that runs when you press the button
public void buttonAceitarnome_Click(object sender, EventArgs e)
{
if (textBoxnomenova.TextLength == 0)
{
toolTipEmptyname.Show("O nome da máquina não pode estar vazio", textBoxnomenova);
}
else
{
Variables.var = textBoxnomenova.Text;
//MessageBox.Show(Variables.var); debug purpose, the messagebox does carry variables.var text
obj.listMachine.Items.Add(Variables.var); //If I change the variables.var to "test" it will NOT add the item.
this.Close();
}
}
Also, I forgot to mention my Variables.cs class, I created it because it was the only way I found to pass variables from a class to another (machinename.cs to form1.cs), but still, the items are not added into the listview.
This is my variables.cs code
public static class Variables
{
public static string var;
}
The comments I added to the code also give you some extra debug info..
I didn't want to ask for online help, but couldn't solve this on my own :(
If I were you, I would first remove the Variables class.
Then, you'r first form/class is called obj.cs, am I right? Or is it form1.cs?
I made it look like this:
public partial class obj : Form
{
public static string text; //This is a variable that can be reached from
public obj()
{
InitializeComponent();
}
private void máquinaToolStripMenuItem_Click(object sender, EventArgs e)
{
machinename open = new machinename();
open.ShowDialog(); //I put ShowDialog instead of Show
addItem(); //This method is called when the showed dialog is closed (machinename.cs)
}
private void addItem()
{
listMachine.Items.Add(text);
}
}
and the machinename.cs class like this:
public partial class machinename : Form
{
public machinename()
{
InitializeComponent();
}
private void buttonAceitarnome_Click(object sender, EventArgs e) //This one can be private
{
if (textBoxnomenova.TextLength == 0)
{
//Something here
}
else
{
obj.text = textBoxnomenova.Text; //Initializing the public static variable
this.Close(); //Closes the form, next step will be to run the method in obj.cs
}
}
}
If I understood your question correctly, you wanted to add an item to the ListView called "listMachine" via a button in the form "machinename.cs". This code will do that. I hope it helps you.
Change the click event from private to protected.
protected void máquinaToolStripMenuItem_Click(object sender, EventArgs e)
I have 2 custom controls in my silverlight application and each one is having its own animations. I want to play all of the animations in order but when they play at the same time, the animations are not playing properly. How can I put some delay between them?
private void button1_Click(object sender, RoutedEventArgs e)
{
Dispatcher.BeginInvoke(() => {
myCustomControl1.Play();
Thread.Sleep(200);
Dispatcher.BeginInvoke(() => { myCustomControl2.Play(); });
}
I'm assuming your controls are using a StoryBoard to play the animations. StoryBoard has a "Completed" event that fires after it completes running. So, you could do something like:
public class CustomControl:Control
{
public override void ApplyTemplate()
{
_storyBoard=GetTemplateChild("AnimationStoryBoard") as Storyboard;
_storyBoard.Completed+=OnCompleted;
}
public event eventhandler AnimationCompleted;
public void Play()
{
_storyBoard.Begin();
}
private void OnCompleted(object sender, EventArgs args)
{
if(AnimationCompleted!=null)
AnimationCompleted(this,EventArg.Empty);
}
}
What you can then do is chain your animations together:
myCustomControl1.AnimationCompleted+=RunSecondAnimation;
myCustomControl1.Play();
private void RunSecondAnimation(object sender, EventArgs args)
{
myCustomControl2.Play();
}
Now we can guarantee that animation 2 will always happen after animation 1.
I am creating a WP7 application that requires various sound effects to be played (on button press) over looped background music. The background music is initiated by pressing Button 1 and loops fine. When I press button3 (triggers a sound effect), the sound effect overlays on the background music fine on first press. However, when I press button3 again, the background music stops. I cannot figure out why this might be happening!? I have pasted the relevant portions of code below. Would appreciate any help.
public partial class MainPage : PhoneApplicationPage
{
SoundEffect soundEffect;
Stream soundfile;
// Constructor
public MainPage()
{
InitializeComponent();
}
static protected void LoopClip(SoundEffect soundEffect)
{
{
SoundEffectInstance instance = soundEffect.CreateInstance();
instance.IsLooped = true;
FrameworkDispatcher.Update();
instance.Play();
}
}
public void PlaySound(string soundFile)
{
using (var stream = TitleContainer.OpenStream(soundFile))
{
var effect = SoundEffect.FromStream(stream);
effect.Play();
}
}
private void button1_Click(object sender, RoutedEventArgs e)
{
soundfile = TitleContainer.OpenStream("BackgroundMusic.wav");
soundEffect = SoundEffect.FromStream(soundfile);
LoopClip(soundEffect);
}
private void button3_Click(object sender, RoutedEventArgs e)
{
PlaySound("sound3.wav");
}
}
}
This should work if you are always working with Instances so change your code to this and it should clear up the problem:
public partial class MainPage : PhoneApplicationPage
{
SoundEffectInstance loopedSound = null;
// Constructor
public MainPage()
{
InitializeComponent();
}
static protected void LoopClip(SoundEffect soundEffect)
{
loopedSound = soundEffect.CreateInstance();
loopedSound.IsLooped = true;
loopedSound.Play();
}
public void PlaySound(string soundFile)
{
SoundEffect sound = SoundEffect.FromStream(Application.GetResourceStream(new Uri(soundFile, UriKind.Relative)).Stream);
SoundEffectInstance instance = sound.CreateInstance();
instance.Play();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
SoundEffect sound = SoundEffect.FromStream(Application.GetResourceStream(new Uri(#"BackgroundMusic.wav", UriKind.Relative)).Stream);
LoopClip(sound);
}
private void button3_Click(object sender, RoutedEventArgs e)
{
PlaySound("sound3.wav");
}
}
The above example assumes your sound files are set with Build Action = Content and are in the top level directory.
You will need to play each sound from a separate thread.
What seems to be happening here is that the different Play method calls are interfering with each other since they are in the same thread.
Try just putting the background music in a separate thread and see if that solves the problem you mention in the question. If so, split the others out as well.
In WPF you get to call ShowDialog on a window exactly once. After that it is done for.
Seems kind of lame to me, but those are the rules. If you call ShowDialog again you get this exception:
Cannot set Visibility or call Show, ShowDialog, or WindowInteropHelper.EnsureHandle after a Window has closed
What I want to know is: How can I take a Window (or UserControl really) and check to see if it has had ShowDialog called (so I know to new up a different one before calling ShowDialog again).
Something like this:
public void ShowListOfClients()
{
// | This is the method I want to write
// V
RefreshViewIfNeeded(_myWindowOrUserControlThatShowsAList);
FillWindowWithBusinessData(_myWindowOrUserControlThatShowsAList);
_myWindowOrUserControlThatShowsAList.ShowDialog();
}
NOTE: Clearly in the above example it would be easier to just create a new WindowOrUserControlThatShowsAList every time I enter the method. But please consider the question more that the dumbed down example.
This isn't exclusive to ShowDialog(), Show() does it too. And no, there is no IsDisposed property to check. IsLoaded is only half a solution, it will be false for the 1st invocation as well.
First approach is to just make a dialog that can be re-shown:
public bool CloseAllowed { get; set; }
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) {
if (!CloseAllowed) {
this.Visibility = System.Windows.Visibility.Hidden;
e.Cancel = true;
}
}
The next one is to explicitly keep track of the health of the object reference:
private Window1 win = new Window1(); // say
private void button1_Click(object sender, RoutedEventArgs e) {
if (win == null) {
win = new Window1();
win.Closing += delegate { win = null; };
}
win.ShowDialog();
}
Well the dirty way to do it would be to catch the exception.
The clean way to do it would be to show a window with ShowDialog, and destroy (lose reference to, etc) the window when the function returns. The view should not be tightly coupled with the models (you are using MVVM right?) so creating new visual objects for each client view should not be an issue.
Easy way to deal with this problem without messing up with the Closing event :
public partial class MainWindow
{
private SomeCustomWindow _someCustomWindow;
public MainWindow()
{
InitializeComponent();
}
private void OnOpenCustomWindowButtonClick(object sender, RoutedEventArgs e)
{
if (_someCustomWindow != null)
_someCustomWindow.Close();
_someCustomWindow = new SomeCustomWindow();
_someCustomWindow.ShowDialog();
}
private void OnWindowClosing(object sender, CancelEventArgs e)
{
if (_someCustomWindow!= null)
_someCustomWindow.Close();
}
}