Play SoundEffect only on first time page load - windows-phone-7

WP7.5/Silverlight App...
On my page load, I play a Sound clip (e.g. Hello! Today is a wonderful day.)
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
seLoadInstance = seLoad.CreateInstance(); //I initialize this seLoad in Initialize method
seLoadInstance.Play();
}
Now I have 3-4 other elements on the page. When user click on any of them, a sound clip for that element plays.
private void ElementClick_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
seElementInstance = seElement.CreateInstance();
seElementInstance .Play();
}
What I want is:
When the page first loads and while the seLoadInstance is being played and user clicks the element, I don't want the seElementInstance to be played.
I can check the state of seLoadInstance like below to not play seElementInstance
private void ElementClick_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if(seLoadTextInstance.State != SoundState.Playing)
{
seElementInstance = seElement.CreateInstance();
seElementInstance .Play();
}
}
But the problem with above is that I have another element that can play the seLoadInstance on it's click.
Problem: I don't know how to differentiate if the seLoadInstance being played is first time or upon element click.
Possible solution: One way I see is using different instances to play the same sound.
I was hoping some better way like I set a flag upon load but I couldn't find any explicit event for SoundInstance completed or Stopped that I can handle.
Any ideas??

Have not used sounds until now but what I have seen:
Why do you always create new instances when you want to play a sound?
Isn't it possible to create a instance for both "se"-elements and cust check if anyone is running before calling "play"?
For example:
private var seLoadInstance;
private var seElementInstance;
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
seLoadInstance = seLoad.CreateInstance();
seElementInstance = seElement.CreateInstance();
seLoadInstance.Play(); // no need to check if something is playing... nothing will be loaded
}
private void ElementClick_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if(seLoadInstance.State != SoundState.Playing && seElementInstance.State != SoundState.Playing)
{
seElementInstance .Play();
}
}

I was able to find a way using flag. Instead of setting a flag upon the firsttime load complete, I set the flag from one of my element that plays the seLoadTextInstance.
Something like below:
private bool isElementLoadSoundPlaying = false; //I set this to true below in another handler
private void ElementClick_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
//This if means LoadTextInstance is playing and it is the first time play
if(seLoadTextInstance.State != SoundState.Playing && isElementLoadSoundPlaying == false )
{
return;
}
seElementInstance = seElement.CreateInstance();
seElementInstance .Play();
}
private void ElementLoadTextClick_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
isElementLoadSoundPlaying = true;
seLoadInstance = seLoad.CreateInstance();
seLoadInstance.Play();
}

Related

Lottie animationView isn't playing in XamarinForms after I change the source

When I try to change the source of an animationview to simulate play-stop solution, the source of the animationview is changed successfully, but when I click for the second time, it doesn't play the animation.
Here is my code:
private void AnimationView_OnClick(object sender, EventArgs e)
{
animationView.Play();
}
and on finish when I try to change the source:
private void AnimationView_OnFinish(object sender, EventArgs e)
{
if (animationView.Animation == "play_to_pause.json")
animationView.Animation = "pause_to_play.json";
else
animationView.Animation = "play_to_pause.json";
}
What am I doing wrong?
It will be your json files. I tried to recreate your issue and I had no problem.

How do I listen to UWP Xaml Slider manipulation start/end events?

What events should I listen to on a UWP Xaml Slider to determine when the user begins and ends manipulation.
This functionality is important when you have a slider that represents some continuously changing app state (say, an animation time) and you want to pause the update when the user interacts with the slider.
This question has been answered for WPF and Windows Phone, but not UWP. The other solutions do not work, or are incomplete, for UWP.
You need to listen to interaction events from a couple of the elements of the Slider template: the Thumb, and the Container. This is because the user can manipulate the thumb directly by clicking and dragging it, but also they can click anywhere on the slider and the thumb will jump to that location (even though it looks like you are then manipulating the Thumb, actually the thumb is just being relocated every time the mouse moves - you are still interacting with the container).
There are a couple caveats:
the thumb and container both process their input events and do not pass them on, so you need to use the AddHandler method of attaching RoutedEvent handlers so that you get events which have already been processed.
you need to attach the event handlers after the control template has been applied, which means you need to subclass the Slider to override a protected method.
The RoutedEvent handler information is covered here: https://learn.microsoft.com/en-us/windows/uwp/xaml-platform/events-and-routed-events-overview#registering-handlers-for-already-handled-routed-events
The following SliderEx class adds some events which can be used to detect when the user begins/ends interacting with the slider:
public class SliderEx : Slider
{
public event EventHandler SliderManipulationStarted;
public event EventHandler SliderManipulationCompleted;
public event EventHandler SliderManipulationMoved;
private bool IsSliderBeingManpulated
{
get
{
return this.isContainerHeld || this.isThumbHeld;
}
}
private bool isThumbHeld = false;
private bool isContainerHeld = false;
protected override void OnApplyTemplate()
{
base.OnApplyTemplate();
var thumb = base.GetTemplateChild("HorizontalThumb") as Thumb;
if (thumb == null)
{
thumb = base.GetTemplateChild("VerticalThumb") as Thumb;
}
if (thumb != null)
{
thumb.DragStarted += this.Thumb_DragStarted;
thumb.DragCompleted += this.Thumb_DragCompleted;
thumb.DragDelta += this.Thumb_DragDelta;
}
var sliderContainer = base.GetTemplateChild("SliderContainer") as Grid;
if (sliderContainer != null)
{
sliderContainer.AddHandler(PointerPressedEvent,
new PointerEventHandler(this.SliderContainer_PointerPressed), true);
sliderContainer.AddHandler(PointerReleasedEvent,
new PointerEventHandler(this.SliderContainer_PointerReleased), true);
sliderContainer.AddHandler(PointerMovedEvent,
new PointerEventHandler(this.SliderContainer_PointerMoved), true);
}
}
private void SliderContainer_PointerMoved(object sender,
Windows.UI.Xaml.Input.PointerRoutedEventArgs e)
{
this.InvokeMove();
}
private void SliderContainer_PointerReleased(object sender,
Windows.UI.Xaml.Input.PointerRoutedEventArgs e)
{
this.SetContainerHeld(false);
}
private void SliderContainer_PointerPressed(object sender,
Windows.UI.Xaml.Input.PointerRoutedEventArgs e)
{
this.SetContainerHeld(true);
}
private void Thumb_DragDelta(object sender, DragDeltaEventArgs e)
{
this.InvokeMove();
}
private void Thumb_DragCompleted(object sender, DragCompletedEventArgs e)
{
this.SetThumbHeld(false);
}
private void Thumb_DragStarted(object sender, DragStartedEventArgs e)
{
this.SetThumbHeld(true);
}
private void SetThumbHeld(bool held)
{
bool wasManipulated = this.IsSliderBeingManpulated;
this.isThumbHeld = held;
this.InvokeStateChange(wasManipulated);
}
private void SetContainerHeld(bool held)
{
bool wasManipulated = this.IsSliderBeingManpulated;
this.isContainerHeld = held;
this.InvokeStateChange(wasManipulated);
}
private void InvokeMove()
{
this.SliderManipulationMoved?.Invoke(this, EventArgs.Empty);
}
private void InvokeStateChange(bool wasBeingManipulated)
{
if (wasBeingManipulated != this.IsSliderBeingManpulated)
{
if (this.IsSliderBeingManpulated)
{
this.SliderManipulationStarted?.Invoke(this, EventArgs.Empty);
}
else
{
this.SliderManipulationCompleted?.Invoke(this, EventArgs.Empty);
}
}
}
}

Auto play and next each song after each song fishish on wpf 8.1. How do that?

Here is code play music when choosen item on listbox but i don't know do auto playing and next each song....
private void lst_album_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (currentSongindex != -1)
{
currentSongindex = lst_album.SelectedIndex;
}
currentSongindex++;
if (currentSongindex < lst_album.Items.Count)
{
mymedia.Source = new Uri((lst_album.SelectedItem as Data.Data).link);
mymedia.Play();
}
}
You can put logic to play next song on current song finished in MediaEnded event handler method.
XAML :
<MediaElement Name="mymedia" MediaEnded="mymedia_MediaEnded"
......... />
Code-behind :
private void mymedia_MediaEnded(object sender, EventArgs e)
{
//check if next song available, play next song
if (currentSongindex < lst_album.Items.Count)
{
mymedia.Source = new Uri((lst_album.Items[currentSongindex] as Data.Data).link);
mymedia.Play();
}
}

Selection changed event also called Lostfocus event?

NET C# ,
In my windows phone 7.5 application , I want to make visible the application bar if any item has selected .. So I am making it visible in selected change event. But what is happening in my code is when ever selection change it also triggers LostFocus event and in that event I am making selected index = 0.
Now the resultant of the code is when ever I select any item , application bar gets visible then automatically invisible ( because of lost focus event).
Following is the piece of code .
private void ShopingListItemDetails_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (ShopingListItemDetails.SelectedIndex != -1)
{
ApplicationBar.IsVisible = true;
int selind = ShopingListItemDetails.SelectedIndex;
}
}
private void ShopingListItemDetails_LostFocus(object sender, RoutedEventArgs e)
{
ApplicationBar.IsVisible = false;
ShopingListItemDetails.SelectedIndex = -1;
}
I am just at start with .NET C#(XAML) so assuming that selection change event is also triggering LostFocus event.
Please help me what is the real problem behind.Thanks
Zauk
You can use the following hack. Initialize a variable, say selectChanged to False initially in the xaml.cs. In SelectionChanged function change it to True. Now, in the LostFocus function do processing only if the selectChanged variable is false, and if it is true set it back to False
Boolean selectChanged=false;
private void ShopingListItemDetails_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (ShopingListItemDetails.SelectedIndex != -1)
{
ApplicationBar.IsVisible = true;
int selind = ShopingListItemDetails.SelectedIndex;
selectChanged=true;
}
}
private void ShopingListItemDetails_LostFocus(object sender, RoutedEventArgs e)
{
if(!selectChanged)
{
ApplicationBar.IsVisible = false;
ShopingListItemDetails.SelectedIndex = -1;
}
selectChanged=false;
}
I think this should solve your problem.

Play loop of sound in WP7

i need to play a sound on the touch of screen, and it should remain in play state until user get his hands off from the screen. here is the code of mine,
private void OnMouseDown(object sender, Moenter code hereuseButtonEventArgs e)
{
clicked = true;
ColoringSound.Source = new Uri("Sounds/drawing.mp3", UriKind.Relative);
ColoringSound.Play();
}
private void OnMouseUp(object sender, MouseButtonEventArgs e)
{
clicked = false;
}
There is an MSDN article that describes how to loop MediaElements, all you have to do to stop a looped sound is invoke the Stop method.

Resources