Taglib adding a tag that doesnt exist

[亡魂溺海] 提交于 2019-12-25 07:55:22

问题


I'm adding a frame that doesnt exist to some of my mp3. Example the rating "POPM" is missing. The problem I have is when I add the frame I cant set the ratings value. I have two save() there that I used one at a time just to see if either one worked. But if I re-run this function and isPOPMExist is now valid I can set the rating. So i'm not sure what step is missing after i set the POPM when isPOPMExist fails

bool isPOPMExist = id3v2Tag->frameListMap().contains("POPM");
if(!isPOPMExist)
{
    TagLib::ByteVector bytePOPM("POPM");

    TagLib::ID3v2::TextIdentificationFrame *frame = new TagLib::ID3v2::TextIdentificationFrame(bytePOPM, TagLib::String::UTF8);
    id3v2Tag->addFrame(frame);
    // frame->setText("");
    file.save();
    dynamic_cast<TagLib::ID3v2::PopularimeterFrame *>(file.ID3v2Tag()->frameList("POPM").front())->setRating(255);
    file.save();
}   
else
{
   auto framelistCount = file.ID3v2Tag()->frameList("POPM").size();
   if(framelistCount > 0)
   {
       if(dynamic_cast<TagLib::ID3v2::PopularimeterFrame *>(file.ID3v2Tag()->frameList("POPM").front()) != 0)
       {
           int ratingVal = (int)rating;
           dynamic_cast<TagLib::ID3v2::PopularimeterFrame *>(file.ID3v2Tag()->frameList("POPM").front())->setRating((int)rating);

           return file.save();
       }
   }
}

回答1:


You can't dynamic_cast between ID3v2::TextIdentificationFrame and ID3v2::PopularimeterFrame. They're unrelated frame types, so the cast will always fail. You need to create the ID3v2::PopularimeterFrame directly with new, e.g.:

auto frame = new TagLib::ID3v2::PopularimeterFrame;
frame->setRating(1);
tag->addFrame(frame);
file.save();


来源:https://stackoverflow.com/questions/41477563/taglib-adding-a-tag-that-doesnt-exist

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!