안드로이드 앱(Kotlin|Java)/[2025~] 안드로이드 함수
mp3 파일의 앨범 이미지 가져오기
heylo
2025. 2. 21. 17:42
250227 이미지 로드 시간 단축
https://bluefootedbooby.tistory.com/187
음악 재생 앱을 만드는 중,
앨범 이미지를 어떻게 로드할 지 고민하게 되었습니다.
MediaStore.Albums 에는 ALBUM_ART 라고,
안드로이드의 MediaStore 데이터베이스에 저장된 앨범의
커버 이미지(아트워크)를 가리키는 필드 - 가 있습니다.
위와 같이 이미지를 가져오려고 했으나,
아래와 같이 deprecated 로, 권장하지 않는 방식임을
컴파일 단계에서 알게 되었습니다.
그럼 MediaStore.Audio 에는 앨범정보가 있을까요?
아쉽게도 앨범이름, 앨범 아이디, 앨범 아티스트 이름, 앨범 키 밖에 없네요.
이런 상황에서는 mp3 파일의 메타데이터를 비트맵으로 변환해주는
오픈소스 라이브러리를 사용할 수 있습니다.
저는 wseeman 씨의 라이브러리를 사용해보았습니다.
https://github.com/wseemann/FFmpegMediaMetadataRetriever/commits?author=wseemann
GitHub - wseemann/FFmpegMediaMetadataRetriever: FFmpegMediaMetadataRetriever provides a unified interface for retrieving frame a
FFmpegMediaMetadataRetriever provides a unified interface for retrieving frame and meta data from an input media file. - wseemann/FFmpegMediaMetadataRetriever
github.com
먼저,
아래와 같이 FFMPEG 미디어 메타 데이터 리트리버 라이브러리를
앱 수준 그래들 파일에서 implement 해줍니다.
implementation (libs.ffmpegmediametadataretriever)
아래는 Activity 에서 mp3 파일 경로로 앨범 이미지를 추출해
이미지를 비트맵으로 반환 하는 함수입니다.
// ■ 앨범 이미지 추출 (파일 경로로 이미지 추출, ffmpeg - 다익스트라)
private fun getAlbumArt(filePath: String): Bitmap? {
val mmr = FFmpegMediaMetadataRetriever()
try {
mmr.setDataSource(filePath)
val artBytes = mmr.embeddedPicture
if (artBytes != null) {
return BitmapFactory.decodeByteArray(artBytes, 0, artBytes.size)
}
} catch (ex: Exception) {
ex.printStackTrace()
} finally {
mmr.release()
}
return null
}
이 함수의 호출은 아래와 같이 했습니다.
path는 mp3파일의 경로입니다.
musicList 에서 음악 데이터를 담고 있는데요.
이에 MusicFile 데이터 클래스 객체를 담아주었습니다.
val albumArt = getAlbumArt(path)
musicList.add(MusicFile(index, title, artist, album, albumArt, path, duration))
UI 업데이트는 아래와 같이 했습니다.
binding.ivAlbum.setImageBitmap(musicFile.albumArt)