When adding a new source at the end of the previous song finished, the plugin media element will report an error

dghgd 0 Reputation points
2025-01-15T07:41:23.6566667+00:00

I want to realize that auto play next song when the previous song was finished playing,so I add a function for the event of mediaended, and add a new source by the mediaelemen object's properties source(mediaelement.source) ,however it report a bug, I can't find reason and resolve method

you can find all code by this link:

https://github.com/fsdfsfd/SimpleAudioPlayer

I'll give you a picture, and it'll show you exactly where the bug is:

The white highlighted areas in the picture mean:

When the onmediaended function calls loadsongomediaelementsource function meanwhile The program runs to mediaelemnt. source=, Bugs are starting to appear, but the value on the right side of the equal sign runs correctly
屏幕截图 2025-01-15 124212

Windows
Windows
A family of Microsoft operating systems that run across personal computers, tablets, laptops, phones, internet of things devices, self-contained mixed reality headsets, large collaboration screens, and other devices.
5,729 questions
.NET
.NET
Microsoft Technologies based on the .NET software framework.
4,059 questions
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,842 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 78,761 Reputation points Microsoft Vendor
    2025-01-16T04:00:49.9566667+00:00

    Hello,

    When you add the song, your SongName will be null, you will get the null exceptions, I get the song name from Sony_path and set it like following code.

    public void AddSong(string Song_path)
    {
        Song song = new Song();
        song.InitInstanceByTagLib(Song_path);
     
        int index = Song_path.LastIndexOf('\\');
        if (index >= 0)
        {
            song.SongName = Song_path.Substring(index + 1);       
        }
        SongList.Add(song.SongName,song);
        PlayList.Add(song.SongName);
    }
    

    Then you have added an Action and wrap it by try/catch when you change the song, your application could run normally and this exception is disappeared.

    try
    {
        // 使用闭包捕获参数
        Action updateAction = () => UpdateMediaElementSource(path, songname);
     
        // 使用 Dispatcher 延迟设置 Source 属性
        Dispatcher.Dispatch(updateAction);
    }
    catch (COMException comEx)
    {
        // 处理 COMException
        Console.WriteLine($"COMException caught: {comEx.Message}");
    }
    catch (Exception ex)
    {
        // 处理其他异常
        Console.WriteLine($"Exception caught: {ex.Message}");
    }
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.