Play loop of sound in WP7 - windows-phone-7

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.

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.

Play SoundEffect only on first time page load

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

How do I prevent SoundEffect from playing before the page is fully loaded

In my windows phone 7 app I'm using the following code to navigate to a page
private void button2_Click(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/Views/Game.xaml, UriKind.RelativeOrAbsolute));
}
In the OnNavigatedTo event of Game.xaml.cs I use the following code to play two wav files one after another. In order for me to prevent both audio play at the same time, I use while loop to wait until the first audio is finish before the second file is played. I believe this is what causing the first audio to play before the game page is displayed. Is there a better way of doing this?
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
playAudio("intro.wav");
while (MySoundEffect.State == SoundState.Playing)
{
}
playAudio("firstQuestion.wav");
base.OnNavigatedTo(e);
}
The following is the code for playAudio.
protected void playAudio(string fileName)
{
Stream stream = TitleContainer.OpenStream("audio/" + fileName);
SoundEffect effect = SoundEffect.FromStream(stream);
MySoundEffect = effect.CreateInstance();
FrameworkDispatcher.Update();
MySoundEffect.Play();
}
MySoundEffect is a property of the Game class.
private SoundEffectInstance _MySoundEffect;
public SoundEffectInstance MySoundEffect
{
get { return _MySoundEffect; }
set { _MySoundEffect = value; }
}
Whenever user click the button, the sound
To answer your question in the title, you could try the same with the
Loaded
event handler instead of the OnNavigateTo. This way, your audio files will start playing
only after the page is loaded.

Alarm feature in Windows Phone 7

I am trying to controll the snooze and dismiss button when the alarm sound in windows phone 7.
But i couldnt find any source code example on it.
Can anyone help me with it?
What i want is like when i clicked on the snooze or dimiss button it will do something such as the dismiss button will naviagte to another page.
And one more thing is that when the alarm is triggered can i set it to play some music??
Because the one that i have tried out does not play any music.
private static void CreateAlarm(string title, string time)
{
var alarm = new Alarm(title)
{
Content = "You have a meeting with your team now.",
BeginTime = Convert.ToDateTime(time),
};
ScheduledActionService.Add(alarm);
}
private static void ResetAlarm()
{
ScheduledActionService.Remove("MyAlarm");
}
private void SetAlarm_Click(object sender, RoutedEventArgs e)
{
string time = Convert.ToString(timePicker1.ValueString);
CreateAlarm(txtTitle.Text, time);
}
private void ResetAlarm_Click(object sender, RoutedEventArgs e)
{
ResetAlarm();
}
}
The Alarm class has a .Sound property to controls the sound file that is played.
To use it, say you have Alarm.mp3 in your project under the Sounds folder with it's build action set to Content.
var alarm = new Alarm() {
Sound = new Uri('Sounds/Alarm.mp3', UriKind.Relative)
}
There is no way to respond to snooze or dismiss that I have seen.

How to determine orientation of Windows Phone 7?

How can you tell whether the device is oriented vertically (portrait) or horizontally (landscape)?
Is there an API that simplifies this or do you have to make the determination "by hand" using the accelerometer?
I myself just have looked at windows 7 phones(through vs2010 express phone edition).
It seems to have in the code behind this
public MainPage()
{
InitializeComponent();
// seems to set the supported orientations that your program will support.
SupportedOrientations = SupportedPageOrientation.Portrait | SupportedPageOrientation.Landscape;
}
Then the actual form has
private void PhoneApplicationPage_OrientationChanging(object sender, OrientationChangedEventArgs e)
{
var test = e.Orientation;
}
So when the orientation changes it e.Orientation will tell you what orientation it is. Like for instance LandscapeRight.
Also you don't have to track this only via the event, you can ask for it directly from the PhoneApplicationPage instance:
private void Button_Click(object sender, RoutedEventArgs e)
{
MyCurrentOrientation.Text = this.Orientation.ToString();
}
You can also ask it through this.Orientation when your application starts so you know what the orientation is. Afther the start you can use the OrientationChanged event.
In your main:
OrientationChanged += new EventHandler<OrientationChangedEventArgs>(MainPage_OrientationChanged);
void MainPage_OrientationChanged(object sender, OrientationChangedEventArgs e)
{
Console.WriteLine(e.Orientation.ToString());
}

Resources