Write MP3 tags

I am writing an audio player that will have an mp3 editor included and the read mp3 tags works great, how ever I’m having trouble with the write side of things. Here is my write code which populates a set of textfields with name, year artist etc

dim f as FolderItem
dim readMP3 as BinaryStream
dim mb1 as MemoryBlock
dim output, tag, song, artist, album, year, comment,track,s as String
dim genre As integer

'build the memory block by getting text from text fields

mb1 = NewMemoryBlock( 128 )
If mb1 <> Nil Then
mb1.StringValue( 0, 3) = “TAG”
mb1.StringValue( 3, 30 ) = SongTF.text
mb1.StringValue( 33, 30 ) = ArtistTF.text
mb1.StringValue( 63, 30 ) = AlbumTF.text
mb1.StringValue( 93, 4 ) = YearTF.text
mb1.StringValue( 97, 28 ) = CommentTF.text
'mb1.StringValue( 126, 1 ) = ascB(TrackTF.text)
'mb1.StringValue( 127, 1 ) = val(GenreTF.text)
Else
Raise New OutOfMemoryException
End If

'this bit checks that the memory block has been created
s=mb1
TextField2.text=s

'open file - textfield3 contains the file path of the mp3 file
f = GetFolderItem(Textfield3.text)

if f <> nil then
readMP3 = BinaryStream.Open(f,false)
readMP3.Position = (readMP3.Length - 128)

readMP3.Write( mb1 )

readMP3.Close
end if

When I call the write routine I get this error:

An exception of class IOException was not handled. The application must shut down.
Exception Error Number 2

readMP3 = BinaryStream.Open(f,false)

I think (false) open readMP3 read only, try

readMP3 = BinaryStream.Open(f,true)

I hope your code was just an example or snippet, as there is alot more required to support the ID3v2 metadata format in an MP3 file

Thanks Axel, you were right, I had to change that line, I also had to set the path of the FolderItem to Native. And Dave, yes this is just a snippet. I’ve just started the project but I only want to simply read and write id3 tags. Here’s my working code. Thanks guys.

dim f as FolderItem
dim readMP3 as BinaryStream
dim mb1 as MemoryBlock

'open file
f = GetFolderItem(Textfield3.text, FolderItem.PathTypeNative)
TextField3.text=f.NativePath
if f <> nil then

readMP3 = BinaryStream.Open(f,true)
readMP3.Position = (readMP3.Length - 128)

mb1 =  readMP3.Read(readMP3.Length)

'build the memory block by getting text from text fields

mb1 = NewMemoryBlock( 128 )
If mb1 <> Nil Then
  mb1.StringValue( 0, 3) = "TAG"
  mb1.StringValue( 3, 30 ) = SongTF.text
  mb1.StringValue( 33, 30 ) = ArtistTF.text
  mb1.StringValue( 63, 30 ) = AlbumTF.text
  mb1.StringValue( 93, 4 ) = YearTF.text
  mb1.StringValue( 97, 28 ) = CommentTF.text
  mb1.StringValue( 126, 1 ) = ChrB(val(TrackTF.text))
  mb1.StringValue( 127, 1 ) = ChrB(ComboGenre.ListIndex)
Else
  Raise New OutOfMemoryException
End If

readMP3.Write(mb1)

end if