2013-04-09 86 views
0

我有一个应用程序在应用程序级别运行背景音乐,以便音乐不停止时,用户浏览页面。但是,我也使用VideoBrush。正如我发现的那样,在设置源代码时,我无法在VideoBrush崩溃的同时运行它们。全局MediaElement将不会重新启动一次停止,wp7

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

但是,当用户从VideoBrush中跳出时,我试图使音乐开始备份(开始很好),但是无济于事。简而言之,我无法让音乐重新启动。

这里是我的代码:

的App.xaml

<Application.Resources> 

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

    </Application.Resources> 

App.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的页面。

MainPage.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. 
     } 
    } 

回答

0

写这最后一夜,终于今天上午的测试之后,我看看有什么可能发生,但我仍然有一次它停止不重放音乐的问题。

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

有人建议我将我的应用程序部署到我的手机,关闭zune,并断开USB连接。我尝试过这种方式,并且一旦我尝试重播音乐,音乐仍然无法播放。这也发生在模拟器上。

同样的AG_E_NETWORK_ERROR仍在抛出。

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

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

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 

老问题,但希望这会帮助别人......我花了几次尝试才弄明白。