How to play songs in wpf c# without repeatition

Sep 10 2014 3:46 AM

Hi All

I have created an application in WPF C# in which i am playing number of songs one after another

But the songs are repeating

Please guide me with an solution how to stop repeating of songs

My Code

XML Code

<Window x:Class="RandomlyPlaySongs.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="500" Width="600">
<Grid>
<Button Name="btnPlayAudio" Content="Play Audio" HorizontalAlignment="Left" Margin="135,117,0,0" VerticalAlignment="Top" Width="97" Height="43" Click="btnPlayAudio_Click"/>
<Label Name="lblAudioNo" HorizontalAlignment="Left" Margin="26,235,0,0" VerticalAlignment="Top" Width="463" Height="47"/>
<Label x:Name="lblPlayNext" HorizontalAlignment="Left" Margin="26,308,0,0" VerticalAlignment="Top" Width="463" Height="47"/>
<Image x:Name="imgImage9" HorizontalAlignment="Left" Height="100" Margin="389,103,0,0" VerticalAlignment="Top" Width="100" Source="C:\Users\Public\Pictures\Sample Pictures\Image9.jpg" Stretch="UniformToFill"/>

</Grid>
</Window>

C# Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.Windows.Threading;

namespace RandomlyPlaySongs
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
Random randomNumber = new Random();
MediaElement myPlayer = new MediaElement();
List<int> playedFiles = new List<int>();

string[] loadMusic = Directory.GetFiles(@"C:\Users\kirti\Music\WavMusic");
int[] genNumber = new int[10];
int i;

public MainWindow()
{
InitializeComponent();
}
int playAudio;

private void btnPlayAudio_Click(object sender, RoutedEventArgs e)
{
generateRandNum();
loadAudioPlayer();

}
public void generateRandNum()
{
randomNumber = new Random();
playAudio = randomNumber.Next(loadMusic.Length);
}
public void loadAudioPlayer()
{
for (i = 0; i < genNumber.Length; i++)
{
myPlayer.Source = new Uri(loadMusic[playAudio]);
myPlayer.LoadedBehavior = MediaState.Manual;
myPlayer.UnloadedBehavior = MediaState.Manual;
myPlayer.Play();
lblAudioNo.Content = "The Audio Number is : " + loadMusic[playAudio];
lblPlayNext.Content = "Current Audio : " + playAudio.ToString();
}

}


public void loadCurrentNumber()
{
imgImage9.Name = "Image9";
if ((playAudio == 3) && (imgImage9.Name == "Image9"))
{
myPlayer.Stop();
MessageBox.Show("Right Selection");

}
else
{
MessageBox.Show("Next Song");
this.Close();
}
}

}
}
 
Regrads 

Answers (1)