This is a migrated thread and some comments may be shown as answers.

Programmatically tell the media player to start playing

1 Answer 59 Views
MediaPlayer
This is a migrated thread and some comments may be shown as answers.
Andrew
Top achievements
Rank 1
Iron
Iron
Andrew asked on 25 Aug 2011, 01:52 AM
I have a web page which contains a silverlight control that hosts a RadMediaPlayer.
Javascript on the web page can tell the RadMediaPlayer to start playing a particular file.

This is almost working, except it won't start playing automatically. The user still has to press the start button.

Why doesn't RadMediaPlayer.Play() start it?

Edit: The first time my function PlayMediaFile is called, it does not start by itself. The second time my function is called, it DOES start (even if the user had not pressed the start button the first time, and even if it's a different media file then the first time).


Here is the code:

[ScriptableMember(ScriptAlias="playMediaFile")]
public void PlayMediaFile(long entityId, string fileName)
{
    try
    {
        _tbDebugOutput.Text = string.Format("PlayMediaFile({0}, {1}", entityId, fileName);
        SetPlaylist(entityId, fileName);
         this._radMediaPlayer.Play(); // This should start it playing! But it doesn't.
    }
    catch (Exception ex)
    {
        this._tbDebugOutput.Text = ex.ToString();
    }
}
 
public void SetPlaylist(long entityId, string fileName)
{
    var path = GenerateMediaFilePath(entityId, fileName);
    RadMediaItem mediaItem = new RadMediaItem();
    mediaItem.Source = new Uri(path, UriKind.RelativeOrAbsolute);
    this._radMediaPlayer.Items.Clear();
    this._radMediaPlayer.Items.Add(mediaItem);
}

1 Answer, 1 is accepted

Sort by
0
Andrew
Top achievements
Rank 1
Iron
Iron
answered on 26 Aug 2011, 06:21 AM
I replaced the direct call to "Play" to a posted dispatcher call. Now it works.


[ScriptableMember(ScriptAlias="playMediaFile")]
public void PlayMediaFile(long entityId, string fileName)
{
    try
    {
        this._radMediaPlayer.AutoPlay = true;
        SetPlaylist(entityId, fileName);
        this.Dispatcher.BeginInvoke(new Action(() => this._radMediaPlayer.Play()));
    }
    catch (Exception ex)
    {
        this._tbDebugOutput.Text = ex.ToString();
    }
}
Tags
MediaPlayer
Asked by
Andrew
Top achievements
Rank 1
Iron
Iron
Answers by
Andrew
Top achievements
Rank 1
Iron
Iron
Share this question
or