Programmatically tell the media player to start playing

2 posts, 0 answers
  1. Andrew
    Andrew avatar
    34 posts
    Member since:
    Dec 2010

    Posted 24 Aug 2011 Link to this post

    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);
    }
  2. Andrew
    Andrew avatar
    34 posts
    Member since:
    Dec 2010

    Posted 26 Aug 2011 Link to this post

    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();
        }
    }
  3. DevCraft banner
Back to Top