Windows Phone Audio Crash

0 favourites
  • 1 posts
From the Asset Store
Get Ready for the New Wave of "Bottom Screen" Games!
  • I have used the following tutorial and was able to audio working for the most part: https://www.scirra.com/tutorials/593/windows-phone-games-with-construct-2#h2a2

    I am having a couple issues:

      1. Audio doesnt stop or pause on Windows Phone emulator or device
      2. Game crashes when going to the next layout

    For the first, I am playing background music during most of the game. I want it to pause when I get to a question when another audio file plays. On the emulator and device the background music keeps playing while the question music starts. I have tried stopping all, stopping tag, etc but nothing seems to work. I must be missing something.

    For the second, there is an audio clip that plays for a correct or incorrect answer at the end of the layout, pause for 2 seconds then goes back to the question list layout. The game is crashing after I select an answer.

    There must be something in the C# edits because if I test before adding that portion in everything works, except the audio of course. The C# audio section must be the issue.

    Any help would be appreciated.

    Here is the code that I am using from the above link

    MainPage.xaml - Add this into the "…phone:WebBrowser…" section

    ScriptNotify="Browser_ScriptNotify"[/code:3ph8sn0x]
    
    MainPage.xaml.cs - Add the following into the top references:
    [code:3ph8sn0x]// The following are required for the Windows Phone Plugin
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Audio;
    using Microsoft.Xna.Framework.Media;
    using System.IO;
    using System.Runtime.Serialization.Json;
    using Microsoft.Devices;
    using System.Threading.Tasks;
    using Microsoft.Phone.Marketplace;
    using Microsoft.Phone.Tasks;
    using System.Globalization;
    [/code:3ph8sn0x]
    
    MainPage.xaml.cs - Insert the following into code after the "… navigation failures." section
    [code:3ph8sn0x]        // Convert DB volume to scale
    
            public float dbToScale(Single db)
            {
                float volume = (Single)Math.Pow(10, db / 20);
                volume = volume * (6 - (volume * 3)); // Added to account for quiet sounds being too quiet. Not a technically correct conversion.
                return Math.Max(0, Math.Min(1, volume));
            }
    
            // Check whether user is playing music on the phones media player
    
            public void checkMusic()
            {
                FrameworkDispatcher.Update();
    
                if (MediaPlayer.GameHasControl)
                {
                    try
                    {
                        Browser.InvokeScript("eval", "window['deviceMusicPlaying'] = false;");
                    }
                    catch
                    {
                    }
                }
                else
                {
                    try
                    {
                        Browser.InvokeScript("eval", "window['deviceMusicPlaying'] = true;");
                    }
                    catch
                    {
                    }
                }
            }
            // Sound effect instance
    
            SoundEffectInstance sound = null;
            public Dictionary<string, SoundEffectInstance> SoundList = new Dictionary<string, SoundEffectInstance>();
            // **********************************************************************
            // JavaScript communicating with C#
            // **********************************************************************
    
            private async void Browser_ScriptNotify(object sender, NotifyEventArgs e)
            {
    
                // Get a comma delimited string from js and convert to array
                string valueStr = e.Value;
                string[] valueArr = valueStr.Split(',');
    
                // Trim and convert empty strings to null
                for (int i = 0; i < valueArr.Length; i++)
                {
                    valueArr[i] = valueArr[i].Trim();
                    if (string.IsNullOrWhiteSpace(valueArr[i]))
                        valueArr[i] = null;
                }
                // Stop music
                if (valueArr[0] == "stopMusic")
                {
                    if (MediaPlayer.GameHasControl)
                    {
                        FrameworkDispatcher.Update();
                        MediaPlayer.Stop();
                    }
                }
                // Play sound
                if (valueArr[0] == "playSound")
                {
    
                    var file = valueArr[1];
                    var loop = valueArr[2] == "0" ? false : true;
    
                    var db = Single.Parse(valueArr[3], CultureInfo.InvariantCulture);
                    var volume = Convert.ToSingle(dbToScale(db));
    
                    var tag = valueArr[4];
    
                    Stream stream = TitleContainer.OpenStream(file);
                    SoundEffect effect = SoundEffect.FromStream(stream);
    
                    sound = effect.CreateInstance();
                    sound.IsLooped = loop;
                    sound.Volume = volume;
    
                    if (!string.IsNullOrEmpty(tag))
                    {
                        if (SoundList.ContainsKey(tag))
                        {
                            SoundList[tag].Stop();
                        }
                        SoundList[tag] = sound;
                    }
    
                    FrameworkDispatcher.Update();
                    sound.Play();
    
                }
            }
    [/code:3ph8sn0x]
  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)