[Windows Phone|C#] 送審驗證錯誤-6.5.1 If the app plays its own background music or adjusts background music volume, it must ask the user for consent to stop playing/adjust the background music (e.g. message dialog or settings menu).

[Windows Phone|C#] 送審驗證錯誤-6.5.1 If the app plays its own background music or adjusts background music volume, it must ask the user for consent to stop playing/adjust the background music (e.g. message dialog or settings menu).

當發應用程式時,有些情況要會需要用到撥放音樂的功能或資源(特別是遊戲),在Windows Phone中,撥放音樂是透過MediaPlayer這個類別庫,並且使用MediaPlayer一次只能撥放一首音樂



所以當你的應用程式在啟動要撥放音樂的功能時,使用者已有在撥放音樂的話,基於良好的使用者體驗,我們的應用程式因該告知使用者:有音樂正在撥放,請問是要關閉音樂、調整音量還是離開應用程式,又或者是關閉我們應用程式的音樂(這些選擇依你的設計,像我是要嘛進入遊戲關閉正在撥放的音樂,

前言

 


 

這是最近上架的遊戲,被退件的另一個原因:

「When the user is already playing music on the phone when the app is launched, the app must not pause, resume, or stop the active music in the phone MediaQueue by calling the Microsoft.Xna.Framework.Media.MediaPlayer class.

If the app plays its own background music or adjusts background music volume, it must ask the user for consent to stop playing/adjust the background music (e.g. message dialog or settings menu」

 

當發應用程式時,有些情況要會需要用到撥放音樂的功能或資源(特別是遊戲),在Windows Phone中,撥放音樂是透過MediaPlayer這個類別庫,並且使用MediaPlayer一次只能撥放一首音樂

 

所以當你的應用程式在啟動要撥放音樂的功能時,使用者已有在撥放音樂的話,基於良好的使用者體驗,我們的應用程式因該告知使用者:有音樂正在撥放,請問是要關閉音樂、調整音量還是離開應用程式,又或者是關閉我們應用程式的音樂(這些選擇依你的設計,像我是要嘛進入遊戲關閉正在撥放的音樂,不然離開遊戲)

 

而這邊,就是要紀錄如何判斷有無撥放音樂(我開發的是WP7),所以同樣開發7.1的朋友們遇到此問題的話,也可使用看看:)

 

透過MediaPlayer.GameHasControl

 


 

首先請記得,在你的程式中加入:


using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Media;

來使用MediaPlayer

 

然後透過

MediaPlayer.GameHasControl

來判斷使用者有無撥已經在放音樂

布林值 意思
false 使用者有在放音樂,告知使用者
true 沒有放音樂,可使用

 

便可以如下判斷,是否已有正在撥放音樂判斷


if (!MediaPlayer.GameHasControl){ //進入表示有在撥放音樂

     string msg = String.Format("Music + Videos Hub is currently playing music. Game needs to stop music.\nPlease click Ok to continue or Cancel to close the Game.");
     MessageBoxResult res = MessageBox.Show(msg, "Music + Videos Hub", MessageBoxButton.OKCancel);

    if (res == MessageBoxResult.Cancel)
    {
             //still run app and close your music or close app
     }
     else
     {
            //start game or app or load play music

      }
}
else{
          //start game or app or load play music
}

關閉正在撥放的音樂

 


 

得知已有正在撥放音樂,如果我們要停止正在撥放的音樂,便可以如此做


if (MediaPlayer.State == MediaState.Playing) {
       MediaPlayer.Stop();
}

便可以關閉正在撥放的音樂,不過如果你是撥放播客(PodCast)的話,他便不會進入判斷式PodCast,因為播客Podcast是走AudioPlayer,所以撥放播客的話,MediaPlayer.State 會是Stop,不進入判斷式

但是好像Mediaplayer優先權高,所以仍會自動關掉Podcast

 

完整判斷程式碼:


if (!MediaPlayer.GameHasControl){ //進入表示有在撥放音樂

     string msg = String.Format("Music + Videos Hub is currently playing music. Game needs to stop music.\nPlease click Ok to continue or Cancel to close the Game.");
     MessageBoxResult res = MessageBox.Show(msg, "Music + Videos Hub", MessageBoxButton.OKCancel);

    if (res == MessageBoxResult.Cancel)
    {
             //close your music or close app
     }
     else
     {

            if (MediaPlayer.State == MediaState.Playing) {
                MediaPlayer.Stop();
             }
            //start game or app or load play music

      }
}
else{
          //start game or app or load play music
}

 

結論

希望這篇文章能幫助遇到依樣問題的朋友,快速排除並上架:)

 

參考資料

How to Play Music in XNA games on Windows Phone 7 (and still pass Cert)
MediaPlayer.GameHasControl is not working properly
Part 64 - Windows Phone - MediaPlayer and Marketplace

 


 

文章中的敘述如有觀念不正確錯誤的部分,歡迎告知指正 謝謝 =)

另外要轉載請附上出處 感謝