SoundPlayer StartUpMusic = new SoundPlayer(Resources.Guiles_Theme);
private void MuteButton_Click(object sender, EventArgs e)
{
if (StartUpMusic.IsLoadCompleted == true)
{
StartUpMusic.Stop();
StartUpMusic.Dispose();
}
else
{
StartUpMusic.Load();
StartUpMusic.Play();
}
}
This is an event triggered when the user clicks the play button. I think my condition within the if statement is not good. I basically want the sound to be muted when the button is pressed. Then I want the sound to continue when the button is pressed and the sound is already muted. What is wrong here? You time and effort are greatly appreciated. Thank you!
Just use a boolean flag to indicate if the sound is playing or not. So something like this might work:
private boolean isPlaying = true;
private void MuteButton_Click(object sender, EventArgs e)
{
if (StartUpMusic.IsLoadCompleted)
{
if (isPlaying)
StartUpMusic.Stop();
else
StartUpMusic.Play();
isPlaying = !isPlaying;
}
}
Related
I know the solution on Android
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
switch(keyCode){
case KeyEvent.KEYCODE_BACK:
// do something here
return true;
}
return super.onKeyDown(keyCode, event); }
But i cant do it on the forms side. The focus is an Entry so the keyboard is visible and when i press hardware back button (keyboard disappear) i'd like to set my variable value.
Edited:
bool _tapped;
LwTeszt.PropertyChanged += (s, e) =>
{
if (LwTeszt.SelectedItem != null)
{
EntryTeszt.Focus();
}
};
EntryTeszt.Unfocused += EntryTeszt_Unfocus;
private void EntryTeszt_Unfocus(object sender, FocusEventArgs e)
{
_tapped = true;
}
private void ViewCell_Tapped(object sender, EventArgs e)
{
if (_tapped)
{
EntryTeszt.Unfocus();
_tapped = false;
return;
}
EntryTeszt.Focus();
}
Unless you have additional logic that you didn't post I think you might be overcomplicating things. I believe all you need to do is focus your entry on ViewCell_Tapped
private void ViewCell_Tapped(object sender, EventArgs e)
{
EntryTeszt.Focus();
}
Forms will handle unfocusing your Entry for you when back is pressed and in that case you won't need the LwTeszt.PropertyChanged, _tapped, and the EntryTeszt.Unfocused parts.
There is an Event you have to just override that and you can handle back button in Xamarin forms. Use below code for handling back button:
protected override bool OnBackButtonPressed()
{
// your code
return base.OnBackButtonPressed();
}
Also you can refer this question for more information.
Sorry, this may be a duplicate question, but I couldnot understand the solutions already provided in different answers.
I have created a mp3 player in a different manner, it plays one mp3 file at a time but one listbox have the chapters, which is not only handling to move position of that particular mp3 but also changes a picturebox image. Now somewhere I need to change the selection of the listbox from a seekbar but dont want to fire the following event of;
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
Please guide.
One way to inhibit your selection indexed change event doing its normal way is to use a boolean flag. Also, make sure that this inhibition does not stay around when some exception is raised:
private bool inhibit = true;
private void doSomeProcessWithInhibit()
{
try
{
inhibit = true;
// processing comes here
}
// if something goes wrong, make sure other functionality is not blocked
finally
{
inhibit = false;
}
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
// fast return to reduce nesting
if (inhibit)
return;
// do event handling stuff here
}
P.S. Try to use meaningful names for controls (check listBox1). You will thank yourself when revisiting the code and/or others have to.
Add a Boolean with class scope called something like isProcessing. Set it to true. Do your work, then set it to false. Warp your event in the Boolean:
bool isProcessing = true;
private void switchControls(){
isProcessing = true;
//do work;
isProcessing = false;
}
private void MyControl.OnEvent(object sender, EventArgs e){
if(!isProcessing){
//what you would normally do
}
}
OR....
Deregister the event, the re-register it
private void switchControls(){
myButton1.OnClick -= myButtonClick;
//do work
myButton1.OnClick += myButtonClick;
}
When changing AxWindowsMediaPlayer URL in PlayStateChange Event, it doesn't start playing automatically, just changes to "Ready" state.
I have an "AxWindowsMediaPlayer" Control in my C# WinForms program. when I normally change the URL property of WindowsMediaPlayer1, it works fine and plays new mp3 file automatically.
When the song ended WindowsMediaPlayer1 State changes to Stopped and I Want next URL automatically start Playing.
I used PlayStatChange event, so when player state is Stopped, URL Will change, but Not playing automatically!
The player goes to Ready State until I press the play button on the WindowsMediaPlayer1.
Here is the Code:
private void Form1_Load(object sender, EventArgs e)
{
WindowsMediaPlayer1.URL = "6.mp3"; //Works fine
}
private void button1_Click(object sender, EventArgs e)
{
WindowsMediaPlayer1.URL = "4.mp3"; //Works fine. It changes the music.
}
private void WindowsMediaPlayer1_PlayStateChange(object sender,
AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
if (e.newState == 1) //1 is for "Stopped" State
WindowsMediaPlayer1.URL = "5.mp3";
// Here is the problem.
// URL Will change but player goes to "Ready" State
// But not in "playing" until I press the play button in control.
}
Any help would be appreciated.
As mentioned in media player documentations, you should not set the Url from event handler code. Instead you can play next file this way:
private void axWindowsMediaPlayer1_PlayStateChange(object sender,
AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
if (e.newState == 1)
{
this.BeginInvoke(new Action(() => {
this.axWindowsMediaPlayer1.URL = #"address of nextfile";
}));
}
}
Also as another option you can consider using a playlist.
I found this note on msdn about player.URL:
"Do not call this method from event handler code. Calling URL from an event handler may yield unexpected results."
so I tried another way to solve it and its worked.
added a timer and a bool varible to check if WindowsMediaPlayer1 is "Stopped"
Here is the solution:
public partial class Form1 : Form
{
bool nextURL = false;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
WindowsMediaPlayer1.URL = "5.mp3";
}
private void WindowsMediaPlayer1_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
if (e.newState == 1) // 1 is consider for "Stopped" State
{
nextURL = true; // if the song ended "nextURL" flag sets to true
}
}
private void timer1_Tick(object sender, EventArgs e)
{
if (nextURL)
{
WindowsMediaPlayer1.URL = "6.mp3";
nextURL = false;
}
}
I've got the code, and all seems correct, I've had it reviewed and it seems impossible to find out why the button isn't doing what its coded to do. I'm making a Music player, and when I press the play button, it will be sent to the back and the pause button will become visible, when I next click the pause button, nothing happens and its primary function stops working all together. Here is the code for people to examine.
private void btnPlay_Click(object sender, EventArgs e)
{
try
{
if (_mp3Player != null)
_mp3Player.Play();
btnPlay.SendToBack();
btnPause.BringToFront();
}
catch (Win32Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void btnPause_Click(object sender, EventArgs e)
{
if (_mp3Player != null)
_mp3Player.Stop();
btnPause.SendToBack();
btnPlay.BringToFront();
}
Perhaps it would be best to use visibility?
private void btnPause_Click(object sender, EventArgs e)
{
if (_mp3Player != null)
{
_mp3Player.Stop();
}
btnPause.Visible = False;
btnPlay.Visible = True;
}
Or even the enabled property?
... btnPause.Enabled = false; ...
However I feel you could make it better by having it be the same button, with a value, so just have a value on it of true if it's player or false if it's pause and then in the click event check against that value to determine what it is currently and then just execute the relevant functionality and change the text or image that you have on the button.
I am making a application where i would like to log in with a face detection. But this is not real, its just to make ik look like its scanning.
So when i press the LOG IN button, the kinect takes my picture and show me the picture, on top of it is showing me in a text that its scanning.
Now i am stuck with the following issue, when i press the login button, the scanning label appears, but i would like to fire an other event that takes me to the next page, the homepage.
So i want the SCANNING label appearing for 3 seconds, and then the page should change.
This is what i tried, i worked with a timer, but that doesnt do annything.
private void ActionButton_Click(object sender, System.EventArgs eventArgs)
{
_main.TakePicture();
identifyBox.Source = _main.source.Clone();
scanningLabel.Visibility = Visibility.Visible;
_storyboard = (Storyboard)FindResource("scanningSB");
//_storyboard.Begin();
Start();
}
private void Start()
{
_tm = new Timer(3000);
_tm.Elapsed += new ElapsedEventHandler(_tm_Elapsed);
_tm.Enabled = true;
}
void _tm_Elapsed(object sender, ElapsedEventArgs e)
{
if (_tm == new Timer(3000))
{
((Timer)sender).Enabled = false;
_main.ContentPage.Children.Clear();
_main.ContentPage.Children.Add(_homeScreen);
}
}
Okay i removed the if statement, but now it fires every 3 seconds a method.
How can i make it work 1 time.
Ok even this works, now i my ContentPage wont change? It gives me this error: The calling thread cannot access this object because a different thread owns it.
What can be wrong?
I think you can remove condition
if (_tm == new Timer(3000))
and keep it simple
void _tm_Elapsed(object sender, ElapsedEventArgs e)
{
((Timer)sender).Enabled = false;
_main.ContentPage.Children.Clear();
_main.ContentPage.Children.Add(_homeScreen);
}
when you set _tm = new Timer(3000); it will set the time to fire event after 3 seconds..
Change the _tm_Elapse to this:
void _tm_Elapsed(object sender, ElapsedEventArgs e)
{
if (_tm == (sender as Timer))
{
_main.ContentPage.Children.Clear();
_main.ContentPage.Children.Add(_homeScreen);
}
}
Edit for answering:
"I just want it 1 time to fire after 3 sec"
void _tm_Elapsed(object sender, ElapsedEventArgs e)
{
if (_tm == (sender as Timer))
{
_tm.Stop();
_main.ContentPage.Children.Clear();
_main.ContentPage.Children.Add(_homeScreen);
}
}
This is even simpler, and it will only run once per call, the three second timer is built in, and furthermore, it won't disable other program functionalities while it is running:
async Task Start()
{
await Task.Delay(3000);
_main.ContentPage.Children.Clear();
_main.ContentPage.Children.Add(_homeScreen);
}