0

我有一个在应用程序级别运行背景音乐的应用程序,因此当用户浏览页面时音乐不会停止。但是,我也使用 VideoBrush。正如我发现的那样,我不能让两者同时运行,因为 VideoBrush 在设置其源时会崩溃。

我发现,如果在用户尝试使用 VideoBrush 时将 MediaElement 的源设置为 null,则一切正常。当然音乐停止了,这让我很懊恼,但没有发生错误。

但是,当用户从 VideoBrush 上轻按时,我试图让音乐重新开始(开始很好)但无济于事。简而言之,我无法让音乐重新开始。

这是我的代码:

应用程序.xaml

    <Application.Resources>

        <MediaElement x:Key="GlobalMedia" Source="minutelongsong.mp3"
         MediaEnded="MediaElement_MediaEnded" Visibility="Collapsed" />

    </Application.Resources>

应用程序.xaml.cs

    public static MediaElement GlobalMediaElement
    {
        get { return Current.Resources["GlobalMedia"] as MediaElement; }
    }

    private void Application_Launching(object sender, LaunchingEventArgs e)
    {
        var AppMediaElement = App.GlobalMediaElement;
        AppMediaElement.Position = TimeSpan.Zero;
        AppMediaElement.Play();
    }

    private void MediaElement_MediaEnded(object sender, RoutedEventArgs e)
    {
        var AppMediaElement = App.GlobalMediaElement;
        AppMediaElement.Position = TimeSpan.Zero;
        AppMediaElement.Play();
    }

现在是使用 VideoBrush 的页面。

主页.xaml

    <Canvas x:Name="viewfinderCanvas" Width="480" Height="800" HorizontalAlignment="Center" VerticalAlignment="Center" Visibility="Collapsed">
        <Canvas.Background>
            <VideoBrush x:Name="videoBrush" Stretch="Fill">
                <VideoBrush.RelativeTransform>
                    <CompositeTransform x:Name="previewTransform"
                        CenterX=".5"
                        CenterY=".5" />
                </VideoBrush.RelativeTransform>
            </VideoBrush>
        </Canvas.Background>
    </Canvas>

MainPage.xaml.cs

    private void Button_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {

        var AppMediaElement = App.GlobalMediaElement;
        AppMediaElement.Pause();
        AppMediaElement.Stop();
        AppMediaElement.Source = null; //set it to null to allow the cam to be set.


        if ((PhotoCamera.IsCameraTypeSupported(CameraType.Primary)))
        {
           viewfinderCanvas.Visibility = Visibility.Visible;
           cam = new PhotoCamera(CameraType.Primary);
           if (Orientation == PageOrientation.PortraitUp || Orientation == PageOrientation.PortraitDown || Orientation == PageOrientation.Portrait)
           {

               videoBrush.RelativeTransform = new CompositeTransform() { CenterX = 0.5, CenterY = 0.5, Rotation = 90 };
           }

           videoBrush.SetSource(cam);
        }

当用户通过点击屏幕按钮退出相机 VideoBrush 时,将触发此代码。它处理凸轮,并在用户允许音乐的情况下尝试再次播放音乐。但是,即使使用此代码,音乐也不会播放。

    private void zoomout_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        if (cam != null)
        {
            cam.Dispose();
        }

        viewfinderCanvas.Visibility = Visibility.Collapsed;

        if (allowingamemusic == true)
        {
            var AppMediaElement = App.Current.Resources["GlobalMedia"] as MediaElement;
            AppMediaElement.Source = new Uri("minutelongsong.mp3", UriKind.RelativeOrAbsolute);
            AppMediaElement.Position = TimeSpan.Zero;
            AppMediaElement.Play(); //despite this here, it will not play. No error thrown.
        }
    }
4

2 回答 2

1

我在手机应用程序的同一页面中来回切换捕获图像和音频。

您非常接近,但您还必须将 videoBrush 源设置为其他内容,否则它不会释放资源。

因此,当您想使用 MediaElement 时,首先要像这样处理相机:

cam.Dispose();
viewfinderBrush.SetSource(new MediaElement()); //so it will let go of cam
cam = null;
//now set source for MediaElemet and do whatever

当您再次使用相机时:

mediaElement.Stop();
mediaElement.Source = null; //or else setting the source for the video brush will fail
//now set source for videoBrush and do whatever

老问题,但希望这会对某人有所帮助......我花了几次尝试才弄清楚。

于 2013-12-19T18:33:58.280 回答
0

在昨晚写完这篇文章后,今天早上终于测试了它,我看到可能发生了什么,但仍然遇到音乐停止后无法重播的问题。

MediaElement.MediaFailed 一直被调用。我发现它正在调用:AG_E_NETWORK_ERROR。当 Zune 正在运行并且我的设备通过 USB 连接到同一台计算机时会引发此错误。

有人建议我将我的应用程序部署到我的手机,关闭 zune,然后断开 USB。我试过这个,但我尝试重播后仍然无法播放音乐。这也发生在模拟器上。

仍然抛出相同的 AG_E_NETWORK_ERROR。

从那以后,我已经放弃了这个,因为我花了几个小时没有找到如何做到这一点。所以,我已经使用了 MediaPlayer 类,并将改用它。

------ 如果有人解决了这个问题,我会给你一个正确答案的复选标记。

于 2013-04-09T22:08:43.823 回答