Music tracks include header information such as artist, album, duration, etc
In SharePoint Foundation 2010, I wanted to extract this header data and use it to populate library metadata.
To do this, I first downloaded TagLib.
The clever bit about TagLib is that you can create your own SharePoint-specific implementation of the TagLib.File.IFileAbstraction interface. I then used this in a SharePoint event receiver to extract the header data when a user uploads a music file.
Note that this works for other types of music tracks, not just mp3.
The following code shows the code I created for a class that implements IFileAbstraction:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.SharePoint;
namespace Redweb.Redio.Audio
{
public class SharePointMediaFile : TagLib.File.IFileAbstraction
{
private string _fileUrl = String.Empty;
public string FileUrl
{
get
{
return _fileUrl;
}
set
{
_fileUrl = value;
}
}
void TagLib.File.IFileAbstraction.CloseStream(System.IO.Stream stream)
{
if (stream == null)
throw new ArgumentNullException("stream");
stream.Close();
}
string TagLib.File.IFileAbstraction.Name
{
get { return _fileUrl; }
}
System.IO.Stream TagLib.File.IFileAbstraction.ReadStream
{
get
{
MemoryStream outputSteam = null;
using (SPSite site = new SPSite(_fileUrl))
{
using (SPWeb web = site.OpenWeb())
{
if ((web == null) || (web.Exists == false))
{
throw new Exception("No web exists.");
}
SPFile file = web.GetFile(_fileUrl);
if ((file == null) || (file.Exists == false))
{
throw new Exception("No file exists.");
}
byte[] bytes = file.OpenBinary();
outputSteam = new MemoryStream(bytes);
}
}
return outputSteam;
}
}
System.IO.Stream TagLib.File.IFileAbstraction.WriteStream
{
get { throw new NotImplementedException(); }
}
}
}
No comments:
Post a Comment