Archive
Gotcha: MediaElement Source=null releases stream, SetSource(null) fails
This is my contribution to:
If you use MediaElement, make sure you don’t get bitten by this one: http://msdn.microsoft.com/en-us/library/cc626563(v=vs.95).aspx
ArgumentNullException – The mediaStreamSource is null.
…After calling this method, MediaElement.Source returns null. If this is called and MediaElement.Source is set, the last operation wins.
If a MediaElement is removed from the UI tree while is has an opened MediaStreamSource, subsequent calls to SetSource may be ignored. To ensure featureSetSource calls will work, set the Source property to null before detaching the MediaElement from the UI tree.
naturally one would expect, if they only use SetSource(somestream) to use SetSource(null) to release the resources. Nope, they thought "better", you have to use Source=null instead to release resources and SetSource(null) throws ArgumentNullException
that is what I call a design bug (breaks the rule of "least expected" behavior and causes bugs that bite you at runtime only [unless somebody has made a static analysis rule to catch such a thing – would need metadata of course that some argument can’t be null, like in Code Contracts])
I managed to introduce this bug while refactoring some code in ClipFlair Studio‘s AudioRecorder control the other day 😦
Note that you can’t use at MediaElement something like Source = stream to open a Stream, since that is a Uri property (not an Object property to also accept Stream) and you have to use SetSource(stream) instead, so you’d also expect to be able to use SetSource(null) to release the resources.
Update: Fixed this in AudioRecorderView class (uses MVVM pattern) of AudioRecorderControl, at Audio property’s "set" accessor it needed the following null-guarding pattern:
if (mediaStreamSource != null)
player.SetSource(mediaStreamSource);
//must set the source once, not every time we play the same audio,
//else with Mp3MediaSource it will throw DRM error
else
player.Source = null;
Gotcha: MediaElement must be in visual tree for MediaOpened, MediaEnded to be fired
At ClipFlair’s AudioRecorderControl (used in Captions/Revoicing component of ClipFlair Studio), I use the following code to initialize a MediaElement to use for playback.
After a long time a found out that if the MediaElement is not in the visual tree (for example defined in XAML, or defined in code and then added to the visual tree), then it will not always fire MediaOpened and MediaEnded events, which are crucial if you want to have a two-state Play/Stop button (a ToggleButton).
public MediaElement Player { get { return player; }
set {
if (player != null) {
player.MediaOpened -= MediaElement_MediaOpened;
player.MediaEnded -= MediaElement_MediaEnded;
}
player = value;
if (player != null) {
player.MediaOpened += MediaElement_MediaOpened;
player.MediaEnded += MediaElement_MediaEnded;
player.AutoPlay = false;
player.PlaybackRate = 1.0;
player.Balance = 0;
Volume = DEFAULT_VOLUME;
}
} }
protected void MediaElement_MediaOpened(object sender, RoutedEventArgs e) {
try {
//player.Position = TimeSpan.Zero;
player.Stop(); //this stops current playback (if any) and rewinds
player.Play();
} catch (Exception ex) {
PlayCommandUncheck(); //depress playback toggle button
//don't talk to ToggleButton directly
Status = MSG_PLAY_FAILED + ex.Message;
} }
protected void MediaElement_MediaEnded(object sender, RoutedEventArgs e)
{
PlayCommandUncheck(); //depress play button to stop playback
//don't talk to ToggleButton directly }
Gotcha: MediaElement AutoPlay faster than doing Play at MediaOpened
Just added the following comment to: https://github.com/loarabia/ManagedMediaHelpers/issues/15
Managed Media Helpers contains the very useful Mp3MediaSource class for .NET / Silverlight / Windows Phone.
Added compile-time SWITCHES and respective code to Silverlight demo code for PRELOAD (into memory stream) and AUTOPLAY (this was a bit tricky, need to call Play at MediaOpened event, not right after setting the source to the MediaElement).
Also seems to be 2-3 sec slower than AutoPlay (!) irrespective of using PRELOAD. Maybe it is faster to set the source again every time you want to play and just keep AutoPlay=true
Update: had a bug at the following code, obviously you first must set the MediaOpened event handler, then set the Source to the MediaPlayer. Also, one could set that handler once at the InitializeComponent method.
//———————————————————————–
// <copyright file="Page.xaml.cs" company="Larry Olson">
// (c) Copyright Larry Olson.
// This source is subject to the Microsoft Public License (Ms-PL)
// See http://code.msdn.microsoft.com/ManagedMediaHelpers/Project/License.aspx
// All other rights reserved.
// </copyright>
//
// Edited by George Birbilis (http://zoomicon.com)
//———————————————————————–#define AUTOPLAY
//#define PRELOADnamespace Mp3MediaStreamSourceDemo
{
using Media;
using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;/// <summary>
/// A Page of a Silvelight Application.
/// </summary>
public partial class Page : UserControl
{
/// <summary>
/// Initializes a new instance of the Page class.
/// </summary>
public Page()
{
InitializeComponent();me.Volume = 1.0; //set max volume
#if !AUTOPLAY
me.AutoPlay = false;me.MediaOpened += (s, e) =>
{
//me.Position = TimeSpan.Zero;
me.Stop(); //this stops current playback (if any) and rewinds back to start
//me.PlaybackRate = 1.0;
try
{
me.Play();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
};
#endif
}/// <summary>
/// Event handler for the Button on the Page.
/// </summary>
/// <param name="sender">
/// The button which was clicked.
/// </param>
/// <param name="e">
/// The state when this event was generated.
/// </param>
private void OpenMedia(object sender, RoutedEventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.ShowDialog();Stream data = ofd.File.OpenRead();
#if PRELOAD
Stream m = new MemoryStream((int)ofd.File.Length);
data.CopyTo(m);
m.Position = 0;
data = m;
#endif
Mp3MediaStreamSource mediaSource = new Mp3MediaStreamSource(data);
me.SetSource(mediaSource);
}
}
}
Fix: Transform Manager ignoring media files added to watch folders
I’ve been puzzled for some days now with the IIS Transform Manager installation we have at ClipFlair for automating the conversion of media files (for example using Microsoft Expression Encoder) into IIS Smooth Streams.
The watch folder I’ve set up for this task was ignoring some of the media files I was dropping in for no apparent reason (e.g. it was picking up some .MP4 files but not other .MP4 ones).
Eventually I realized that the files it wasn’t picking up didn’t have at their Properties/Security tab access rights for the user account that has been assigned to the Transform Manager service, while the processed files (it keeps them at the “WorkQueue/Finished” subfolder” under the watch folder for the respective job [e.g. “Smooth (VC1)”]) had full access rights for that account.
Obviously by dropping them in the watch folder that had those rights assigned they also got the same rights, but not all the filed did. Maybe it is some Windows Server 2008 R2 bug when dropping multiple files together into a folder, not sure though. Adding the needed rights to each of those files, Transform Manager watch folder task immediately pulled them for processing (and soon put them into the “WorkQueue/Running”)
Another thing I noted was that at the Watch Folder task’s (say the “Video files to VC-1 Smooth Streams” ) “Scheduler” tab, at “Concurrent Jobs”, the default setting was set to 1. If you control how many files are placed in the watch folder (e.g. users aren’t uploading files there themselves) you can even check the value “Unlimited” there to process all the dropped files in parallel.
Collection of Smooth Streaming Video URLs
Here are some Smooth Streaming URLs I found on the Internet that you could use during development to test your smooth streaming players (like those based on SMF).
If you haven’t yet started building your SMF-based player you can try the following URLs here: http://www.smoothhd.com/livetestplayer/
Update: you can also try Smooth Stream URLs at ClipFlair Studio Silverlight-based application, turning over its Clip component (using the “Wrench” button on its titlebar) and pasting the Media URL there. Alternatively, you can launch it directly using a URL of the form http://studio.clipflair.net?video=http://smoothstreamer.doit.wisc.edu/doit-nms/BBB_carbon/BBB.ism/Manifest
http://smoothstreamer.doit.wisc.edu/doit-nms/BBB_carbon/BBB.ism/Manifest
http://streams.smooth.vertigo.com/BigBuckBunny_30sec/bigbuck.ism/manifest
http://streams.smooth.vertigo.com/elephantsdream/Elephants_Dream_1024-h264-st-aac.ism/manifest (this one also features multiple audio streams, director’s comments in english and also english and spanish dialogs)
http://playready.directtaps.net/smoothstreaming/TTLSS720VC1/To_The_Limit_720.ism/Manifest
http://playready.directtaps.net/smoothstreaming/SSWSS720H264/ SuperSpeedway_720.ism/Manifest
At the same test server one can also find two audio-only smooth streams:
http://playready.directtaps.net/smoothstreaming/ISMAAACLC/Taxi3_AACLC.ism/Manifest
http://playready.directtaps.net/smoothstreaming/ISMAAACHE/Taxi3_AACHE.ism/Manifest
http://mediadl.microsoft.com/mediadl/iisnet/smoothmedia/Experience/ BigBuckBunny_720p.ism/Manifest
http://ecn.channel9.msdn.com/o9/content/smf/smoothcontent/bbbwp7/big%20buck%20bunny.ism/manifest
http://video3.smoothhd.com.edgesuite.net/ondemand/Big%20Buck%20Bunny%20Adaptive.ism/Manifest
http://video3.smoothhd.com/ondemand/Turner_Sports_PGA.ism/Manifest
http://video3.smoothhd.com/ondemand/Turner_Sports_NASCAR.ism/Manifest
http://video3.smoothhd.com/ondemand/Turner_Sports_MLB.ism/Manifest
http://video3.smoothhd.com/ondemand/Akamai_ASP_Cutdown.ism/Manifest
http://video3.smoothhd.com/ondemand/mix1/mix1.ism/Manifest
http://video3.smoothhd.com/ondemand/mix2/mix2.ism/Manifest
http://video3.smoothhd.com/ondemand/Big%20Buck%20Bunny%20Adaptive.ism/Manifest
http://video3.smoothhd.com/ondemand/ElephantsDream.ism/Manifest
http://video3.smoothhd.com/ondemand/Got_Imagination_(Indo).ism/Manifest
http://video3.smoothhd.com/ondemand/Got_Imagination_(California).ism/Manifest
http://video3.smoothhd.com/ondemand/Coral_Reefs.ism/Manifest
http://video3.smoothhd.com/ondemand/eHow_Wakeboard.ism/Manifest
http://video3.smoothhd.com/ondemand/eHow_Baseball.ism/Manifest
http://video3.smoothhd.com/ondemand/eHow_Alligator.ism/Manifest
http://video3.smoothhd.com/ondemand/NBA.ism/Manifest
http://video3.smoothhd.com/ondemand/Changeling.ism/Manifest
http://video3.smoothhd.com/ondemand/Livestrong_ThyroidCancer.ism/Manifest
http://video3.smoothhd.com/ondemand/Livestrong_BeginnerGuideExercising.ism/Manifest
http://video3.smoothhd.com/ondemand/Livestrong_Autism.ism/Manifest
Also found some (probably) Bollywood movie:
http://az280594.vo.msecnd.net/athadu/athadu480.ism/Manifest
You can use the following search query to search for “.ism/Manifest”:
http://www.google.com/?q=%22http%22+%22.ism%2FManifest%22&oq=%22http%22+%22.ism%2FManifest%22
A good source found is http://www.smoothhd.com/content/smoothhd/smoothhd.xml
which seems to be from http://wwwns.akamai.com/hdnetwork/demo/silverlight/default.html
Some more URLs (use the unencrypted ones that write CLEAR) are found at: