XNA extending the existing Content type
- by Maarten
We are doing a game in XNA that reacts to music. We need to do some offline processing of the music data and therefore we need a custom type containing the Song and some additional data:
// Project AudioGameLibrary
namespace AudioGameLibrary
{
public class GameTrack
{
public Song Song;
public string Extra;
}
}
We've added a Content Pipeline extension:
// Project GameTrackProcessor
namespace GameTrackProcessor
{
[ContentSerializerRuntimeType("AudioGameLibrary.GameTrack, AudioGameLibrary")]
public class GameTrackContent
{
public SongContent SongContent;
public string Extra;
}
[ContentProcessor(DisplayName = "GameTrack Processor")]
public class GameTrackProcessor : ContentProcessor<AudioContent, GameTrackContent>
{
public GameTrackProcessor(){}
public override GameTrackContent Process(AudioContent input, ContentProcessorContext context)
{
return new GameTrackContent()
{
SongContent = new SongProcessor().Process(input, context),
Extra = "Some extra data" // Here we can do our processing on 'input'
};
}
}
}
Both the Library and the Pipeline extension are added to the Game Solution and references are also added. When trying to use this extension to load "gametrack.mp3" we run into problems however:
// Project AudioGame
protected override void LoadContent()
{
AudioGameLibrary.GameTrack gameTrack = Content.Load<AudioGameLibrary.GameTrack>("gametrack");
MediaPlayer.Play(gameTrack.Song);
}
The error message:
Error loading "gametrack". File contains Microsoft.Xna.Framework.Media.Song but trying to load as AudioGameLibrary.GameTrack.
AudioGame contains references to both AudioGameLibrary and GameTrackProcessor. Are we maybe missing other references?