HtmlMediaElement<TResource>
abstract class HtmlMediaElement<TResource> : HtmlElement, IHtmlMediaElement, IHtmlElement, IElement, INode, IEventTarget, IMarkupFormattable, IParentNode, IChildNode, INonDocumentTypeChildNode, IElementCssInlineStyle, IGlobalEventHandlers, IMediaController, ILoadableElement where TResource : IMediaInfo
Represents the abstract base for HTML media (audio / video) elements.
using AngleSharp.Dom.Css;
using AngleSharp.Dom.Events;
using AngleSharp.Dom.Media;
using AngleSharp.Extensions;
using AngleSharp.Html;
using AngleSharp.Network;
using AngleSharp.Network.RequestProcessors;
using AngleSharp.Services.Media;
using System;
namespace AngleSharp.Dom.Html
{
internal abstract class HtmlMediaElement<TResource> : HtmlElement, IHtmlMediaElement, IHtmlElement, IElement, INode, IEventTarget, IMarkupFormattable, IParentNode, IChildNode, INonDocumentTypeChildNode, IElementCssInlineStyle, IGlobalEventHandlers, IMediaController, ILoadableElement where TResource : class, IMediaInfo
{
private readonly MediaRequestProcessor<TResource> _request;
private ITextTrackList _texts;
public IDownload CurrentDownload => _request?.Download;
public string Source {
get {
return this.GetUrlAttribute(AttributeNames.Src);
}
set {
this.SetOwnAttribute(AttributeNames.Src, value, false);
}
}
public string CrossOrigin {
get {
return this.GetOwnAttribute(AttributeNames.CrossOrigin);
}
set {
this.SetOwnAttribute(AttributeNames.CrossOrigin, value, false);
}
}
public string Preload {
get {
return this.GetOwnAttribute(AttributeNames.Preload);
}
set {
this.SetOwnAttribute(AttributeNames.Preload, value, false);
}
}
public MediaNetworkState NetworkState {
get {
MediaRequestProcessor<TResource> request = _request;
if (request == null)
return MediaNetworkState.Empty;
return request.NetworkState;
}
}
public TResource Media {
get {
MediaRequestProcessor<TResource> request = _request;
if (request == null)
return null;
return request.Resource;
}
}
public MediaReadyState ReadyState => Controller?.ReadyState ?? MediaReadyState.Nothing;
public bool IsSeeking { get; set; }
public string CurrentSource => Source;
public double Duration => Controller?.Duration ?? 0;
public double CurrentTime {
get {
return Controller?.CurrentTime ?? 0;
}
set {
IMediaController controller = Controller;
if (controller != null)
controller.CurrentTime = value;
}
}
public bool IsAutoplay {
get {
return this.GetBoolAttribute(AttributeNames.Autoplay);
}
set {
this.SetBoolAttribute(AttributeNames.Autoplay, value);
}
}
public bool IsLoop {
get {
return this.GetBoolAttribute(AttributeNames.Loop);
}
set {
this.SetBoolAttribute(AttributeNames.Loop, value);
}
}
public bool IsShowingControls {
get {
return this.GetBoolAttribute(AttributeNames.Controls);
}
set {
this.SetBoolAttribute(AttributeNames.Controls, value);
}
}
public bool IsDefaultMuted {
get {
return this.GetBoolAttribute(AttributeNames.Muted);
}
set {
this.SetBoolAttribute(AttributeNames.Muted, value);
}
}
public bool IsPaused {
get {
if (PlaybackState == MediaControllerPlaybackState.Waiting)
return (int)ReadyState >= 2;
return false;
}
}
public bool IsEnded => PlaybackState == MediaControllerPlaybackState.Ended;
public DateTime StartDate => DateTime.Today;
public ITimeRanges BufferedTime => Controller?.BufferedTime;
public ITimeRanges SeekableTime => Controller?.SeekableTime;
public ITimeRanges PlayedTime => Controller?.PlayedTime;
public string MediaGroup {
get {
return this.GetOwnAttribute(AttributeNames.MediaGroup);
}
set {
this.SetOwnAttribute(AttributeNames.MediaGroup, value, false);
}
}
public double Volume {
get {
return Controller?.Volume ?? 1;
}
set {
IMediaController controller = Controller;
if (controller != null)
controller.Volume = value;
}
}
public bool IsMuted {
get {
return Controller?.IsMuted ?? false;
}
set {
IMediaController controller = Controller;
if (controller != null)
controller.IsMuted = value;
}
}
public IMediaController Controller {
get {
MediaRequestProcessor<TResource> request = _request;
if (request == null)
return null;
return request.Resource?.Controller;
}
}
public double DefaultPlaybackRate {
get {
return Controller?.DefaultPlaybackRate ?? 1;
}
set {
IMediaController controller = Controller;
if (controller != null)
controller.DefaultPlaybackRate = value;
}
}
public double PlaybackRate {
get {
return Controller?.PlaybackRate ?? 1;
}
set {
IMediaController controller = Controller;
if (controller != null)
controller.PlaybackRate = value;
}
}
public MediaControllerPlaybackState PlaybackState => Controller?.PlaybackState ?? MediaControllerPlaybackState.Waiting;
public IMediaError MediaError { get; set; }
public virtual IAudioTrackList AudioTracks => null;
public virtual IVideoTrackList VideoTracks => null;
public ITextTrackList TextTracks {
get {
return _texts;
}
protected set {
_texts = value;
}
}
static HtmlMediaElement()
{
Element.RegisterCallback<HtmlMediaElement<TResource>>(AttributeNames.Src, (Action<HtmlMediaElement<TResource>, string>)delegate(HtmlMediaElement<TResource> element, string value) {
element.UpdateSource(value);
});
}
public HtmlMediaElement(Document owner, string name, string prefix)
: base(owner, name, prefix, NodeFlags.None)
{
_request = MediaRequestProcessor<TResource>.Create(this);
}
public void Load()
{
string currentSource = CurrentSource;
UpdateSource(currentSource);
}
public void Play()
{
Controller?.Play();
}
public void Pause()
{
Controller?.Pause();
}
public string CanPlayType(string type)
{
Document owner = base.Owner;
if (((owner != null) ? AngleSharp.Extensions.ConfigurationExtensions.GetResourceService<TResource>(owner.Options, type) : null) == null)
return string.Empty;
return "maybe";
}
public ITextTrack AddTextTrack(string kind, string label = null, string language = null)
{
return null;
}
internal override void SetupElement()
{
base.SetupElement();
string ownAttribute = this.GetOwnAttribute(AttributeNames.Src);
if (ownAttribute != null)
UpdateSource(ownAttribute);
}
private void UpdateSource(string value)
{
Url url = new Url(value);
this.Process(_request, url);
}
}
}