Search Unity

Audio Getting the image of an audio file

Discussion in 'Audio & Video' started by WazupGames, Oct 6, 2019.

  1. WazupGames

    WazupGames

    Joined:
    Sep 7, 2019
    Posts:
    10
    I'm trying to get the image of an audio file like mp3 to create something similar to this (The BassCandy picture)


    I have no idea how they store the picture and song details in the mp3 file!
    I have tried using the following but it simply returns a question mark image because im guessing im not supposed to be calling this method on an audio file but rather an actual texture lol... anyway! any ideas?
    Code (CSharp):
    1. UnityWebRequestTexture.GetTexture(path);
    2.         yield return textureFileRequest.SendWebRequest();
    3.         Texture2D texture = DownloadHandlerTexture.GetContent(textureFileRequest);
     
  2. WazupGames

    WazupGames

    Joined:
    Sep 7, 2019
    Posts:
    10
    Bump! Anyone? lol
     
  3. Hikiko66

    Hikiko66

    Joined:
    May 5, 2013
    Posts:
    1,304
    In future just ask a search engine, it would have given you the answer immediately, unlike this forum. You also could have gone straight to the file format specification and looked at that. If it's in the file, then it's in the format specification.

    https://en.wikipedia.org/wiki/MP3#File_structure
    https://en.wikipedia.org/wiki/ID3
     
  4. WazupGames

    WazupGames

    Joined:
    Sep 7, 2019
    Posts:
    10
    I'm sorry but i don't really know how to GRAB that data in Unity, im still a beginner! Any built in available methods or do i have to look up how to grab such data in C#
     
  5. brunopava

    brunopava

    Joined:
    Aug 7, 2012
    Posts:
    5
    Sup @WazupGames, since Mr @Hikiko66 is not of much help.
    I did some research and came up with this:

    You should use TagLibSharp (just google "TagLibSharp dll download")
    Then put .dll in Plugins folder

    Code (CSharp):
    1.  
    2. using TagLib;
    3. using System.IO;
    4. public RawImage rawImage;
    5. private void ReadMetadata(string path)
    6. {
    7.      var tfile = TagLib.File.Create(path);
    8.      //get metadata
    9.      string title = tfile.Tag.Title;
    10.      string album = tfile.Tag.Album;
    11.      System.TimeSpan duration = tfile.Properties.Duration;
    12.      // Load you image data in MemoryStream
    13.      TagLib.IPicture pic = tfile.Tag.Pictures[0];
    14.      MemoryStream ms = new MemoryStream(pic.Data.Data);
    15.      ms.Seek(0, SeekOrigin.Begin);
    16.      //Create texture2d with MemoryStream
    17.      Texture2D tex = new Texture2D(2, 2);
    18.      tex.LoadImage(ms.ToArray());
    19.      /// assign to a RawImage
    20.      rawImage.texture = tex;
    21. }
     
    amirgame197 likes this.