timheuer / taglib-sharp-portable

.NET Portable Class Library implementation of TagLib# (forked from TagLib# main project)
Other
58 stars 33 forks source link

Writing tags #12

Closed ghost closed 8 years ago

ghost commented 8 years ago

Hi! I am experiencing one problem while writing tags using taglib sharp portable in my winRT app. Writing works, but it takes 20 seconds to finish and app is frezzed during that time. Is this a bug or I am doing something wrong??? I will really appreciate any help!

MrCSharp22 commented 8 years ago

is it happening with every MP3 file. What are the sizes of the files and can u put some of the code u r using to write the tags?

ghost commented 8 years ago

Thank you so much for answering on my question!

I use this lib to write tags to music files in winRT app. I first must notice that tagging totally works, it is just that it lasts for 30 seconds and literally!

Btw, it is happening to every file! It is just that the files are in .m4a format, not mp3! Sizes of those files are max 5MB...

Please, wait few seconds for code...

ghost commented 8 years ago

I cannot post the code right now, but I will do it as soon as I can!

MrCSharp22 commented 8 years ago

My app deals with M4As too along with MP3s and i dont think i have noticed this issue. I will run some tests now and get back to you.

ghost commented 8 years ago

Thank you! Maybe the potential bug is in my code? I will soon post it...

ghost commented 8 years ago

So, writing tags for you happens instantly?

MrCSharp22 commented 8 years ago

Hi, i just tested it and it fine on my side. And it doesnt happen instantly, it takes about 1~2 seconds for MP3 files which i think is normal.

ghost commented 8 years ago

Strange... I would like that delay is 1-2 seconds for me too... But for me and .m4as in my UWP app the writing process last for 20-30 seconds! In few minutes will post code... Hope you can take a closer look at it?

ghost commented 8 years ago

This is my code... Am I doing something wrong here???

                    try
                    {
                        var writeStream = await file.OpenAsync(FileAccessMode.ReadWrite);
                        var tagFile = TagLib.File.Create(new StreamFileAbstraction(file.Name, writeStream.AsStreamForRead(), writeStream.AsStreamForWrite()));
                        var tag = tagFile.Tag;
                        tag.AlbumArtists = new string[] { artistName };
                        tag.Title = fileName;
                        tag.Album = albumName;
                        tagFile.Save();
                        writeStream.Dispose();
                        tagFile.Dispose();
                    }
                    catch
                    {

                    }
MrCSharp22 commented 8 years ago

ok, if u can place breakpoints on those lines and see which one takes longer to execute.

ghost commented 8 years ago

Will try that now... But, the code is OK?

MrCSharp22 commented 8 years ago

I would re-write your code like this just to make use of the "using" keyword, so it can dispose everything automatically

try
{
    using (var writeStream = await file.OpenAsync(FileAccessMode.ReadWrite))
    {
        using (var tagFile = TagLib.File.Create(new StreamFileAbstraction(file.Name, writeStream.AsStreamForRead(), writeStream.AsStreamForWrite())))
        {
            var tag = tagFile.Tag;
            tag.AlbumArtists = new string[] { artistName };
            tag.Title = fileName;
            tag.Album = albumName;
            tagFile.Save();
        }
    }
}
catch (Exception)
{
    throw;
}
MrCSharp22 commented 8 years ago

Here is part of my code


var songStorageFile = await StorageFile.GetFileFromPathAsync(await song.GetLocalSongStorageFilePathAsync());

using (var songFileStream = await songStorageFile.OpenStreamForWriteAsync())
{
    using (var tagsFile = File.Create(new StreamFileAbstraction(songStorageFile.Name, songFileStream, songFileStream), songStorageFile.ContentType, ReadStyle.None))
    {
        tagsFile.RemoveTags(TagTypes.Id3v1 | TagTypes.Id3v2);

        var tagsV2 = tagsFile.GetTag(TagTypes.Id3v2, true); // get the ID3v2 tags and if not available let the lib create the frames 

        tagsV2.Title = song.Name; //set song name as the title
        tagsV2.Album = song.Album.Name; //set the album name
        tagsV2.AlbumArtists = new string[] { song.Album.Artist.Name }; // use the album artist name as the album artist
        tagsFile.Save(); //and then save the tags
    }
}

Note that because i am just writing tags and not reading them, i am opening a write stream only.

ghost commented 8 years ago

Thanks for your code! I have used the breakpoints in my code... The line tagFile.Save(); is the problem... 15-20 seconds...

MrCSharp22 commented 8 years ago

ok give my code a try and see if it will change anything

ghost commented 8 years ago

Will do...

ghost commented 8 years ago

I have tried your first code, but no improvement... I got 18 seconds on tagFile.Save(); ... Any other idea? lolol

punker76 commented 8 years ago

@IgorGiga

ghost commented 8 years ago

@punker76

  1. Cannot confirm about mp3 files, because my app Works ONLY with .m4a files.
  2. (if you mean on "tag editor kind of app") No!
MrCSharp22 commented 8 years ago

@IgorGiga could u run through your breakpoints again and show me the values of the variables in your tagFile object? i think i have an idea of whats going on.

ghost commented 8 years ago

Sure... But don't know if I understood you... lolol

MrCSharp22 commented 8 years ago

what i meant was when u r debugging the app, show me the contents of the tagFile object. You know VS can show you the runtime values of any object in scope of the breakpoint.

ghost commented 8 years ago

Will do that soon...

ghost commented 8 years ago
                    try
                    {
                        StatusTextBlock.Text = Resources.GetString("tagsString");
                        var writeStream = await file.OpenAsync(FileAccessMode.ReadWrite);
                        var tagFile = TagLib.File.Create(new StreamFileAbstraction(file.Name, writeStream.AsStreamForRead(), writeStream.AsStreamForWrite()));
                        var tag = tagFile.Tag;
                        tag.AlbumArtists = new string[] { artistName };
                        tag.Title = fileName;
                        tag.Album = albumName;
                        tagFile.Save();
                        tagFile.Dispose();
                        writeStream.Dispose();
                    }
                    catch
                    {

                    }

snimak ekrana 49

MrCSharp22 commented 8 years ago

i dont see any problem there, best thing to do now, is place breakpoints in the save() method and see which line is causing the issue. I cant reproduce this so i cant try this way.

ghost commented 8 years ago

But how would I know which line is causing the issue?

MrCSharp22 commented 8 years ago

@IgorGiga sorry, what i meant was which line is taking 20 seconds to execute.

ghost commented 8 years ago

Ok! Will do that! I will get back to you once I have something new to share... If you came across any idea in the future on how to solve this problem, please feel free to let me know!