Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
AudioEngine.Windows.Runtime.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 #if SILICONSTUDIO_PLATFORM_WINDOWS_RUNTIME
4 using System;
5 using SharpDX.MediaFoundation;
6 using SharpDX.Multimedia;
7 
8 namespace SiliconStudio.Paradox.Audio
9 {
10  partial class AudioEngine
11  {
12  private MediaEngine mediaEngine;
13  private MediaEngineEx mediaEngineEx;
14 
15  private void PlatformSpecificInit()
16  {
17  // Setup Media Engine attributes
18  using (var attributes = new MediaEngineAttributes { AudioEndpointRole = AudioEndpointRole.Console,
19  AudioCategory = AudioStreamCategory.GameEffects })
20  {
21  var creationFlags = MediaEngineCreateFlags.None;
22 #if SILICONSTUDIO_PLATFORM_WINDOWS_STORE
23  // MSDN: On the phone, the Media Engine only supports frame-server mode. Attempting to initialize the interface in either rendering mode or audio mode will fail.
24  creationFlags |= MediaEngineCreateFlags.AudioOnly;
25 #endif
26  using (var factory = new MediaEngineClassFactory())
27  mediaEngine = new MediaEngine(factory, attributes, creationFlags, OnMediaEngineEvent);
28 
29  mediaEngineEx = mediaEngine.QueryInterface<MediaEngineEx>();
30  }
31  }
32 
33  private void PlatformSpecificDispose()
34  {
35  mediaEngine.Shutdown();
36  mediaEngine.Dispose();
37  }
38 
39  private void ResetMusicPlayer()
40  {
41  StopCurrentMusic();
42 
43  isMusicPlayerReady = false;
44 
45  currentMusic = null;
46  }
47 
48  private void RestartCurrentMusic()
49  {
50  mediaEngine.CurrentTime = 0;
51  }
52 
53  private void StartCurrentMusic()
54  {
55  mediaEngine.Play();
56  }
57 
58  private void UpdateMusicVolume()
59  {
60  mediaEngine.Volume = currentMusic.Volume;
61  }
62 
63  private void StopCurrentMusic()
64  {
65  PauseCurrentMusic();
66  RestartCurrentMusic();
67  }
68 
69  private void PauseCurrentMusic()
70  {
71  mediaEngine.Pause();
72  }
73 
74  private void LoadNewMusic(SoundMusic lastPlayRequestMusicInstance)
75  {
76  currentMusic = lastPlayRequestMusicInstance;
77 
78  mediaEngineEx.SetSourceFromByteStream(new ByteStream(currentMusic.Stream), "MP3");
79  }
80 
81  private struct MediaEngineErrorCodes
82  {
83  public long Parameter1;
84  public int Parameter2;
85 
86  public MediaEngineErrorCodes(long param1, int param2)
87  {
88  Parameter1 = param1;
89  Parameter2 = param2;
90  }
91  }
92 
93  private void OnMediaEngineEvent(MediaEngineEvent mediaEvent, long param1, int param2)
94  {
95  switch (mediaEvent)
96  {
97  case MediaEngineEvent.LoadedMetadata:
98  break;
99  case MediaEngineEvent.CanPlay:
100  musicMediaEvents.Enqueue(new SoundMusicEventNotification(SoundMusicEvent.ReadyToBePlayed, null));
101  break;
102  case MediaEngineEvent.Ended:
103  musicMediaEvents.Enqueue(new SoundMusicEventNotification(SoundMusicEvent.EndOfTrackReached, null));
104  break;
105  case MediaEngineEvent.Error:
106  musicMediaEvents.Enqueue(new SoundMusicEventNotification(SoundMusicEvent.ErrorOccurred, new MediaEngineErrorCodes(param1, param2)));
107  break;
108  }
109  }
110 
111  private void ProcessMusicError(SoundMusicEventNotification eventNotification)
112  {
113  if (eventNotification.Event == SoundMusicEvent.ErrorOccurred)
114  {
115  var errorCodes = (MediaEngineErrorCodes) eventNotification.EventData;
116 
117  if (errorCodes.Parameter1 == (long)MediaEngineErr.SourceNotSupported)
118  {
119  if(currentMusic!=null)
120  ResetMusicPlayer();
121  else
122  throw new AudioSystemInternalException("Audio Engine is in an unconsistant state. CurrentMusic is null while Error on Unsupported media was reached.");
123 
124  throw new InvalidOperationException("The data format of the source music file is not valid.");
125  }
126 
127  throw new AudioSystemInternalException("An unhandled exception happened in media engine asynchronously. Error info [param1="+errorCodes.Parameter1+", param2="+errorCodes.Parameter2+"].");
128  }
129  }
130 
131  private void ProcessMusicMetaData()
132  {
133  throw new System.NotImplementedException();
134  }
135 
136  private void ProcessPlayerClosed()
137  {
138  throw new System.NotImplementedException();
139  }
140 
141  private void PlatformSpecificProcessMusicReady()
142  {
143  // nothing to do here
144  }
145  }
146 }
147 
148 #endif
System.Console Console