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.