Click here to Skip to main content
16,020,714 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I need to play multiple instances of a same media file on a system and delete the file whenever last instance is deleted/closed.
There is no UI involved in it and media is played/stopped on some triggers.
I have been able to play the file using MediaElement and I am able to listen to same song in parallel and stop them individually.

The issue occured while deletion of the media file.
MediaElement doesn't release the filestream on MediaElement.Close() and the file is not deleted. It gets deleted only after application is closed.
I feel it to be a timing issue between MediaElement.Close() and File.Delete() as it happens intermittently.
I wanted to delete the file in order to create memory by deleting unused file(s).
It can be reporduced by creating atleast 2 MediaElement whose Source(Uri) refer to same media file.
After playing both the MediaElement, we close the media and delete the file(s). Sometimes it deletes the file, sometime doesn't.

Appreciate any thoughts on this issue of file being locked by the MediaElement and unable to delete instantly.

Thanks in advance!
Posted
Comments
HS0007 25-Nov-14 17:41pm    
Hi Praveen,
Did you ever get to solve this problem? I am curious to know how you resolved it as i am facing exactly the same issue today with mediaElement. It really sucks!
Please Help!
- Yuga

1 solution

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 />
			// Play all the players<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 />
		/// <summary><br />
		/// Creates a player to play media on this device<br />
		/// </summary><br />
		/// <returns>player to play the media</returns><br />
		private static IWavePlayer CreatePlayer()<br />
		{<br />
			return new WaveOutEvent();<br />
		}<br />
		private static object lockObj = new object();<br />
<br />
		/// <summary><br />
		/// Called when media has stopped playing<br />
		/// </summary><br />
		/// <param name="sender">sender</param><br />
		/// <param name="e">Stopped Event arguments</param><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 />
				// Delete the media File<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!!!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900