Achilles
Achilles

Reputation: 741

Data structure for songs in Android

Basically I am creating an android music player. I have managed to store a STRING into the "comment" section of each song in my library.

The String is of form eg. "guitar, drums, spanish, sad"

Now my concerns is to come up with a data structure to move around this mp3 file (its path) along with this STRING, in my code. What makes it more challenging is that:

the String as u saw above is made up of commas "," So every time a user wants to create a playlist, they will just give me a list of words (TAGS) and I have to look into every song to find if it has the TAG they are looking for or not. I do so by splitting the String stored in the COMMENT section of the song into sub-strings(TAGS) and match it when the users input.

I am looking for suggestion for a good data structure.

Should I split the COMMENT Section before hand and keep it in some sort of collection? or do it as I am looping through every song ? split it to check it against the inputs?

I thought of using ArrayList > but maybe there is a better way...

Upvotes: 2

Views: 1021

Answers (2)

sgp15
sgp15

Reputation: 1280

A reverse index will be more suitable if the list of songs is long. That is instead of looping through each song and checking if contains the tag you can find the tag directly and extract list of songs associated with it. Jumping to a tag directly can be done by using a Map and within an entry the value could be a List of Song(s).

Map<String, List<Song>> tagIndex

I am assuming Song is a class that abstracts a song.

If its not possible to persist the index it can be built at startup in background.

EDIT-1: (reply to first comment)

I think all Android devices have SQLite available. If this is indeed the case an easy to integrate then there are couple of ways you can store and access the tags.

  1. Store tags in database and comments.
  2. Store tags in database only.

In the first approach tags will survive application re-installs. But then comments are accessible for other applications too which may use it in other ways or edit/overwrite your values. In this approach you'll have to build the index at application install and keep the comments and database in sync on the go once the initial index is built.

Second approach is partly similar to first where tags stored in database and maintained on the go. But there is no initial build and tags may get erased if application is un-installed. This may or may not be acceptable but this approach is simpler than the first.

If SQLite is used you'll not need to maintain tags in memory at all. Everything can be maintained in database.

Table: tag_index

Columns: (tag_name, song_name, song_path)

Upvotes: 1

Ryan Fung
Ryan Fung

Reputation: 2217

Arraylist is the way to good in my opinion, since you will have a list of different TAGS i suppose.

But i would suggest not using a string as the type, perhaps you should use a list of a tag class which will give you more flexibility in the future when you need to make changes.

Hope it helps.

Upvotes: 1

Related Questions