Question

I am trying develop a silverlight player based on SmoothStreamingMediaElement. For Ref: SSME:SmoothStreamingMediaElement Grid.Row="2" x:Name="medSmooth" AutoPlay="True" MinWidth="320" MinHeight="240""

Now the Source Smooth Streams are encoded using H.264 video codec and AAC as Audio codec. I found at below URL, that audiostreamindex and audiostreamcount properties are for WMV type only and that killed my only left hope. http://msdn.microsoft.com/en-us/library/microsoft.web.media.smoothstreaming.smoothstreamingmediaelement_properties(v=vs.90).aspx

Can any body help me on How I can detect the currently playing language in video, and then I want to put an event handler or "Users action of changing language", once that event i s fired, I want to change the currently playing audio track to the selected one.

Was it helpful?

Solution

I suggest to use Silverlight Media Framework, it really simplifies development of video applications. You can download its source code here: http://smf.codeplex.com/downloads/get/386528.

However, you can do some things without framework

  • How I can detect the currently playing language in video

Here is the code:

var currentSegment = mediaElement.ManifestInfo.Segments[mediaElement.CurrentSegmentIndex.Value];
var currentAudioStream = currentSegment.SelectedStreams.Where(i => i.Type == MediaStreamType.Audio).FirstOrDefault()
  • I want to change the currently playing audio track to the selected one

Something like this:

foreach (var segment in mediaElement.ManifestInfo.Segments)
{
    var newStreams = new List<StreamInfo>();
    // use current video streams
    var selectedVideoStreams = segment.SelectedStreams.Where(i => i.Type != MediaStreamType.Audio).ToList();
    newStreams.AddRange(selectedVideoStreams);
    // add a new audio stream
    newStreams.Add(newAudioStream);
    // replace old streams by new ones
    segment.SelectStreamsAsync(newStreams);
}

OTHER TIPS

If you are using SMF, here is the easiest solution:

private CustomPlayer SetAudioStreamLanguage(string languageCode)
{
    const string languageAttributeKey = "Language";

    if (AvailableAudioStreams.Count() < 2) return this;

    var languageCode = new CultureInfo(languageCode).ThreeLetterISOLanguageName();
    if (languageCode == null)
    {
        throw new Exception(string.Format("Audio stream language code {0} cannot be converted to three-letter ISO language code.", languageCode));
    }

    StreamMetadata newAudioStream =
        AvailableAudioStreams.FirstOrDefault(
            s =>
                s.Attributes.ContainsKey(languageAttributeKey) &&
                s.Attributes[languageAttributeKey].Equals(languageCode, StringComparison.InvariantCultureIgnoreCase));

    if (newAudioStream == null) return this;

    SelectedAudioStream = newAudioStream;
    return this;
}

This method should be called after MediaOpened event has been fired.

NOTE: CultureInfo.ThreeLetterISOLanguageName() method does not exist in Silverlight. You can find it's sample implementation in this answer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top