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

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

Related

How to put a timer to appear on the screen?

I am developing a quiz in the visual studio with Windows Form but I have to put a timer with a countdown of 20s to appear on the screen and when it reaches 0 it takes to the last Form which is the "Game Over" how can I do that?
There are many ways to do this.
The samplist is you can add a timer(from Toolbox), set interval=1000, then enable a Tick function when the quiz started, count the seconds down, when the totalSeconds hits 0, display "Game Over".
private void startButton_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
if(totalSeconds > 0)
{
totalSeconds--;
this.timeDisplay.Text = totalSeconds.ToString();
}
else
{
this.timer1.Stop();
this.timeDisplay.Text = "Game Over";
}
}

WP7 background music play

In my WP7 app, I want 2 music files to run in background.I am using MediaElement to do this. I am facing two issues.
how to play in background?
How to loop background music?
This is how I do it. Add the following:
<MediaElement x:Name="meSong" />
This is added in the constructor:
meSong.MediaEnded += new RoutedEventHandler(meSong_MediaEnded);
This is how I loop the song once it has ended:
private void meSong_MediaEnded(object sender, RoutedEventArgs e)
{
meSong.Position = TimeSpan.Zero;
meSong.Play();
}
This is how I set my song:
private void SetSong(string selectedSong)
{
if (ViewModel.IsMusicOn)
{
try
{
meSong.Stop();
meSong.Source = new Uri(string.Format("Media/Sounds/{0}.wav", selectedSong), UriKind.Relative);
meSong.Position = new TimeSpan(0);
meSong.Volume = 0.5;
}
catch (Exception)
{
// nothing for now
}
}
}
And this is how you obviously start your music:
meSong.Play();

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

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.

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.

Resources