Saturday, March 31, 2012

TrimEnd of strings help needed!

Ok, I'm a flash developer, comfortable with actionscripting... so keep this in mind and thanks for any help.

I got some code from a german site that pulled the ID tags from a directory of mp3s. This is for a client who wants to populate a flash mp3 player with his own music. Anyways, I got the flash part, which will pull the data from an XML file. What I want is for the client to populate the ID tags of the mp3s, the .NET page reads that and creates an xml file out of it. So here's the code:


<%@dotnet.itags.org. Page Language="C#" Debug="True" EnableViewStateMac="false" %>
<%@dotnet.itags.org. Import Namespace="System" %>
<%@dotnet.itags.org. Import Namespace="System.IO" %>
<%@dotnet.itags.org. Import Namespace="System.Text.RegularExpressions" %
<script language="C#" runat="server"
private static String ConvertByteToString(byte[] bytes, int pos1, int pos2)
{
//pos2 muß größer oder gleich pos1 sein und
//pos2 darf Länge des Arrays nicht überschreiten
if ((pos1 > pos2) || (pos2 > bytes.Length - 1))
{
throw new ArgumentException("Aruments out of range");
}

//Länge des zu betrachtenden Ausschnittes
int length = pos2 - pos1 + 1;

//neues Char-Array anlegen der Länge length
Char[] chars = new Char[length];

//packe alle Bytes von pos1 bis pos2 als
//Char konvertiert in Array chars
int started = 0;
for (int i = 0; i < length; i++)
{
chars[i] = Convert.ToChar(bytes[i + pos1]);

}//end for

//konvertiere Char-Array in String und gebe es zurück
return new String(chars);
}

void Page_Load(object s, EventArgs e)
{
DirectoryInfo di = new DirectoryInfo("C:/mp3directory");
FileInfo[] rgFiles = di.GetFiles("*.mp3");
String xmlcontent = "<?xml version='1.0' standalone='yes'?><songs>";
foreach(FileInfo fi in rgFiles)
{

//mp3 parser section
Stream s1 = fi.OpenRead();

byte[] bytes = new byte[128];
s1.Seek(-128, SeekOrigin.End);
int numBytesToRead = 128;
int numBytesRead = 0;
while (numBytesToRead > 0) {
int n = s1.Read(bytes, numBytesRead, numBytesToRead);

if (n==0) {
break;
}
numBytesRead += n;
numBytesToRead -= n;
}
s1.Close();

String tag = ConvertByteToString(bytes, 0, 2);
//if (tag != "TAG") {
//return false;
//}

String m_title = ConvertByteToString(bytes, 3, 32);
m_title = m_title.TrimEnd();
String m_artist = ConvertByteToString(bytes, 33, 62);
String m_comment = ConvertByteToString(bytes, 97, 126);
//m_album = ConvertByteToString(bytes, 63, 92);
//m_year = Int32.Parse(ConvertByteToString(bytes, 93, 96));

//m_genre = bytes[127];

//end mp3 parser section

xmlcontent = xmlcontent + "<song title='"+m_title+"' artist='"+m_artist+"' path='" + fi.Name + "' />";
}
xmlcontent = xmlcontent + "</songs>";
//xmlcontent = Regex.Replace(xmlcontent, " ", "*");
FileInfo t = new FileInfo("C:/mydirectory/songInfo.xml");
StreamWriter Tex =t.CreateText();
Tex.WriteLine(xmlcontent);
Tex.Close();
Response.Write("Radio Listings created.");
Response.Write(xmlcontent);
}

</script>

Problem is, the silly thing is creating a string with what looks like TONS of extra spaces at the end. What I guess it's doing is creating a string 32-3 characters long for the title (as an example). I tried using TrimEnd, but that didn't work. I even tried just as a test to replace all the spaces with asterisks (you can see it commented above), but even that didn't work.

It looks something like this: <song title='Hey Ya! ' artist='Outkast ' path='HeyYa.mp3' /
Very frustrating.

Thanks!Not exactly sure I was clear about what the problem is. If you look above at the xml that gets generated, you'll notice after the title there is a space. In the xml file that is created, there are a ton of spaces, but my pasting in the example strips out the spaces.

Anyways, I _think_ I figured it out. I removed the Streamwriter part and replaced it with XmlTextWriter. This changed the spaces to "�", and flash just ignores that. Everything works exactly as it should. I'm assuming I have a encoding issue with transferring bytes to strings and then to xml. Eh... it works, so who cares I suppose.

<%@. Page Language="C#" Debug="True" EnableViewStateMac="false" %>
<%@. Import Namespace="System" %>
<%@. Import Namespace="System.IO" %>
<%@. Import Namespace="System.Xml" %>
<%@. Import Namespace="System.Text" %
<script language="C#" runat="server"
private static String ConvertByteToString(byte[] bytes, int pos1, int pos2)
{
//pos2 muß größer oder gleich pos1 sein und
//pos2 darf Länge des Arrays nicht überschreiten
if ((pos1 > pos2) || (pos2 > bytes.Length - 1))
{
throw new ArgumentException("Aruments out of range");
}

//Länge des zu betrachtenden Ausschnittes
int length = pos2 - pos1 + 1;

//neues Char-Array anlegen der Länge length
Char[] chars = new Char[length];

//packe alle Bytes von pos1 bis pos2 als
//Char konvertiert in Array chars
int started = 0;
for (int i = 0; i < length; i++)
{
chars[i] = Convert.ToChar(bytes[i + pos1]);

}//end for

//konvertiere Char-Array in String und gebe es zurück
return new String(chars);
}

void Page_Load(object s, EventArgs e)
{
DirectoryInfo di = new DirectoryInfo("C:/mp3directory");
FileInfo[] rgFiles = di.GetFiles("*.mp3");
XmlTextWriter writer = new XmlTextWriter("C:/xmldirectory/songInfo.xml", Encoding.UTF8);

// start writing!
writer.WriteStartDocument();
writer.WriteStartElement("songs");

foreach(FileInfo fi in rgFiles)
{

//mp3 parser section
Stream s1 = fi.OpenRead();

byte[] bytes = new byte[128];
s1.Seek(-128, SeekOrigin.End);
int numBytesToRead = 128;
int numBytesRead = 0;
while (numBytesToRead > 0) {
int n = s1.Read(bytes, numBytesRead, numBytesToRead);

if (n==0) {
break;
}
numBytesRead += n;
numBytesToRead -= n;
}
s1.Close();

String tag = ConvertByteToString(bytes, 0, 2);
//if (tag != "TAG") {
//return false;
//}

String m_title = ConvertByteToString(bytes, 3, 32);
String m_artist = ConvertByteToString(bytes, 33, 62);
String m_comment = ConvertByteToString(bytes, 97, 126);
//m_album = ConvertByteToString(bytes, 63, 92);
//m_year = Int32.Parse(ConvertByteToString(bytes, 93, 96));

//m_genre = bytes[127];

//end mp3 parser section

writer.WriteStartElement("song");
writer.WriteAttributeString("title", m_title);
writer.WriteAttributeString("artist", m_artist);
writer.WriteAttributeString("path", fi.Name);
writer.WriteEndElement();
}
writer.WriteEndElement();
writer.WriteEndDocument();
writer.ToString();
writer.Close();
Response.Write("Radio Listings created.");

}

</script

0 comments:

Post a Comment