There was a change in requirement and I am not using the MediaElement. I am using NAudio library to play the media files as the requirement is to play media files through ASIO driver.
However, I tried to code the above issue with NAudio and it seems to be working.
Please have a look at the below sample code. It's not a tested code.
using System;<br />
using System.Collections.Generic;<br />
using System.Linq;<br />
using NAudio.Wave;<br />
using System.Windows.Threading;<br />
using System.Diagnostics;<br />
using System.IO;<br />
<br />
namespace NAudioConsoleDemo<br />
{<br />
class Program<br />
{<br />
private static List<iwaveplayer> _mediaPlayers = new List<iwaveplayer>(2);<br />
private static List<wavestream> _mediaStreams = new List<wavestream>(2);<br />
<br />
private static string _fileName1 = @"D:\Audio Files\Song skip\911.wav";<br />
static Stopwatch _watch = new Stopwatch();<br />
<br />
static void Main(string[] args)<br />
{ <br />
Console.WriteLine("Started");<br />
<br />
Play();<br />
<br />
Console.ReadLine();<br />
}<br />
<br />
private static void Play()<br />
{<br />
for(int index = 0; index < 2; index++)<br />
{<br />
_mediaPlayers.Add(CreatePlayer());<br />
if (_mediaPlayers[index] != null)<br />
{<br />
_mediaPlayers[index].PlaybackStopped += MediaPlayerOnPlaybackStopped;<br />
}<br />
_mediaStreams.Add(CreateMediaStream(_fileName1));<br />
}<br />
<br />
int counter = 0;<br />
<br />
foreach (var mediaPlayer in _mediaPlayers)<br />
{<br />
if (_mediaStreams[counter] != null)<br />
{<br />
mediaPlayer.Init(_mediaStreams[counter]);<br />
mediaPlayer.Play();<br />
_watch.Start();<br />
Console.WriteLine("[{0}] Media Playing... ", counter);<br />
System.Threading.Thread.Sleep(1000);<br />
}<br />
counter++;<br />
}<br />
}<br />
<br />
private static IWavePlayer CreatePlayer()<br />
{<br />
return new WaveOutEvent();<br />
}<br />
private static object lockObj = new object();<br />
<br />
protected static void MediaPlayerOnPlaybackStopped(object sender, StoppedEventArgs e)<br />
{<br />
_watch.Stop();<br />
<br />
var mediaPlayer = sender as IWavePlayer;<br />
if (mediaPlayer != null)<br />
{<br />
int index = _mediaPlayers.IndexOf(mediaPlayer);<br />
var mediaStream = _mediaStreams.ElementAt(index);<br />
Dispose(mediaPlayer, mediaStream);<br />
_mediaStreams.RemoveAt(index);<br />
_mediaPlayers.RemoveAt(index);<br />
Console.WriteLine("[{0}] Media Stopped", index);<br />
Console.WriteLine("[{0}] Total Time: {1}", index, _watch.Elapsed.TotalSeconds);<br />
} <br />
<br />
if (_mediaPlayers.Count == 0)<br />
{<br />
File.Delete(_fileName1);<br />
Console.WriteLine("Media Deleted");<br />
}<br />
}<br />
<br />
protected static WaveStream CreateMediaStream(string filePath)<br />
{<br />
return new WaveFileReader(filePath);<br />
}<br />
<br />
private static void Dispose(IWavePlayer mediaPlayer, WaveStream mediaStream)<br />
{<br />
if (mediaPlayer != null)<br />
{<br />
mediaPlayer.Stop();<br />
mediaPlayer.Dispose();<br />
mediaPlayer = null;<br />
}<br />
if (mediaStream != null)<br />
{<br />
mediaStream.Dispose();<br />
mediaStream = null;<br />
}<br />
}<br />
}<br />
}</wavestream></wavestream></iwaveplayer></iwaveplayer>
You need to include NAudio.dll to your project in order to use it.
Hope this helps!!!