Added base classes to encapsulate metadata in media

This commit is contained in:
watsonb8
2019-05-24 15:59:26 -04:00
parent 93be6dc100
commit 80e9a4543d
12 changed files with 222 additions and 97 deletions

View File

@ -0,0 +1,35 @@
using System;
namespace Aurora.Backend.Models.Media
{
public class AudioMetadata : BaseMetadata
{
public void AutioMetadata()
{
}
/// <summary>
/// The title of the song.
/// </summary>
/// <value></value>
public string Title { get; set; }
/// <summary>
/// The artist of the song.
/// </summary>
/// <value></value>
public string Artist { get; set; }
/// <summary>
/// The album from which the song belongs.
/// </summary>
/// <value></value>
public string Album { get; set; }
/// <summary>
/// The duration of the song.
/// </summary>
/// <value></value>
public string Duration { get; set; }
}
}

View File

@ -0,0 +1,54 @@
using System;
using System.IO;
namespace Aurora.Backend.Models.Media
{
public abstract class BaseMedia
{
private bool _loaded;
private Stream _stream;
public BaseMedia()
{
_loaded = false;
Id = Guid.NewGuid().ToString();
}
#region Properties
public string Id { get; private set; }
#endregion Properties
public virtual void Load()
{
_loaded = true;
}
public virtual void Unload()
{
_loaded = false;
}
public abstract MediaTypeEnum MediaType { get; }
/// <summary>
/// Gets or sets the data stream that holds the song.
/// </summary>
/// <value>The data stream.</value>
public Stream DataStream
{
get
{
return _stream;
}
protected set
{
if (value != _stream)
{
_stream = value;
}
}
}
}
}

View File

@ -0,0 +1,17 @@
using System;
namespace Aurora.Backend.Models.Media
{
public class BaseMetadata
{
public BaseMetadata()
{
}
/// <summary>
/// Extra data associated with a song.
/// </summary>
/// <value></value>
public object ExtraData { get; set; }
}
}

View File

@ -0,0 +1,66 @@
using System;
using System.IO;
namespace Aurora.Backend.Models.Media
{
public class LocalAudio : BaseMedia
{
public LocalAudio(FileInfo fileInfo)
{
File = fileInfo;
LoadMetadata();
}
#region Properties
public FileInfo File { get; private set; }
public AudioMetadata Metadata { get; private set; }
public override MediaTypeEnum MediaType
{
get { return MediaTypeEnum.Audio; }
}
#endregion Properties
/// <summary>
/// Override load method.
/// </summary>
public override void Load()
{
if (this.DataStream != null)
{
DataStream.Close();
DataStream = null;
}
this.DataStream = System.IO.File.OpenRead(File.FullName);
base.Load();
}
/// <summary>
/// Override unload method
/// </summary>
public override void Unload()
{
if (this.DataStream != null)
{
DataStream.Close();
DataStream = null;
}
base.Unload();
}
private void LoadMetadata()
{
TagLib.File tagFile = TagLib.File.Create(File.FullName);
Metadata = new AudioMetadata()
{
Title = tagFile.Tag.Title,
Album = tagFile.Tag.Album,
Artist = tagFile.Tag.FirstAlbumArtist,
ExtraData = tagFile.Tag
};
}
}
}

View File

@ -0,0 +1,9 @@
using System;
namespace Aurora.Backend.Models.Media
{
public enum MediaTypeEnum
{
Audio,
Video,
}
}