UWP MediaPlayer仍在重定向的应用实例中播放音乐。

huangapple go评论58阅读模式
英文:

UWP MediaPlayer still playing music in redirected app instance

问题

我想要做的是,在打开媒体文件(音乐或视频)时,它会在一个应用程序窗口中打开,而不会创建另一个窗口来播放另一个文件(例如,不会同时播放两个曲目或两个视频)。我在Main方法中进行了重定向,它几乎正确工作,当打开另一个媒体文件时,应用程序会在已经存在的窗口中打开它,就好像覆盖了应用程序的先前实例。

但主要问题是,来自先前实例的播放器中的音乐继续播放并与新实例中的声音重叠。

带有重定向的Main方法:

static void Main()
{
    IActivatedEventArgs activatedArgs = AppInstance.GetActivatedEventArgs();

    if (activatedArgs is FileActivatedEventArgs fileArgs)
    {
        IStorageItem file = fileArgs.Files.FirstOrDefault();
        if (file != null)
        {
            AppInstance instance;

            if (Constants.SupportedImagesFormates.Any(x => file.Name.Contains(x)))
            {
                instance = AppInstance.FindOrRegisterInstanceForKey(file.Name);
            }
            else
            {
                instance = AppInstance.FindOrRegisterInstanceForKey("playbackMedia");
            }

            if (instance.IsCurrentInstance)
            {
                Application.Start((p) => new App());
            }
            else
            {
                instance.RedirectActivationTo();
            }
        }
    }
    else
    {
        Application.Start((p) => new App());
    }
}

有没有关于如何修复它的想法?我希望我解释得清楚...

英文:

I wanted to make it so that when opening a media file (music or video), it opens in one application window, and not create another window with another file being played (for example, so that two tracks do not play at the same time or two videos do not play at the same time). I made a redirect in the Main method, and it works almost correctly, when opening another media file, the application opens it in an already existing window, as if overwriting the previous instance of the application.
But the main problem is that the music in the player from the previous instance continues to play and overlaps with the sound from the new instance.

Main method with redirection:
`static void Main()
{
IActivatedEventArgs activatedArgs = AppInstance.GetActivatedEventArgs();

    if (activatedArgs is FileActivatedEventArgs fileArgs)
    {
        IStorageItem file = fileArgs.Files.FirstOrDefault();
        if (file != null)
        {
            AppInstance instance;

            if (Constants.SupportedImagesFormates.Any(x => file.Name.Contains(x)))
            {
                instance = AppInstance.FindOrRegisterInstanceForKey(file.Name);
            }
            else
            {
                instance = AppInstance.FindOrRegisterInstanceForKey("playbackMedia");
            }

            if (instance.IsCurrentInstance)
            {
                Application.Start((p) => new App());
            }
            else
            {
                instance.RedirectActivationTo();
            }
        }
    }
    else
    {
        Application.Start((p) => new App());
    }
}`

Any ideas how to fix it? I hope I explained it clearly...

答案1

得分: 0

打开新实例时,您需要手动暂停MediaPlayer。

建议使用MediaPlayer.Pause来在打开新实例时暂停您的音频或视频文件。

英文:

When opening a new instance, you need to pause the MediaPlayer manually.

It is recommended to use MediaPlayer.Pause to pause your audio or video files when opening new instance.

huangapple
  • 本文由 发表于 2023年6月11日 23:49:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76451262.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定