0

I want to save songs in SqlLite database in my app. And want to listen offline music. My app is online play music and playing music online from links Please guide. Thanks in Advance

  • 1
    I would suggest rather saving songs in DB, save them in storage and in SQLite update their reference path – Rahul Nov 28 '16 at 07:38
  • can you guide me how to do it? – Razia Rani Nov 28 '16 at 07:41
  • If you are happy to use javascript, you could use a library like pouchdb https://pouchdb.com to provide a higher level api for working with metadata (docs) and binary objects (attachments). – Chris Snow Nov 28 '16 at 07:42
  • @RaziaRani Firstly save music file in storage, Link(http://stackoverflow.com/questions/13161470/saving-file-in-internal-storage-android). Then put reference of that file in your sqlite – Rahul Nov 28 '16 at 07:48

1 Answers1

0

Currently I am working on a media player app, and my advice is to save the songs encrypted in the app folder. And than to save the path to the song in the SQL.

I am doing the same thing, but rather than saving in SQL, I am serializing the ArrayList of objects with GSON and save them in en encrypted Shared Preferences. The object has just the essentials (Title, Artist ...) and the path to the saved song.

Sample code:

public void saveReferenceToSavedSongs(String json) {
    try {
        editor.putString("songs_key", json);
        editor.commit();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public String getReferenceToSavedSongs() {
    try {
        return sharedPreferences.getString("songs_key", "");
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "";
}

How to use: (Don't forget to encrypt the json)

// to save them
ArrayList<Song> temp = new ArrayList<Song>();
// add songs
saveReferenceToSavedSongs(new Gson().toJson(temp));



// to load them
ArrayList<Song> songs = load(getReferenceToSavedSongs());


public ArrayList<Song> load(String json) {
    if (json != null && !json.equals("")) {

        Gson gson = new Gson();
        Type typeToken = new TypeToken<ArrayList<Song>>() {
        }.getType();
        savedSongs = gson.fromJson(json, typeToken);
    }
    return savedSongs;
}