C# MediaElement在一段时间后变黑
创建了一个程序,每10秒向mediaElement传递一个列表中的视频,这里的问题是,在一段时间后,比如1小时或2小时,mediaElement的屏幕变黑。我看到它就发生在我面前,并立即检查内存泄漏,但没有。这是MediaElement反复出现的问题吗?此外,我还做了一些程序,例如,如果我需要播放视频,则创建mediaElement,当不再需要时,将其删除 [编辑]C# MediaElement在一段时间后变黑,c#,wpf,mediaelement,C#,Wpf,Mediaelement,创建了一个程序,每10秒向mediaElement传递一个列表中的视频,这里的问题是,在一段时间后,比如1小时或2小时,mediaElement的屏幕变黑。我看到它就发生在我面前,并立即检查内存泄漏,但没有。这是MediaElement反复出现的问题吗?此外,我还做了一些程序,例如,如果我需要播放视频,则创建mediaElement,当不再需要时,将其删除 [编辑] namespace VideoPlayer { public partial class MainWindow
namespace VideoPlayer
{
public
partial class MainWindow : Window
{
#region Global var
private const string VIDEO_EXTENSION = "*.mp4";
private const string URL_NEXT_PLAYLIST = "http://tomiservice.net/mop/json/?json[device]=031232bc880aab7fc6687fd9377d8756&lang=pt&json[f]=get_next_playlist&json[typologie]=1";
private const string URL_PLAYLIST = "http://tomiservice.net/mop/json/?json[device]=031232bc880aab7fc6687fd9377d8756&lang=pt&json[f]=get_next_playlist&json[typologie]=1&json[date_interval]=";
private const string CONTENT_TYPE = "application/json";
private int randIndex = 0, seconds = 0;
private string vidName = null;
private static string DIRECTORY = System.Configuration.ConfigurationManager.AppSettings["Directory"];
private DirectoryInfo directoryInfo = new DirectoryInfo(DIRECTORY);
private DispatcherTimer videoPlaytimVerification = new System.Windows.Threading.DispatcherTimer();
private JsonParser json = new JsonParser();
private List<VideoEntity.Video> videoPlayList = new List<VideoEntity.Video>();
private List<VideoEntity.VideoTails> videoPlayListFill = new List<VideoEntity.VideoTails>();
MediaElement me = new MediaElement();
#endregion
public MainWindow()
{
InitializeComponent();
[EDIT]
me.MediaEnded += MediaEnded;
seconds = (int)DateTime.Now.TimeOfDay.TotalSeconds;
LoadFillVideo();
videoPlaytimVerification.Tick += new EventHandler(videoPlaytimVerification_Tick);
videoPlaytimVerification.Interval = new TimeSpan(0, 0, 1);
videoPlaytimVerification.Start();
}
#region METHODS
/*###############################################*/
/*###############################################*/
/*###############################################*/
private bool SearchVideo(string vidName)
{
foreach (var file in directoryInfo.GetFiles())
{
if (vidName == file.ToString())
{
return true;
}
}
return false;
}
/*###############################################*/
/*###############################################*/
/*###############################################*/
void createMediaElement(string videoSource)
{
me.LoadedBehavior = MediaState.Manual;
me.Source = new Uri(DIRECTORY + videoSource, UriKind.Absolute);
me.Play();
mediaPlayerGrid.Children.Add(me);
}
/*###############################################*/
/*###############################################*/
/*###############################################*/
private void LoadFillVideo()
{
Random rand = new Random(DateTime.Now.ToString().GetHashCode());
randIndex = rand.Next(0, videoPlayListFill.Count);
try
{
mediaPlayerGrid.Children.Remove(me);
createMediaElement(videoPlayListFill[randIndex].Filename);
}
catch { }
}
/*###############################################*/
/*###############################################*/
/*###############################################*/
[EDIT]
private void MediaEnded(Object sender, RoutedEventArgs e)
{
LoadFillVideo();
}
/*###############################################*/
/*###############################################*/
/*###############################################*/
#endregion
#region EVENTS
private void videoPlaytimVerification_Tick(object sender, EventArgs e)
{
foreach (VideoEntity.Video vid in videoPlayList)
{
if (vid.Playtime == seconds)
{
vidName = vid.Filename;
try
{
mediaPlayerGrid.Children.Remove(me);
//if the video exists, the switching happens here!!
createMediaElement(vidName);
}
catch { }
if (!SearchVideo(vidName))
{
//if the video does not exist, the fill video switching happens here!!
LoadFillVideo();
seconds++;
return;
}
}
}
if ((seconds >= videoPlayList[119].Playtime))
GetPlayList(1);
if (seconds >= 89990)
seconds = 3599;
seconds++;
}
#endregion
}
命名空间视频播放器
{
公众的
部分类主窗口:窗口
{
#区域全局var
private const string VIDEO_EXTENSION=“*.mp4”;
私有常量字符串URL\u下一个\u播放列表=”http://tomiservice.net/mop/json/?json[device]=031232bc880aab7fc6687fd9377d8756&lang=pt&json[f]=get_next_playlist&json[typologie]=1”;
私有常量字符串URL_播放列表=”http://tomiservice.net/mop/json/?json[device]=031232bc880aab7fc6687fd9377d8756&lang=pt&json[f]=get_next_playlist&json[typologie]=1&json[date_interval]=”;
private const string CONTENT_TYPE=“application/json”;
私密指数=0,秒=0;
私有字符串vidName=null;
私有静态字符串目录=System.Configuration.ConfigurationManager.AppSettings[“目录”];
private DirectoryInfo DirectoryInfo=新的DirectoryInfo(目录);
private Dispatchermer videoPlaytimVerification=new System.Windows.Threading.Dispatchermer();
私有JsonParser json=新JsonParser();
私有列表videoPlayList=新列表();
私有列表VideoPlayFill=新列表();
MediaElement me=新的MediaElement();
#端区
公共主窗口()
{
初始化组件();
[编辑]
me.MediaEnded+=MediaEnded;
秒=(int)DateTime.Now.TimeOfDay.TotalSeconds;
LoadFillVideo();
videoPlaytimVerification.Tick+=新事件处理程序(videoPlaytimVerification\u Tick);
videoPlaytimVerification.Interval=新的时间跨度(0,0,1);
videoPlaytimVerification.Start();
}
#区域方法
/*###############################################*/
/*###############################################*/
/*###############################################*/
私有布尔搜索视频(字符串vidName)
{
foreach(directoryInfo.GetFiles()中的var文件)
{
if(vidName==file.ToString())
{
返回true;
}
}
返回false;
}
/*###############################################*/
/*###############################################*/
/*###############################################*/
void createMediaElement(字符串视频源)
{
me.LoadedBehavior=MediaState.Manual;
me.Source=新Uri(目录+视频源,UriKind.Absolute);
我。玩();
mediaPlayerGrid.Children.Add(me);
}
/*###############################################*/
/*###############################################*/
/*###############################################*/
私有void LoadFillVideo()
{
Random rand=new Random(DateTime.Now.ToString().GetHashCode());
randIndex=rand.Next(0,VideoPlayFill.Count);
尝试
{
mediaPlayerGrid.Children.Remove(我);
createMediaElement(VideoPlayFill[randIndex].Filename);
}
捕获{}
}
/*###############################################*/
/*###############################################*/
/*###############################################*/
[编辑]
私有void mediated(对象发送方,路由目标)
{
LoadFillVideo();
}
/*###############################################*/
/*###############################################*/
/*###############################################*/
#端区
#地区活动
私有void videoPlaytimVerification_Tick(对象发送方,事件参数e)
{
foreach(视频播放列表中的VideoEntity.Video vid)
{
如果(视频播放时间==秒)
{
vidName=vid.Filename;
尝试
{
mediaPlayerGrid.Children.Remove(我);
//如果视频存在,则在此处进行切换!!
createMediaElement(vidName);
}
捕获{}
如果(!SearchVideo(vidName))
{
//如果视频不存在,填充视频切换将在此处发生!!
LoadFillVideo();
秒++;
返回;
}
}
}
如果((秒>=视频播放列表[119]。播放时间))
获取播放列表(1);
如果(秒>=89990)
秒=3599;
秒++;
}
#端区
}
}
[编辑][编辑][编辑]
好的,我一直在做一些研究,我的代码现在看起来像这样
namespace VideoPlayer
{
public partial class MainWindow : Window
{
#region Global var
private const string VIDEO_EXTENSION = "*.mp4";
private const string URL_NEXT_PLAYLIST = "http://tomiservice.net/mop/json/?json[device]=031232bc880aab7fc6687fd9377d8756&lang=pt&json[f]=get_next_playlist&json[typologie]=1";
private const string URL_PLAYLIST = "http://tomiservice.net/mop/json/?json[device]=031232bc880aab7fc6687fd9377d8756&lang=pt&json[f]=get_next_playlist&json[typologie]=1&json[date_interval]=";
private const string CONTENT_TYPE = "application/json";
private int randIndex = 0, seconds = 0;
private static string DIRECTORY = System.Configuration.ConfigurationManager.AppSettings["Directory"];
private enum get { CurrentPlaylist = 0, NextPlaylist = 1 };
private DirectoryInfo directoryInfo = new DirectoryInfo(DIRECTORY);
private DispatcherTimer videoPlaytimVerification = new System.Windows.Threading.DispatcherTimer();
private JsonParser json = new JsonParser();
private List<VideoEntity.Video> videoPlayList = new List<VideoEntity.Video>();
private List<VideoEntity.VideoTails> videoPlayListFill = new List<VideoEntity.VideoTails>();
MediaElement me;
#endregion
public MainWindow()
{
InitializeComponent();
me = null;
seconds = (int)DateTime.Now.TimeOfDay.TotalSeconds;
GetPlayList((int)get.CurrentPlaylist);
LoadFillVideo();
videoPlaytimVerification.Tick += new EventHandler(videoPlaytimVerification_Tick);
videoPlaytimVerification.Interval = new TimeSpan(0, 0, 1);
videoPlaytimVerification.Start();
}
#region METHODS
/*###############################################*/
/*###############################################*/
/*###############################################*/
private bool SearchVideo(string vidName)
{
foreach (var file in directoryInfo.GetFiles())
{
if (vidName == file.ToString())
{
return true;
}
}
return false;
}
/*###############################################*/
/*###############################################*/
/*###############################################*/
void createMediaElement(string videoSource)
{
if (me != null )
{
me.Stop();
mediaPlayerGrid.Children.Remove(me);
}
me = new MediaElement();
mediaPlayerGrid.Children.Add(me);
me.LoadedBehavior = MediaState.Manual;
me.Source = new Uri(DIRECTORY + videoSource, UriKind.Absolute);
me.Play();
}
/*###############################################*/
/*###############################################*/
/*###############################################*/
private void GetPlayList(int urlCode)
{
WebRequest request;
switch (urlCode)
{
case (int)get.CurrentPlaylist:
request = WebRequest.Create(URL_PLAYLIST + seconds.ToString());
RequestPlayList(request);
break;
case (int)get.NextPlaylist:
request = WebRequest.Create(URL_NEXT_PLAYLIST);
RequestPlayList(request);
break;
}
}
/*###############################################*/
/*###############################################*/
/*###############################################*/
private void RequestPlayList(WebRequest request)
{
videoPlayList.Clear();
request.ContentType = CONTENT_TYPE;
string jsonSample;
try
{
var response = (HttpWebResponse)request.GetResponse();
using (var sr = new StreamReader(response.GetResponseStream()))
{
jsonSample = sr.ReadToEnd();
}
json.VideoSearch(jsonSample, ref videoPlayList, ref videoPlayListFill);
}
catch { }
}
/*###############################################*/
/*###############################################*/
/*###############################################*/
private void LoadFillVideo()
{
Random rand = new Random(DateTime.Now.ToString().GetHashCode());
randIndex = rand.Next(0, videoPlayListFill.Count);
createMediaElement(videoPlayListFill[randIndex].Filename);
}
/*###############################################*/
/*###############################################*/
/*###############################################*/
private void MediaEnded(Object sender, RoutedEventArgs e)
{
LoadFillVideo();
}
/*###############################################*/
/*###############################################*/
/*###############################################*/
#endregion
#region EVENTS
private void videoPlaytimVerification_Tick(object sender, EventArgs e)
{
seconds = (int)DateTime.Now.TimeOfDay.TotalSeconds;
if (seconds <= 3590)
seconds = 86400 + seconds;
foreach (VideoEntity.Video vid in videoPlayList)
{
if (vid.Playtime == seconds)
{
if (!SearchVideo(vid.Filename))
{
LoadFillVideo();
seconds++;
}
else
createMediaElement(vid.Filename);
}
}
if ((seconds >= videoPlayList[119].Playtime))
GetPlayList((int)get.NextPlaylist);
}
#endregion
}
}
命名空间视频播放器
{
公共部分类主窗口:窗口
{
#区域全局var
private const string VIDEO_EXTENSION=“*.mp4”;
私有常量字符串URL\u下一个\u播放列表=”http://tomiservice.net/mop/json/?json[device]=031232bc880aab7fc6687fd9377d8756&lang=pt&json[f]=get_next_playlist&json[typologie]=1”;
私有常量字符串URL_播放列表=”http://tomiservice.net/mop/json/?json[device]=031232bc880aab7fc6687fd9377d8756&lang=pt&json[f]=get_next_playlist&json[typologie]=1&json[date_interval]=”;
private const string CONTENT_TYPE=“application/json”;
私密指数=0,秒=0;
私有静态字符串目录=System.Configuration.ConfigurationManager.AppSettings[“目录”];
私有枚举get{CurrentPlaylist=0,NextPlaylist=1};
private DirectoryInfo DirectoryInfo=新的DirectoryInfo(目录);
private Dispatchermer videoPlaytimVerification=new System.Windows.Threading.Dispatchermer();
公共关系
private void VideoEnd(MediaElement me, string vidName)
{
me.MediaEnded += (a, b) =>
{
Random rand = new Random(DateTime.Now.ToString().GetHashCode());
randIndex = rand.Next(0, videoPlayListFill.Count);
mediaPlayerGrid.Children.Remove(me);
createMediaElement(videoPlayListFill[randIndex].Filename);
};
}