2012-12-16 2 views
0

나는 MediaElement 클래스를 사용하여 버튼 클릭시 간단한 사운드를 재생합니다. 그걸 두 번 두드리는게 아니라면 그건 잘된거야. 두 번째 탭에서 이전 사운드의 재생을 중지하고 사운드를 다시 시작하겠습니다. 하지만 Stop 메서드가 작동하지 않습니다.C# .NET에서 mediaElement로 WinRT (다시) 사운드 재생

public static MediaElement SoundDelete; 
    public static void PlaySoundCash() 
    { 
     if (SessionHelper.getConfig("SoundDelete") == "true") 
     { 
      System.Diagnostics.Debug.WriteLine(SoundDelete.Position.ToString()); 
      SoundDelete.Stop(); //dosn't work 
      SoundDelete.Position = new System.TimeSpan(0,0,0,0,0); //dosn't work 
      System.Diagnostics.Debug.WriteLine(SoundDelete.Position.ToString()); 
      SoundDelete.Play(); 
     } 
    } 

콘솔에서 시간 범위는 0이 아닙니다. 처음부터 사운드를 재생하려면 어떻게해야합니까?

답변

1

나는 똑같은 문제가있었습니다. Visual Tree에 MediaElement를 추가하면이 추가 기능을 사용할 수 있습니다.

내 기본 Grid의 하위에 MediaElements를 추가하면 이러한 문제가 해결되었습니다. 예를 들어

:

C#을

public static MediaElement SoundDelete; 

private void MediaInit() 
{ 
    // ... 
    // load your sound file into SoundDelete 
    // ... 
    BaseGrid.Children.Add(SoundDelete); 
} 
public static void PlaySoundCash() 
{ 
    System.Diagnostics.Debug.WriteLine(SoundDelete.Position.ToString()); 
    SoundDelete.Stop(); 
    SoundDelete.Position = new System.TimeSpan.Zero; 
    System.Diagnostics.Debug.WriteLine(SoundDelete.Position.ToString()); 
    SoundDelete.Play(); 
} 

또는 XAML에서

:

이봐, 내 XAML 내부를 가지고 있지만 시각적 트리에 추가하는 방법을 모른다
<Grid Name="BaseGrid"> 
    <MediaElement Name="SoundDelete" Source="path_to_your_sound_file.mp3" /> 
</Grid> 
+0

. 어떤 예를 들려 줄 수 있습니까? –

+0

요청에 따라 수정 된 게시물 – MickJ