microsoft / microsoft-ui-xaml

Windows UI Library: the latest Windows 10 native controls and Fluent styles for your applications
MIT License
6.18k stars 667 forks source link

MediaSource.ExternalTimedTextSources.Clear causes unhandled exception if loaded TimeTextSource was not valid #9663

Open bencambridgemaths opened 1 month ago

bencambridgemaths commented 1 month ago

Describe the bug

MediaSource.ExternalTimedTextSources.Clear causes unhandled exception if loaded TimeTextSource was not valid. Remove also causes exception. The exception is 'Invalid index'. It is caught by the app's unhandled exception handler but can't be caught by a wrapping try-catch block. Note there is no way to tell the TimeTextSource was invalid without loading it (The 'Resolved' handler is called after load, but I have omitted it from the code below as not needed).

Steps to reproduce the bug

Here's s minimal app. First, select a video using the 'Load video' button. Then, select an invalid subtitle file using the 'Select subtitles' button (can just select the video again), then, load another subtitles file (valid or invalid). App crashes when trying to clear the ExternalTimedTextSources.

using System;
using System.Threading.Tasks;
using Windows.Media.Core;
using Windows.Media.Playback;
using Windows.Storage.Pickers;
using Windows.Storage;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace TestApp
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }

        IMediaPlaybackSource mediaSource = null;

        private async void testButton_Click(object sender, RoutedEventArgs e) {
            var filePicker = new FileOpenPicker();
            filePicker.CommitButtonText = "Load video";
            filePicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
            filePicker.ViewMode = PickerViewMode.List;
            filePicker.FileTypeFilter.Add("*");
            StorageFile file = null;
            try {
                file = await filePicker.PickSingleFileAsync();
            } catch (Exception) { }
            if (file == null) {
                return;
            }
            MediaSource newMediaSource = null;
            try {
                newMediaSource = MediaSource.CreateFromStorageFile(file);
            }catch (Exception) { }
            if(newMediaSource == null) {
                return;
            }
            await newMediaSource.OpenAsync();
            mediaSource = new MediaPlaybackItem(newMediaSource);
        }

        private async void testButton1_Click(object sender, RoutedEventArgs e) {
            await LoadSubtitlesFromFile();
        }

        FileOpenPicker loadSubtitlesFilePicker = null;

        private async Task LoadSubtitlesFromFile() {
            if (!(mediaSource is MediaPlaybackItem mediaPlaybackItem)) {
                return;
            }
            var mediaSourceSource = mediaPlaybackItem.Source;
            if (mediaSourceSource == null) {
                return;
            }
            if (loadSubtitlesFilePicker == null) {
                loadSubtitlesFilePicker = new FileOpenPicker();
                loadSubtitlesFilePicker.CommitButtonText = "Load subtitles";
                loadSubtitlesFilePicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
                loadSubtitlesFilePicker.ViewMode = PickerViewMode.List;
                loadSubtitlesFilePicker.FileTypeFilter.Add("*");
            }
            StorageFile file = null;
            try {
                file = await loadSubtitlesFilePicker.PickSingleFileAsync();
            } catch (Exception) { }
            if (file == null) {
                return;
            }
            try {
                using (var stream = await file.OpenReadAsync()) {
                    var timedTextSource = TimedTextSource.CreateFromStream(stream);
                    if (timedTextSource == null) {
                        return;
                    }
                    mediaSourceSource.ExternalTimedTextSources.Clear(); //'Invalid index' exception here (unhandled)
                    mediaSourceSource.ExternalTimedTextSources.Add(timedTextSource);
                }
            } catch (Exception) {
                return;
            }
        }

    }
}
<Page
    x:Class="TestApp.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:TestApp"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <StackPanel>
        <Button x:Name="testButton" Click="testButton_Click" Content="Load video"></Button>
        <Button x:Name="testButton1" Click="testButton1_Click" Content="Load subtitles"></Button>
    </StackPanel>
</Page>

Expected behavior

No crash.

Screenshots

No response

NuGet package version

None

Packaging type

No response

Windows version

Windows 11 version 22H2 (22621, 2022 Update)

IDE

Visual Studio 2022

Additional context

Doesn't need WinAppSDK, bug is in Windows.Media.

github-actions[bot] commented 1 month ago

Hi I'm an AI powered bot that finds similar issues based off the issue title.

Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one. Thank you!

Closed similar issues:

Note: You can give me feedback by thumbs upping or thumbs downing this comment.