2012-01-17 39 views
0

6.5.1のガイドラインのため、私のアプリが認証に失敗しました:「アプリケーションはプロンプトなしでユーザーの音楽を一時停止する状態になります。MediaPlayer.GameHasControlが正しく動作していません

私はこの問題の解決策を見つけ出し、MediaPlayer.GameHasControlを使用して、アプリケーションを起動する前に音楽が再生されているかどうかを確認する必要があることを発見しました。

私が実装したとき、うまくいきませんでした。それは音楽を一時停止することを促しましたが、反応する機会がある前に一時停止しました。 OnNavigatedTo()、Loaded()、コンストラクタにMediaPlayer.GameHasControlを入れようとしましたが、しばらくは動作しますが、アプリが数秒後にクラッシュするので、それらはオプションではありません。それから私は3つのMediaElementsの処理をMediaOpenedイベントに入れようとしましたが、運はありませんでした。今私はちょうど立ち往生しています。もし誰かがこれについていくらか光を当てることができたら、私は非常に感謝します。私は私が家に帰った後のプロンプトを行うために使用するコードでこの答えを更新します

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Net; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Animation; 
using System.Windows.Shapes; 
using Microsoft.Phone.Controls; 
using System.Xml.Linq; 
using System.Windows.Navigation; 
using System.Windows.Media.Imaging; 
using Microsoft.Xna.Framework.Media; 
using Microsoft.Xna.Framework; 

namespace RhythmCoach 
{ 
    public partial class BeginnerExercisePage : PhoneApplicationPage 
    { 
    public BeginnerExercisePage() 
    { 
     InitializeComponent(); 
    } 

    protected override void OnNavigatedTo(NavigationEventArgs e) 
    { 
     XDocument beginnerExerciseData = XDocument.Load("XML/BeginnerXML.xml"); 

     string name = string.Empty; 

     if (NavigationContext.QueryString.TryGetValue("name", out name)) 
     { 

      var exercise = (from Exercised in beginnerExerciseData.Descendants("Exercised") 
          where Exercised.Attribute("name").Value == name 
          select new Exercise 
          { 
           ExImage = (string)Exercised.Element("image").Value, 
           ExGuitarAudio = (string)Exercised.Element("GuitarAudio").Value, 
           ExPianoAudio = (string)Exercised.Element("PianoAudio").Value, 
           ExSnareAudio = (string)Exercised.Element("SnareAudio").Value, 
           ExTitle = (string)Exercised.Element("title").Value 
          }).Single(); 

      im.Source = new BitmapImage(new Uri(exercise.ExImage, UriKind.Relative)); 
      pageTitle.Text = exercise.ExTitle; 

      guitarAudioPlayer.Source = new Uri(exercise.ExGuitarAudio,UriKind.Relative); 
      pianoAudioPlayer.Source = new Uri(exercise.ExPianoAudio, UriKind.Relative); 
      snareAudioPlayer.Source = new Uri(exercise.ExSnareAudio, UriKind.Relative); 
     } 

     rbBegSnare.IsChecked = true; 
     base.OnNavigatedTo(e); 
    } 

    private void button1_Click(object sender, RoutedEventArgs e) 
    { 
     if (rbBegGuitar.IsChecked == true) 
     { 
      guitarAudioPlayer.Play(); 

     } 
     else if (rbBegPiano.IsChecked == true) 
     { 
      pianoAudioPlayer.Play(); 

     } 
     else 
      snareAudioPlayer.Play(); 

    } 

    private void button2_Click(object sender, RoutedEventArgs e) 
    { 
     if (rbBegGuitar.IsChecked == true) 
     { 
      guitarAudioPlayer.Stop(); 
      pianoAudioPlayer.Stop(); 
      snareAudioPlayer.Stop(); 
     } 
     else if (rbBegPiano.IsChecked == true) 
     { 
      pianoAudioPlayer.Stop(); 
      guitarAudioPlayer.Stop(); 
      snareAudioPlayer.Stop(); 
     } 
     else 
      snareAudioPlayer.Stop(); 
      guitarAudioPlayer.Stop(); 
      pianoAudioPlayer.Stop(); 
    } 

    private void button3_Click(object sender, RoutedEventArgs e) 
    { 
     if (rbBegGuitar.IsChecked == true) 
     { 
      guitarAudioPlayer.Pause(); 
      pianoAudioPlayer.Pause(); 
      snareAudioPlayer.Pause(); 
     } 
     else if (rbBegPiano.IsChecked == true) 
     { 
      pianoAudioPlayer.Pause(); 
      guitarAudioPlayer.Pause(); 
      snareAudioPlayer.Pause(); 
     } 
     else 
      snareAudioPlayer.Pause(); 
      guitarAudioPlayer.Pause(); 
      pianoAudioPlayer.Pause(); 
    } 

    private void rbBegGuitar_Checked(object sender, RoutedEventArgs e) 
    { 
     pianoAudioPlayer.Stop(); 
     snareAudioPlayer.Stop(); 
    } 

    private void rbBegPiano_Checked(object sender, RoutedEventArgs e) 
    { 
     guitarAudioPlayer.Stop(); 
     snareAudioPlayer.Stop(); 
    } 

    private void rbBegSnare_Checked(object sender, RoutedEventArgs e) 
    { 
     guitarAudioPlayer.Stop(); 
     pianoAudioPlayer.Stop(); 
    } 

    private void guitarAudioPlayer_MediaOpened(object sender, RoutedEventArgs e) 
    { 
     if (!MediaPlayer.GameHasControl) 
     { 
      MessageBoxResult Choice; 
      Choice = MessageBox.Show("Media is currently playing, do you want to stop it?", "Stop Player", MessageBoxButton.OKCancel); 
      if (Choice != MessageBoxResult.OK) 
      { 
       if (NavigationService.CanGoBack) 
        NavigationService.GoBack(); 
       else 
        NavigationService.Navigate(new Uri("/Menu.xaml", UriKind.Relative)); 
       return; 
      } 
     } 
    } 

    private void pianoAudioPlayer_MediaOpened(object sender, RoutedEventArgs e) 
    { 
     if (!MediaPlayer.GameHasControl) 
     { 
      MessageBoxResult Choice; 
      Choice = MessageBox.Show("Media is currently playing, do you want to stop it?", "Stop Player", MessageBoxButton.OKCancel); 
      if (Choice != MessageBoxResult.OK) 
      { 
       if (NavigationService.CanGoBack) 
        NavigationService.GoBack(); 
       else 
        NavigationService.Navigate(new Uri("/Menu.xaml", UriKind.Relative)); 
       return; 
      } 
     } 
    } 

    private void snareAudioPlayer_MediaOpened(object sender, RoutedEventArgs e) 
    { 
     if (!MediaPlayer.GameHasControl) 
     { 
      MessageBoxResult Choice; 
      Choice = MessageBox.Show("Media is currently playing, do you want to stop it?", "Stop Player", MessageBoxButton.OKCancel); 
      if (Choice != MessageBoxResult.OK) 
      { 
       if (NavigationService.CanGoBack) 
        NavigationService.GoBack(); 
       else 
        NavigationService.Navigate(new Uri("/Menu.xaml", UriKind.Relative)); 
       return; 
      } 
     } 
    } 
} 

}

とXAML

<Grid x:Name="LayoutRoot"> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="242*" /> 
     <ColumnDefinition Width="243*" /> 
     <ColumnDefinition Width="243*" /> 
    </Grid.ColumnDefinitions> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="Auto"/> 
     <RowDefinition Height="163*"/> 
     <RowDefinition Height="245*" /> 
    </Grid.RowDefinitions> 

    <!--TitlePanel contains the name of the application and page title--> 
    <StackPanel x:Name="TitlePanel" Margin="12,17,0,28" Grid.ColumnSpan="3"> 
     <TextBlock x:Name="ApplicationTitle" Text="BEGINNER EXERCISES" Style="{StaticResource PhoneTextNormalStyle}" Foreground="Black" /> 
     <TextBlock x:Name="PageTitle" Text="{Binding ExTitle}" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}" Foreground="Black" /> 
    </StackPanel> 

    <!--ContentPanel - place additional content here--> 

     <Image x:Name="im" Grid.Row="1" Margin="12,0,12,155" Grid.RowSpan="2" Grid.ColumnSpan="3" /> 

    <Button Grid.Row="2" Height="72" HorizontalAlignment="Left" Margin="41,13,0,0" x:Name="button1" VerticalAlignment="Top" Width="160" Click="button1_Click" BorderBrush="Black" Foreground="Black" ClickMode="Release"> 
     <Button.Background> 
      <ImageBrush ImageSource="/RhythmCoach;component/Images/appbar.transport.play.rest.png" Stretch="None" /> 
     </Button.Background> 
    </Button> 
    <MediaElement Grid.Row="2" Height="120" HorizontalAlignment="Left" Name="guitarAudioPlayer" VerticalAlignment="Top" Width="160" Source="{Binding ExGuitarAudio}" AutoPlay="False" Volume="100" MediaOpened="guitarAudioPlayer_MediaOpened" /> 
    <MediaElement Grid.Row="2" Height="120" HorizontalAlignment="Left" Name="pianoAudioPlayer" VerticalAlignment="Top" Width="160" Source="{Binding ExPianoAudio}" AutoPlay="False" Volume="100" MediaOpened="pianoAudioPlayer_MediaOpened" /> 
    <MediaElement Grid.Row="2" Height="120" HorizontalAlignment="Left" Name="snareAudioPlayer" VerticalAlignment="Top" Width="160" Source="{Binding ExSnareAudio}" AutoPlay="False" Volume="100" MediaOpened="snareAudioPlayer_MediaOpened" /> 
    <Button Grid.Row="2" Height="72" HorizontalAlignment="Left" Margin="41,13,0,0" Name="button2" VerticalAlignment="Top" Width="160" Click="button2_Click" Grid.Column="2" BorderBrush="Black" Foreground="Black" HorizontalContentAlignment="Center"> 
     <Button.Background> 
      <ImageBrush ImageSource="/RhythmCoach;component/Images/appbar.transport.stop.rest1.png" Stretch="None" /> 
     </Button.Background> 
    </Button> 
    <Button Grid.Column="1" Grid.Row="2" Height="72" HorizontalAlignment="Left" Margin="41,13,0,0" Name="button3" VerticalAlignment="Top" Width="160" Click="button3_Click" Foreground="Black" BorderBrush="Black"> 
     <Button.Background> 
      <ImageBrush ImageSource="/RhythmCoach;component/Images/appbar.transport.pause.rest.png" Stretch="None" /> 
     </Button.Background> 
    </Button> 
    <RadioButton Content="Guitar" Grid.Row="2" Height="72" HorizontalAlignment="Left" Margin="41,101,0,0" Name="rbBegGuitar" VerticalAlignment="Top" Foreground="Black" Background="#BFADADAD" BorderBrush="#BFFFFFFF" Checked="rbBegGuitar_Checked" /> 
    <RadioButton Content="Piano" Grid.Column="1" Grid.Row="2" Height="72" HorizontalAlignment="Left" Margin="41,101,0,0" Name="rbBegPiano" VerticalAlignment="Top" Background="#BFA3A3A3" Foreground="Black" Checked="rbBegPiano_Checked" /> 
    <RadioButton Content="Snare Drum" Grid.Column="2" Grid.Row="2" Height="72" HorizontalAlignment="Left" Margin="32,101,0,0" Name="rbBegSnare" VerticalAlignment="Top" Background="#BF939393" Foreground="Black" Checked="rbBegSnare_Checked" /> 
    <my:AdControl AdUnitId="10029089" ApplicationId="05ab3750-df60-4e42-9939-ab68de9f424b" Height="50" HorizontalAlignment="Left" Margin="186,17,0,0" Name="adControlSmall" VerticalAlignment="Top" Width="300" Grid.ColumnSpan="2" Grid.Column="1" /> 
    <Grid.Background> 
     <ImageBrush ImageSource="/RhythmCoach;component/Images/bg1.png" Stretch="Fill" /> 
    </Grid.Background> 
</Grid> 

答えて

1

は、ここでは、コードです。私は私がLoaded()にいると思う。あなたのmediaElementsがAutoPlayに設定されていないので、問題ではないはずです。プロンプトを完全に削除しても、ページ上のどのボタンも押さずにバックグラウンドミュージックが一時停止しますか?

これを処理する別の方法は、アプリケーションを実行するたびにプロンプ​​トを表示するのではなく、起動時にアプリを実行する際に常にバックグラウンドミュージックを一時停止するかどうかを尋ねることです。その設定を保存して、その後の実行で確認してください。メインメニューを持っているようですので、バックグラウンドミュージックを一時停止することに同意していないと、あなたのページに音楽を送ることはできません。

私は自分のコードを貼り付けするつもりだったが、私はあなたが後にしている肉アウト抽出しているので、それは少し混乱していた可能性があります、これはあなたが求めてきたものであるかもしれないが

protected override void OnNavigatedTo(NavigationEventArgs e) 
{ 
    base.OnNavigatedTo(e); 
    //remember don't show MessageBoxes in OnNavigatedTo.. 
    // better to move stuff over to Loaded as much as you can anyway 
    this.Loaded += RunOnPageLoaded; //you could hook up to this event via the Designer too 
} 

void RunOnPageLoaded(object sender, RoutedEventArgs e) 
{ 
    bool doLoad = true; 
    FrameworkDispatcher.Update(); 
    if (!MediaPlayer.GameHasControl) 
    { 
     doLoad = PromptUserAboutBackgroundMusic(); 
     if (doLoad) MediaPlayer.Pause(); 
    } 

    if (doLoad) 
    { 
     LoadMediaFiles(); 
    } 
} 

private bool PromptUserAboutBackgroundMusic() 
{ 
    var result = MessageBox.Show("Do you want to stop your background music to play this recording?", 
           "Stop music?", MessageBoxButton.OKCancel); 
    if (result == MessageBoxResult.OK) return true; 
    return false; 
} 

private void LoadMediaFiles() 
{ 
    //load your media files into your mediaelements here 
    //mediaElement.AutoPlay = true; 
    //mediaElement.Source = ... 
} 

をあなたが何をしているのか見た後、私はMediaElementsがあなたにとって最高のものであるとは確信していません。代わりにSoundEffectInstanceを使用した場合、バックグラウンドミュージックは一時停止されず、そのフィーチャーを追加しない限り、バックグラウンドミュージックに関するプロンプトでも気にする必要はありません。

+0

お返事ありがとうございます!はい、プロンプトなしで一時停止します。後でコードを投稿していただきありがとうございます!うまくいけば、私はこの問題を解決するのに役立ちます。 –

+0

ありがとうございます!上記のコードはそれを解決しました!今それは動作し、クラッシュしません。 –

+0

私はしばらくそれをテストしたので、プロンプトに再びクラッシュすることがあります。プロンプトを4〜5秒間開いたままにしておくと、アプリが終了します。それはそこにあるはずですか?それ以外はそれだけでうまくいく。 –

0

MediaPlayer.GameHasControlを呼び出す前に、FrameworkDispatcher.Update()を呼び出すことができます。例外があなたのアプリをクラッシュさせないようにするのに役立ちます。

+0

返信ありがとうございます!私はOnNavigatedTo()で試しましたが、それでもクラッシュしました。クラッシュすると、5秒ほど後にアプリを終了することになります。 –

+0

OnNavigatedTo()には、rbBegSnare.IsChecked = trueを設定するともう一度表示されます。 MediaElementのいくつかで.Stop()を呼び出します。その行をコメントアウトして、まだ音楽を一時停止しているかどうかを確認してください。それが修正された場合、たとえばフラグを使ってできることがいくつかあります。 –

+1

Heh私は上記の間違ったコメントにコメントしました。とにかく、このスレッドのコメント:OnNavigatedToのクラッシュは、そこにメッセージボックスのプロンプトが表示されたために発生している可能性があります。もしあなたがそれを行い、X秒間にボタンをクリックしないと、アプリケーションはナビゲーションを考えるので殺されます失敗しました。 –

1
public MainPage() 
    { 
     InitializeComponent(); 

     bool canPlayMusic = true; 

     FrameworkDispatcher.Update(); 

     if (!MediaPlayer.GameHasControl) 
     { 
      if (MessageBox.Show("Is it ok to stop currently playing music?", 
       "Can stop music?", MessageBoxButton.OKCancel) == MessageBoxResult.Cancel) 
      { 
       canPlayMusic = false; 
      } 
     } 

     if (canPlayMusic) 
     { 
      MediaPlayer.Pause(); 

      runBackgroundMusic(); 
     } 
    } 
+0

は、音楽プレーヤーを一時停止します。しかし、私は音楽プレーヤーを閉じたい。一時停止すると、ユーザーが音量を上げようとすると、音楽の一時停止状態が表示されるためです。したがって、ユーザーは曲をもう一度再生できます。 これで、ユーザーが[OK]を押したときに音楽プレーヤーを終了します。 どうすればいいですか?????? – SaravanaKumar

関連する問題