英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论