How to get and set (change) ID3 tag (metadata) of audio files?

后端 未结 4 895
轻奢々
轻奢々 2020-12-04 13:57

I am working to change ID3 tags, the metadata in audio files, such as:

  • Artist
  • Title
  • Album
  • etc.

And the core point,

相关标签:
4条回答
  • 2020-12-04 14:05

    And most important point is that the edited ID3 tags were shown only into the my app.

    If you edit the file then anybody will see that. You can create your own database of mediafiles (like Android's database) and just store the modified data there.

    0 讨论(0)
  • 2020-12-04 14:15

    I have created a sample app, based on the above answers, you can download the sample implementation from here https://github.com/mickyarun/AndroidSongMetaDataUpdate.git

    0 讨论(0)
  • 2020-12-04 14:20

    I think this is what you are looking for MyID3 library to set and get tags for media file.

    Download this jar file MyID3_for_android and add it to your project's build path. here is the sample code. here pathdata is the file path of the audio file.

                File src = new File(pathdata);
                MusicMetadataSet src_set = null;
                try {
                    src_set = new MyID3().read(src);
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } // read metadata
    
                if (src_set == null) // perhaps no metadata
                {
                    Log.i("NULL", "NULL");
                }
                else
                {
                    try{
                        IMusicMetadata metadata = src_set.getSimplified();
                        String artist = metadata.getArtist();  
                        String album = metadata.getAlbum();  
                        String song_title = metadata.getSongTitle(); 
                        Number track_number = metadata.getTrackNumber(); 
                        Log.i("artist", artist);
                        Log.i("album", album);
                    }catch (Exception e) {
                        e.printStackTrace();
                    }
                    File dst = new File(pathdata);
                    MusicMetadata meta = new MusicMetadata("name");
                    meta.setAlbum("Chirag");
                    meta.setArtist("CS");
                    try {
                        new MyID3().write(src, dst, src_set, meta);
                    } catch (UnsupportedEncodingException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (ID3WriteException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }  // write updated metadata
                }
    

    Happy Coding :)

    0 讨论(0)
  • 2020-12-04 14:22

    Actually, FasteKerinns's code is quite good. You should just change

    new MyID3().write(src, dst, src_set, meta);

    to

    new MyID3().update(src, src_set, meta);

    which means you don't need dst variable at all.

    Additionaly, I have this piece of code which refreshes song that is modified in MediaStore:

     scanner=new MediaScannerConnection(getApplicationContext(),  
                new MediaScannerConnectionClient() {    
    
          public void onScanCompleted(String path, Uri uri) {
                  scanner.disconnect(); 
          }
    
          public void onMediaScannerConnected() {
                  scanner.scanFile(path, "audio/*");    
          }
      });
    
      scanner.connect();
    
    0 讨论(0)
提交回复
热议问题