他下面的代码只是使用 xaml 媒体元素一遍一遍地播放 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();
}
}
}