XNA MediaPlayer fastforward/rewind - xna

Using XNA 4.0, I am currently playing songs from the user's PC/XBox360 using the following:
Microsoft.Xna.Framework.Media.MediaPlayer.Play(Song song);
I see that MediaPlayer has a static Property PlayPosition that, as I've researched, used to be a get/set property, but it has been updated to get-only.
Are there any other methods/tricks I can use to explicitly set the playing position of a currently playing song?

It looks liked they added a DynamicSoundEffectInstance class in XNA 4.0. One of the member functions is SubmitBuffer, which allows you to set the "Offset, in bytes, to the starting position of the data."
Please note that I haven't tried this out yet, and I couldn't tell you how to get your audio into a buffer of bytes (maybe you can read it from a XACT wavebank somehow?). It's also probably going to be a pain to do this because it doesn't look like you can remove the buffer if you decide to change position again. You would have to either let it play to the end or create a new class and submit a new buffer with a new offset. So you could keep your byte buffer in memory, and just pass it to new DynamicSoundEffectInstance classes with a new offset each time you wanted to change position.
Like I said, I haven't tried this out yet, and I'm just going by the documentation on MSDN. I searched around for a while, and Shawn Hargreaves said back in October '09 before XNA 4.0 came out that he doesn't think this can be done. Since the PlayPosition is still a get only property in XNA 4.0, I don't think you'll have any luck there, but the DynamicSoundEffectInstance is new and may give you what you need.

Related

Can someone explain me TTextureStyle.RenderTarget?

TTexture can have the style [TTextureStyle.RenderTarget]
the doc of delphi says:
Specifies the rendering target for this canvas object. Use the value
of RenderTarget to access the specific properties of the target on
which the canvas is drawn. RenderTarget returns a Direct2D interface
that can be used directly.
This is a little obscure, can someone explain me exactly the purpose of TTextureStyle.RenderTarget and in which condition we need to use it ?
To make the question also useful for everyone, if someone can also explain all the other different values (and their purpose) that TTextureStyle can take :
TTextureStyle = (MipMaps, Dynamic, RenderTarget, Volatile);

How does g_main_loop_unref(GMainLoop* loop) work?

The Question
Excerpt from the documentation:
Decreases the reference count on a GMainLoop object by one.
If the result is zero, free the loop and free all associated memory.
I could not find information regarding this reference counter. What is it initially set to and how is it used?
Details
In particular, I'm confused about this piece of example code (in the main method) (note that set_cancel is a static method:
void (*old_sigint_handler)(int);
old_sigint_handler = signal (SIGINT, set_cancel);
/* Create a new glib main loop */
data.main_loop = g_main_loop_new (NULL, FALSE);
old_sigint_handler = signal (SIGINT, set_cancel);
/* Run the main loop */
g_main_loop_run (data.main_loop);
signal (SIGINT, old_sigint_handler);
g_main_loop_unref (data.main_loop);
If g_main_loop is blocking, how it ever going to stop? I could not find information on this signal method either. But that might be native to the library (although I do not know).
Note: I reduced the code above to what I thought was the essential part. It is from a camera interface library called aravis under docs/reference/aravis/html/ArvCamera.html
I could not find information regarding this reference counter. What is it initially set to and how is it used?
It is initially set to 1. Whenever you store a reference to the object you increment the reference counter, and whenever you remove a reference you decrement the reference counter. It's a form of manual garbage collection. Just google "reference counting" and you'll get lots of information.
If g_main_loop is blocking, how it ever going to stop?
Somewhere someone will call g_main_loop_quit. Judging by the question I'm guessing you're not very familiar with the concept of an event loop—GLib's manual isn't a very gentle introduction to the basic concept, you may want to try the Wikipedia article or just search for "event loop".
I could not find information on this signal method either. But that might be native to the library (although I do not know).
The signal is a standard function (both C and POSIX). Again, there is lots of information out there, including good old man pages (man 2 signal).

Stream which implements a Seek method

I'm trying to find an interface which allows me to create a stream which allows seeking (just a Reader is fine, too) from either a file or []byte, but can't seem to find anything in the godoc. Some of the types in the bufio package would work quite well, but they don't appear to support seeking.
Is there something I overlooked which would fit what I'm looking for?
Both *os.File (for files) and *bytes.Reader (for having an io.Reader from a []byte) implement the io.Seeker interface and thus have a Seek method.
io.Seeker is implemented by...
*bytes.Reader
*io.SectionReader
io.ReadSeeker
io.WriteSeeker
io.ReadWriteSeeker
mime/multipart.File
net/http.File
*os.File
*strings.Reader
So if you're working with a file, thus very likely *os.File, you don't need to do anything additional to be able to seek it. Just make sure that if you're using interfaces instead of concrete types that you do not want an io.Reader but an io.ReadSeeker.

XNA draw : using one spritebatch at whole game

I am developing an XNA game. This is time I am careful about the architecture. Til today, I have always implemented my own draw method in this way:
public void Draw(SpriteBatch sb, GameTime gameTime)
{
sb.Begin();
// ... to draw ...
sb.End();
}
I was digging the DrawableGameComponent and saw the Draw method comes in this way:
public void Draw(GameTime gameTime)
{
// ...
}
First of all, I know that SpriteBatch can collect many Texture2D between Begin and End, so that it can be useful to sort them, or draw with same Effects.
My question is about the performance and the cost of passing the SpriteBatch. At DrawableGameComponent it is possible to call spritebatch of game object if its spritebatch is public.
What is suggested, what should a xna-game programmer do?
Thanks in advice.
One of the serious disadvantages of DrawableGameComponent is that you're locked into its provided method signature. While there's nothing "wrong", per se, with DrawableGameComponent, do not think of it as the "one true architecture". You're better off thinking of it as an example of a possible architecture.
If you find yourself needing to pass a SpriteBatch (or anything else) to the draw method of a "game component" - the best way is to pass it as an argument. Anything else is convoluted.
Obviously this means that you can't use XNA's provided GameComponent system, and you have to make your own alternative. But this is almost trivial: At its most basic level, it's just a list of some base type that has appropriate virtual methods.
Of course, if you must use GameComponent - or your game is so simple (eg: a prototype) that you don't really care about the architecture - then you can use basically any method you like to get a SpriteBatch to your draw method. They all have disadvantages.
Probably the next-most architecturally robust method is to pass your SpriteBatch instance into the constructor of each of your components. This keeps your components decoupled from your game class.
On the other hand, if you're throwing architecture to the wind, I'd suggest making your MyGame.spriteBatch field public static. This is the simplest way to allow it to be accessed anywhere. It's easy to implement and easy to clean up later when/if you need to.
To answer your question about performance: Anything to do with passing a SpriteBatch around will have almost negligible effect on performance (providing the order of calls to Draw/Begin/End stays the same). Don't worry about it.
(If you see SpriteBatch whatever in code, that represents a reference. A reference is a 32-bit value (in a 32-bit program, which all XNA games are). That's the same size as an int or a float. It's very small and very cheap to pass around/access/etc.)
If you stumbled upon this question you were probably looking at a nice and generic solution. I would suggest you have a look at this:
https://gamedev.stackexchange.com/questions/14217/several-classes-need-to-access-the-same-data-where-should-the-data-be-declared/14232#14232
I felt the need to correct this question because "the best way is to pass it as an argument. Anything else is convoluted." is simply not correct.
Personally i am now doing it this way in my GameBase class:
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
SpriteBatch = new SpriteBatch(GraphicsDevice);
this.Services.AddService(typeof(SpriteBatch), SpriteBatch);
}
Now, since you're adding DrawableGameComponents in the Initialize Method of your Game class you will be able to call
this.Game.Services.GetService(typeof(SpriteBatch))
I'd say that's the cleanest approach to solve the problem.
If the DrawableGameComponent should be part of the parent's SpriteBatch, then just pass it in via the constructor and store it in a private member (or expose it as a property if you wish).
You could also expose the SpriteBatch as a property of your Game class if you wanted, like you suggested, but every time you referenced this.Game, you would need to cast it to your specific Game class type.
((MyGame)this.Game).SpriteBatch.Draw(...)
Or you can just have the DrawableGameComponent create a new SpriteBatch. There's nothing wrong with that (as far as I've ever seen). I suppose it depends how many DrawableGameComponents you'll be creating and how often.
Also browse through the results for a search for DrawableGameComponent - there's a lot of good advice there.

How to free a Interface?

i'm new with this thing named interface and DirectX.
I'm hooking a DirectX Interface from a certain game and i'm using the DirectX to Draw My Own stuff,like textures and Fonts.
My problem are that: When the program call the Hooked Reset Function of the Device,i need to clear all my things from the memory,the Com Interfaces.If i not clear,after the Reset event are called,the Game just try to create a new surface calling d3dDierctx9Create but its fail and just make a error and close the game.
I think that i just need to clear all the things before the Reset Event,its is explained on Msdn.
You don't free an interface. It's reference-counted and managed by the compiler. Let it go out of scope, assign a different interface to the variable, or assign nil to it, and the compiler will generate a call to its _Release method automatically so it can clean itself up when its reference count drops to 0.

Resources