2

他下面的代码只是使用 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();
    }
}

}

4

1 回答 1

0

在将文件作为资源访问时,您没有利用 IDisposable 构造。具体来说,在将文件作为资源访问时,您缺少“使用”构造。结果,内存保持分配状态,因为该资源没有被释放。

_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);
        }               
    }
}
于 2014-09-15T00:33:50.740 回答