1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
using System;
using Android.Content;
using Android.Content.Res;
using Android.Media;
using Android.Util;
namespace Microsoft.Xna.Framework.Audio
{
internal class Sound : IDisposable
{
private const int MAX_SIMULTANEOUS_SOUNDS = 10;
private static SoundPool s_soundPool = new SoundPool(MAX_SIMULTANEOUS_SOUNDS, Stream.Music, 0);
private int _soundId;
private int _streamId;
internal static SoundPool SoundPool
{
get {
return s_soundPool;
}
}
internal static void PauseAll()
{
s_soundPool.AutoPause();
}
internal static void ResumeAll()
{
s_soundPool.AutoResume();
}
~Sound()
{
Dispose();
}
public void Dispose()
{
s_soundPool.Unload(_soundId);
}
public void Resume()
{
s_soundPool.Resume(_soundId);
}
public float Volume { get; set; }
public bool Looping { get; set; }
public float Rate { get; set; }
public float Pan { get; set; }
public double Duration
{
get { return 0; } // cant get this from soundpool.
}
public double CurrentPosition
{
get { return 0; } // cant get this from soundpool.
}
public bool Playing
{
get
{
return false; // cant get this from soundpool.
}
}
public void Play()
{
AudioManager audioManager = (AudioManager)Game.Activity.GetSystemService(Context.AudioService);
float streamVolumeCurrent = audioManager.GetStreamVolume(Stream.Music);
float streamVolumeMax = audioManager.GetStreamMaxVolume(Stream.Music);
float streamVolume = streamVolumeCurrent / streamVolumeMax;
float panRatio = (this.Pan + 1.0f) / 2.0f;
float volumeLeft = Volume * streamVolume * (1.0f - panRatio);
float volumeRight = Volume * streamVolume * panRatio;
float rate = (float)Math.Pow(2, Rate);
rate = Math.Max(Math.Min(rate, 2.0f), 0.5f);
_streamId = s_soundPool.Play(_soundId, volumeLeft, volumeRight, 1, Looping ? -1 : 0, rate);
}
public void Pause()
{
s_soundPool.Pause(_streamId);
}
public void Stop()
{
s_soundPool.Stop(_streamId);
}
public Sound(string filename, float volume, bool looping)
{
using (AssetFileDescriptor fd = Game.Activity.Assets.OpenFd(filename))
_soundId = s_soundPool.Load(fd.FileDescriptor, fd.StartOffset, fd.Length, 1);
this.Looping = looping;
this.Volume = volume;
}
public Sound(byte[] audiodata, float volume, bool looping)
{
throw new NotImplementedException();
}
internal static void IncreaseMediaVolume()
{
AudioManager audioManager = (AudioManager)Game.Activity.GetSystemService(Context.AudioService);
audioManager.AdjustStreamVolume(Stream.Music, Adjust.Raise, VolumeNotificationFlags.ShowUi);
}
internal static void DecreaseMediaVolume()
{
AudioManager audioManager = (AudioManager)Game.Activity.GetSystemService(Context.AudioService);
audioManager.AdjustStreamVolume(Stream.Music, Adjust.Lower, VolumeNotificationFlags.ShowUi);
}
}
}
|