Wednesday, August 5, 2009

Puppy Not Eating All Of A Sudden

Management MP3 with C # (something else)

In the previous post, obviously an important thing and then consider that the turnaround in this second delivery. As mentioned earlier ID3V2 structure allows me to embed images in the frame of audio file information. In these short examples with code shows how to save an image (. Jpg files) and then load, ie read the ID3 information and upload the image in a PictureBox control. Always use the TagLib # library.

The first example is a trivial console application in which we can see how to save an image to be the cover image of the disk (frontcover).


  
using System ;
using System . Collections . Generic ;
using System . Linq ;
using System . Text ;
using System . IO ;
using TagLib ;

namespace TagLibSharpExample_Console
{
class Program
{
static void Main ( string [] args )

{try {

/ / For simplicity in this example the audio files was placed in the path of the executable
/ / I open the audio file
TagLib . File mp3file = TagLib. File . Create ( "01-Catwash - groovabilisme-siberia.mp3" )
/ / Process some data Tag
Console. WriteLine ( "Procesing File \\ n" )
console. WriteLine ( File Name: {0} " , Mp3file . Name )
console. WriteLine ( "MIME Type: {0}" , Mp3file . Mimetype )
console. WriteLine ( "Audio bitrate: {0} [Kbps] , Mp3file . Properties . AudioBitrate . ToString ());
Console . WriteLine ( "Chanels: {0}" , Mp3file . Properties . AudioChannels . ToString ());
Console . WriteLine ( "Sample Rate: {0} [Hz]" , Mp3file . Properties . AudioSampleRate . ToString ());
Console . WriteLine ( "Procesing ID3v2 Tag" );
Console . WriteLine ( "Album: {0}" , Mp3file . Tag . Album );
Console . WriteLine ( "Title: {0}" , Mp3file . Tag . Title );
Console . WriteLine ( "Artist: " + Mp3file . Tag . FirstPerformer );
Console . WriteLine ( "Genre: " + Mp3file . Tag . FirstGenre );
Console . WriteLine ( "\\ nImagenes Embedded" )
/ / Process the images embedded in the ID3, can be several
foreach ( IPicture picture in mp3file . Tag . Pictures ) {

Console. WriteLine ( picture. Type + ":" + picture. Description );
Console. WriteLine ( "Mime Type:" + picture . MimeType);
/ En Bytes
Console. WriteLine ( "Size:" + picture . Date . Count + [Bytes] );}


/ / Creo un object Picture of TagLib and I assign the image you want.
/ / As the audio image file in the path of the executable.
TagLib. Picture myCover = new Picture ( "cover.jpg" )
/ / Put the Picture in TagLib
FrameCorrespondiente . ID3v2. AttachedPictureFrame coverAlbumArt = new TagLib . ID3v2. AttachedPictureFrame ( myCover )
/ / Sets the MimeType
coverAlbumArt . MimeType = System. Net . Mime . MediaTypeNames . Image . Jpeg ;
/ / Sets the type of image I'm putting
coverAlbumArt . Type = TagLib. PictureType . frontcover ;
/ / Add the frame to the collection of images that corresponds
TagLib. IPicture [] pictFrame = { coverAlbumArt };
/ / Dump the collection of images to Tag
mp3file . Tag . Pictures = pictFrame ;
/ / Finally save the changes
mp3file . Save ();
}
catch ( Exception ex )
{
Console . WriteLine ( ex . Message );
}
finally
{

//Wait for user entry
Console . ReadLine ();
}
}
}}


Now for the next example we use a simple Winform Application to display the image just upload the mp3 file.

  
using System ;
using System . ComponentModel ;
using System . Data ;
using System . Drawing ;
using System . Windows . Forms ;
using TagLib ;
using System . IO ;

namespace TagLibSharpExample_WinApp
{
public partial class Form1 : Form
{
public Form1 ()
{
InitializeComponent ();
this . Text = "TagLib # Example" ;}


private void Form1_Load ( object sender, EventArgs e )
{MemoryStream memStream
= null ;
try

{/ / For simplicity in this example the audio files was placed in the path of the executable
/ / I open the audio file
TagLib . File mp3file = TagLib. File . Create ( "01-Catwash - groovabilisme-siberia.mp3" )
/ / Process some data Tag
label1. Text = "File name" + mp3file . Name ;
listBox1 . Items . Add ( "Album:" + Mp3file . Tag . Album );
listBox1 . Items . Add ( "interpreting" + Mp3file . Tag . FirstPerformer );
listBox1 . Items . Add ( "Title:" + Mp3file . Tag . Title );
listBox1 . Items . Add ( "Track nÂș: " + Mp3file . Tag . Track );
listBox1 . Items . Add ( "Bit Rate: " + Mp3file . Properties . AudioBitrate + "[Kbps]" );
listBox1 . Items . Add ( "Sample Rate: " + Mp3file . Properties . AudioSampleRate + "[Hz]" );
listBox1 . Items . Add ( "Duration: " + Mp3file . Properties . Duration . TotalSeconds . ToString () + "[s]" );
foreach ( IPicture picture in Mp3file . Tag . Pictures )
listBox1 . Items . Add ( picture . Type . ToString () + ": "
+ picture . Description + "("
+ picture. Data . Count . ToString () + "Bytes)" )

/ / Position the image in a MemoryStream
/ / This way I can pass the stream created as an argument to Image.FromStream
/ / and so load the Image property of pictureBox1
memStream = new MemoryStream ( mp3file . Tag . Pictures [ 0 ]. Data . Data );
pictureBox1 . Image = Image . FromStream ( memStream );
}
catch ( Exception ex )
{
label1 . Text = ex . Message ;

} finally {

if ( memStream ! = Null )
memStream . Close ();

}}}


Here we see the result:



advise who have to deal with this type of file , take time to study and understand the code API source TagLib # because it is helpful to understand how they work to make things very interesting in applications that handle ID3 Tags.

I hope this information will serve and Happy Programming!

Greetings!

0 comments:

Post a Comment