How to extract album art from mp3 file with faster speed

人盡茶涼 提交于 2019-12-31 05:09:11

问题


I've basically created an application where i'm storing all mp3 data into Realm database here all data which are into string are fetched and stored really fast but when it comes to fetching and storing the images into byte array it's taking really long for my application to fetch and store the images into database which is eventually making my application lag. Below is my code which i have done please take a look.

//This method is for getting song lists where i'm facing issues.

Note: I'm facing issues specifically at try catch block here images are successfully fetched but it's speed is very slow.

fun getSongList()
    {
        //query external audio
        val musicResolver = context!!.getContentResolver()
        val musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
        val musicCursor = musicResolver.query(musicUri, null, null, null, null)
        //iterate over results if valid
        if (musicCursor != null && musicCursor.moveToFirst())
        {
            //get columns
            val titleColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media.TITLE)
            val idColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media._ID)
            val artistColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media.ARTIST)
            val url= musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media.DATA)
            //add songs to list
            do
            {
                val thisId = musicCursor.getLong(idColumn)
                val thisTitle = musicCursor.getString(titleColumn)
                val thisArtist = musicCursor.getString(artistColumn)
                val thisurls = musicCursor.getString(url)
                songList!!.add(Song(thisId, thisTitle, thisArtist,thisurls))
                var byteArray:ByteArray?=null
                try
                {
                    val retriever = MediaMetadataRetriever()
                    retriever.setDataSource(thisurls)
                    val art = retriever.getEmbeddedPicture()
                    if (art != null) {
                        var bitmap=BitmapFactory.decodeByteArray(art, 0, art.size)
                        var stream =  ByteArrayOutputStream()
                        bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream)
                         byteArray = stream.toByteArray()
                    }
                    else
                    {
                        val icon:Bitmap = BitmapFactory.decodeResource(context!!.getResources(), R.drawable.music)
                        var stream =  ByteArrayOutputStream()
                        icon.compress(Bitmap.CompressFormat.PNG, 100, stream);
                         byteArray = stream.toByteArray()
                    }
                }catch (e: Exception){
                    e.printStackTrace()
                }
                realm!!.beginTransaction()
                var songitem = realm!!.createObject(Songdetails::class.java,Companion.getPrimaryKey())
                songitem.songname = thisTitle
                songitem.songartist = thisArtist
                songitem.songurl = thisurls
                songitem.songimage = byteArray
                realm!!.commitTransaction()
            }
            while (musicCursor.moveToNext())
        }
    }

Any suggestions will be appreciated!

来源:https://stackoverflow.com/questions/56019341/how-to-extract-album-art-from-mp3-file-with-faster-speed

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