Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
bencambridgemaths opened this issue May 23, 2024 · 1 comment
Labels
area-External Not owned by the WinUI team, not actionable in this repository.

Comments

@bencambridgemaths
Copy link

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.

@microsoft-github-policy-service microsoft-github-policy-service bot added the needs-triage Issue needs to be triaged by the area owners label May 23, 2024
@codendone codendone transferred this issue from microsoft/WindowsAppSDK May 23, 2024
@codendone codendone added area-MediaPlayerElement team-Controls Issue for the Controls team and removed needs-triage Issue needs to be triaged by the area owners labels May 23, 2024
Copy link

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.

@ranjeshj ranjeshj added area-External Not owned by the WinUI team, not actionable in this repository. and removed area-MediaPlayerElement team-Controls Issue for the Controls team labels Jun 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-External Not owned by the WinUI team, not actionable in this repository.
Projects
None yet
Development

No branches or pull requests

3 participants