Skip to content

Instantly share code, notes, and snippets.

@flibitijibibo
Created July 6, 2018 18:26
Show Gist options
  • Save flibitijibibo/b5142b553769884cdb88fe525f8ab4be to your computer and use it in GitHub Desktop.
Save flibitijibibo/b5142b553769884cdb88fe525f8ab4be to your computer and use it in GitHub Desktop.
VideoPlayer demo with reflection used to access the sound stream directly
/* VideoPlayer DynamicSoundEffectInstance Reverb/Filter Test Program
* Written by Ethan "flibitijibibo" Lee
* http://www.flibitijibibo.com/
*
* Released under public domain.
* No warranty implied; use at your own risk.
*/
using System;
using System.Reflection;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Graphics;
class Program : Game
{
static void Main(string[] args)
{
using (Program p = new Program())
{
p.Run();
}
}
GraphicsDeviceManager gdm;
Texture2D solid;
SpriteBatch sb;
VideoPlayer vp;
Video v;
FieldInfo sfi;
MethodInfo applyReverb;
MethodInfo applyFilter;
float filter, reverb;
Program() : base()
{
gdm = new GraphicsDeviceManager(this);
sfi = typeof(VideoPlayer).GetField(
"audioStream",
BindingFlags.Instance | BindingFlags.NonPublic
);
applyReverb = typeof(DynamicSoundEffectInstance).GetMethod(
"INTERNAL_applyReverb",
BindingFlags.Instance | BindingFlags.NonPublic
);
applyFilter = typeof(DynamicSoundEffectInstance).GetMethod(
"INTERNAL_applyLowPassFilter",
BindingFlags.Instance | BindingFlags.NonPublic
);
}
protected override void LoadContent()
{
sb = new SpriteBatch(GraphicsDevice);
solid = new Texture2D(GraphicsDevice, 1, 1);
solid.SetData(new Color[] { Color.White });
vp = new VideoPlayer();
v = Content.Load<Video>("steamedhams");
gdm.PreferredBackBufferWidth = v.Width;
gdm.PreferredBackBufferHeight = v.Height;
gdm.ApplyChanges();
vp.Play(v);
}
protected override void UnloadContent()
{
sb.Dispose();
solid.Dispose();
vp.Dispose();
v = null;
}
protected override void Update(GameTime gameTime)
{
GamePadState gp = GamePad.GetState(PlayerIndex.One);
if ( gp.IsButtonDown(Buttons.Start) ||
vp.State == MediaState.Stopped )
{
Exit();
return;
}
reverb = gp.Triggers.Left;
filter = gp.Triggers.Right;
object stream = sfi.GetValue(vp);
if (stream != null)
{
applyReverb.Invoke(stream, new object[] { reverb });
applyFilter.Invoke(stream, new object[]
{
Math.Max(1.0f - filter, 0.1f)
});
}
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
sb.Begin(SpriteSortMode.Deferred, BlendState.Opaque);
sb.Draw(vp.GetTexture(), Vector2.Zero, Color.White);
sb.Draw(
solid,
new Rectangle(
0, 0,
50, (int) (v.Height * reverb)
),
Color.Red
);
sb.Draw(
solid,
new Rectangle(
v.Width - 50, 0,
50, (int) (v.Height * filter)
),
Color.Blue
);
sb.End();
base.Draw(gameTime);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment