我正在使用MediaElement长时间显示循环中的视频剪辑。经过一段时间(Win 7/4 GB RAM的小时),程序崩溃,但“内存不足”类型除外。我监视使用Process Explorer-Sysinternals时使用的内存,并使用System.Diagnostics.Process方法记录它。两种方式都表明使用内存逐渐增加。如何释放MediaElement使用的内存
下面是代码:
XAML:
<Grid Name="GridTest">
<MediaElement x:Name="MediaPlayer"
LoadedBehavior="Manual"
MediaEnded="VideoControl_MediaEnded"
MediaOpened="MediaPlayer_MediaOpened"
Source="{Binding Mode=OneWay,
Path=MySource}" />
</Grid>
的.cs:
public partial class MainWindow : Window
{
public MainViewModel model = new MainViewModel();
public MainWindow()
{
InitializeComponent();
this.GridTest.DataContext = model;
// fill in model.MediaFilesUris:
...
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
// choose the next media file
...
MediaPlayer.Play();
}
private void VideoControl_MediaEnded(object sender, RoutedEventArgs e)
{
// choose the next media file
...
model.OnPropertyChanged("MySource");
MediaPlayer.Play();
}
}
public class MainViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
public Uri[] MediaFilesUris = null;
public int crn = 0;
public Uri MySource { get { if (MediaFilesUris != null && MediaFilesUris.Count()>0) return MediaFilesUris[crn]; else return null; } }
}
我还测试的情况下的MediaElement对象是动态创建的,与所有破坏(一起取消订阅活动等),然后重新创建。记忆力再次被消耗。
任何建议,将不胜感激!
你是如何 “毁” 你的MediaElement?这是一个UI元素,WPF为UI元素做了一件糟糕的工作。 – Xcalibur37
试图在游戏结束时使Source = null?你明确地试试。 – EngineerSpock
下面是我如何“销毁”MediaElement obejct(以编程方式创建,而不是在XAML中):MediaPlayer.Stop(); MediaPlayer.MediaEnded - = MediaPlayer_MediaEnded; MediaPlayer.MediaOpened - = MediaPlayer_MediaOpened; MediaPlayer.Close(); GridTest.Children.Remove(MediaPlayer); MediaPlayer = null; GC.Collect(); 如果使用绑定Source属性创建,也在Close()之前创建:BindingOperations.ClearBinding(MediaPlayer,MediaElement.SourceProperty); – MDimitrov