This content is based on Beta release, expect many changes in the final release.
Read What’s New in Silverlight 5 series here.
In this article, we’ll have a brief discussion of the new features in media stack in Silverlight 5.
Silverlight 5 has undergone few changes in the media stack:
- Better Battery Management:
Less battery when playing media. Plus, no screensaver in the full-screen mode.
- Remote Control Support:
Now you can control the media being played in Silverlight 5 using a remote control. This feature is currently not available in the Beta.
- Hardware Decoding; More GPU Support:
For better performance, Silverlight 5 depends more on the GPU for media decoding especially for the high definition H.264 formats. In Beta, no DRM or Mac support.
- 1080p Support:
Combined with hardware decoding, user can now watch high definition 1080p media on a Netbook or Tablet.
- IIS Media Services 4:
Support for media streaming using IIS Media Services 4 has been added.
And two more features require more details:
- Speed Control; Trick Play:
Allows you to control playing speed, and to play the media forward or backward.
- Low-Latency Sounds:
Silverlight 5 has added native support for raw wave files.
Speed Control; Trick Play
This feature allows you to control playing speed, to speed-up or slow-down the media. The ability to change playing speed is called, Trick Play. This feature is available in the MediaElement control through a new property called PlaybackRate. This property takes a decimal value ranges from -8, -4, -2, … to …, 0.5, 1.2, 1.4, 2, 4, 8, and the default is 1. The downside of the Beta version is that it doesn’t have a pitch correction, means that when you change the rate to a value other than the default (1) you wouldn’t have any sound. And because of a bug found in the Beta version, you must set this property in code, if you set it in XAML will reset at application start to the default value.
The following are three examples of changing the playing speed of a media element:
// Normal Speed media.PlaybackRate = 1.0; // Fast media.PlaybackRate = 4.0; // Slow media.PlaybackRate = -2.0;
In the past, you had to create a wave file parser that reads file headers to determine if this is a wave file, and after recognizing a wave file, the parser reads file contents and encode them in order for your application to be able to play it. That changed completely in Silverlight 5. Today, you have native support for raw sounds data (i.e. low-latency sounds.) You can now play wave files and wave data directly in your application without the need to encode them.
This feature is available through a new API based on the XNA Framework. It uses the same classes and functions available in XNA. The main class you have is SoundEffect (Microsoft.Xna.Framework.Audio) that you can use it to play wave files and raw data. You have all the control over the media played; you can set volume, pitch, and stereo space. And you can create a more sophisticated class of SoundEffect called SoundEffectInstance that represents an instance of that sound file. This instance can be played repeatedly, and allows you to have all the control like SoundEffect and more, as we’ll see in the examples.
Finally, keep in mind that Silverlight 5 has the same limitations as XNA; it allows only for 8/16bit PCM, mono/stereo, 22.5/44.1/48khz wave files.
The following code loads the SoundEffect class with a wave file available in application resources:
var resource = Application.GetResourceStream( new Uri("MediaLowLatency;component/tada.wav", UriKind.RelativeOrAbsolute)); SoundEffect effect = SoundEffect.FromStream(resource.Stream);
After you have the SoundEffect at your hands, you can call the Play() method to play it. This method have two overrides, one can accept no parameters in which it plays the media normally with the default volume and no changes in the pitch or the stereo space. The other override accepts three values:
Ranges from 0.0f to 1.0f.
Ranges from -1.0f to 1.0f. Default is 0f.
Controls the stereo space. Ranges from -1.0f to 1.0f. Default is 0f.
The following are some examples of playing sound files using SoundEffect:
// Normal effect.Play(); // Normal (default values) effect.Play(1.0f, 0f, 0f); // Faster effect.Play(1.0f, 1.0f, 0f); // Slower effect.Play(1.0f, -1.0f, 0f);
And finally you can use the SoundEffectInstance to create multiple instances of the same audio, or to play it repeatedly (Volume, Pitch, and Pan values are available through properties):
SoundEffectInstance instance = effect.CreateInstance(); instance.IsLooped = true; instance.Play();
While Silverlight 5 manages battery better, it also has improved media performance by depending more on the GPU for media decoding. And it allows now for high definition 1080p and H.264 videos. In addition, Silverlight 5 has introduced remote control support. And finally, the greatest features are its support for raw wave files and the ability to control playing speed.
Now, check out other What’s New in Silverlight 5? articles.