XNA extending the existing Content type

Posted by Maarten on Game Development See other posts from Game Development or by Maarten
Published on 2012-03-29T16:45:55Z Indexed on 2012/03/29 17:43 UTC
Read the original article Hit count: 401

Filed under:
|
|

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?

© Game Development or respective owner

Related posts about XNA

Related posts about xna-4.0