他下面的代码只使用xaml mediaelement重播2个视频。我注意到Windows 8.1系统上的内存没有得到清理,它最终会死掉。我看到内存在Win8.0上被收回。我做错了吗?
namespace PlaybackTest
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
StorageFile video1;
StorageFile video2;
StorageFile nextvid;
bool firstvid = true;
Windows.Storage.Streams.IRandomAccessStream _stream;
int iterctr = 0;
public MainPage()
{
this.InitializeComponent();
}
/// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
/// <param name="e">Event data that describes how this page was reached. The Parameter
/// property is typically used to configure the page.</param>
protected override async void OnNavigatedTo(NavigationEventArgs e)
{
meMainPlayer.Source = null;
var filepath = Windows.ApplicationModel.Package.Current.InstalledLocation;
video1 = await filepath.GetFileAsync("Data\\output1.mp4");
video2 = await filepath.GetFileAsync("Data\\output2.mp4");
nextvid = video1;
}
private void MediaElement_MediaEnded_1(object sender, RoutedEventArgs e)
{
Debug.WriteLine("media ended event");
if (firstvid == true)
{
Debug.WriteLine("setting up second vid");
firstvid = false;
nextvid = video2;
}
else
{
Debug.WriteLine("setting up first vid");
firstvid = true;
nextvid = video1;
}
meMainPlayer.Stop();
Debug.WriteLine("after stop ");
meMainPlayer.Position = new TimeSpan(0);
Debug.WriteLine("after position set to 0 ");
meMainPlayer.Source = null;
Debug.WriteLine("meplayer source = null ");
}
private void MediaElement_CurrentStateChanged_1(object sender, RoutedEventArgs e)
{
if (meMainPlayer.CurrentState == MediaElementState.Closed)
{
Debug.WriteLine("current media element state closed");
if (_stream != null)
{
_stream.Dispose();
_stream = null;
}
LoadNextVid();
}
}
private async void LoadNextVid()
{
Debug.WriteLine("");
await System.Threading.Tasks.Task.Delay(1000);
try
{
Debug.WriteLine("before strem opened");
_stream = await nextvid.OpenAsync(FileAccessMode.Read);
Debug.WriteLine("after stream opened");
await this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
meMainPlayer.SetSource(_stream, nextvid.ContentType);
txtIter.Text = "Iteration:" + iterctr;
});
iterctr++;
Debug.WriteLine("after dispatcher");
}
catch (Exception ex)
{
Debug.WriteLine("exceoption caught " + ex.Message);
}
}
private async void btnStart_Click_1(object sender, RoutedEventArgs e)
{
LoadNextVid();
}
}
}
答案 0 :(得分:0)
在将文件作为资源访问时,您没有利用IDisposable构造。 特别是你错过了&#34;使用&#34;在作为资源访问文件时构造。 因此,内存保持分配状态,因为此资源不会被丢弃。
_stream = await nextvid.OpenAsync(FileAccessMode.Read);
考虑以下示例:
public async void ContinueFileOpenPicker(FileOpenPickerContinuationEventArgs args)
{
if (args.Files.Count > 0)
{
var imageFile = args.Files[0] as StorageFile;
// Ensure the stream is disposed once the image is loaded
using (IRandomAccessStream fileStream = await imageFile.OpenAsync(Windows.Storage.FileAccessMode.Read))
{
// Set the image source to the selected bitmap
BitmapImage bitmapImage = new BitmapImage();
await bitmapImage.SetSourceAsync(fileStream);
ImageControl.Source = bitmapImage;
await _viewModel.Upload(imageFile);
}
}
}