package com.example.blackmusic.database;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import com.example.blackmusic.entity.MusicInfo;
import com.example.blackmusic.entity.PlayListInfo;
import com.example.blackmusic.util.ChineseToEnglish;
import com.example.blackmusic.util.Constant;
import com.example.blackmusic.util.MyMusicUtil;
import java.util.ArrayList;
import java.util.List;
import static com.example.blackmusic.database.DatabaseHelper.ID_COLUMN;
import static com.example.blackmusic.database.DatabaseHelper.MUSIC_ID_COLUMN;
public class DBManager {
private static final String TAG = DBManager.class.getName();
private DatabaseHelper helper;
private SQLiteDatabase db;
private static DBManager instance = null;
/* 因为getWritableDatabase内部调用了mContext.openOrCreateDatabase(mName, 0,mFactory);
* 需要一个context参数 ,所以要确保context已初始化,我们可以把实例化DBManager的步骤放在Activity的onCreate里
*/
public DBManager(Context context) {
helper = new DatabaseHelper(context);
db = helper.getWritableDatabase();
}
public static synchronized DBManager getInstance(Context context) {
if (instance == null) {
instance = new DBManager(context);
}
return instance;
}
// 获取音乐表歌曲数量
public int getMusicCount(int table) {
int musicCount = 0;
Cursor cursor = null;
switch (table) {
case Constant.LIST_ALLMUSIC:
cursor = db.query(DatabaseHelper.MUSIC_TABLE, null, null, null, null, null, null);
break;
case Constant.LIST_LASTPLAY:
cursor = db.query(DatabaseHelper.LAST_PLAY_TABLE, null, null, null, null, null, null);
break;
case Constant.LIST_MYLOVE:
cursor = db.query(DatabaseHelper.MUSIC_TABLE, null, DatabaseHelper.LOVE_COLUMN + " = ? ", new String[]{"" + 1}, null, null, null);
break;
case Constant.LIST_MYPLAY:
cursor = db.query(DatabaseHelper.PLAY_LIST_TABLE, null, null, null, null, null, null);
break;
}
if (cursor.moveToFirst()) {
musicCount = cursor.getCount();
}
if (cursor != null) {
cursor.close();
}
return musicCount;
}
public List<MusicInfo> getAllMusicFromMusicTable() {
Log.d(TAG, "getAllMusicFromMusicTable: ");
List<MusicInfo> musicInfoList = new ArrayList<>();
Cursor cursor = null;
db.beginTransaction();
try {
cursor = db.query(DatabaseHelper.MUSIC_TABLE, null, null, null, null, null, null);
musicInfoList = cursorToMusicList(cursor);
db.setTransactionSuccessful();
} catch (Exception e) {
e.printStackTrace();
} finally {
db.endTransaction();
if (cursor!=null){
cursor.close();
}
}
return musicInfoList;
}
public MusicInfo getSingleMusicFromMusicTable(int id) {
Log.i(TAG, "getSingleMusicFromMusicTable: ");
List<MusicInfo> musicInfoList = null;
MusicInfo musicInfo = null;
Cursor cursor = null;
db.beginTransaction();
try {
cursor = db.query(DatabaseHelper.MUSIC_TABLE, null, ID_COLUMN + " = ?", new String[]{"" + id}, null, null, null);
musicInfoList = cursorToMusicList(cursor);
musicInfo = musicInfoList.get(0);
db.setTransactionSuccessful();
} catch (Exception e) {
e.printStackTrace();
} finally {
db.endTransaction();
if (cursor!=null){
cursor.close();
}
}
return musicInfo;
}
public List<MusicInfo> getAllMusicFromTable(int playList) {
Log.d(TAG, "getAllMusicFromTable: ");
List<Integer> idList = getMusicList(playList);
List<MusicInfo> musicList = new ArrayList<>();
for (int id : idList) {
musicList.add(getSingleMusicFromMusicTable(id));
}
return musicList;
}
/**
* 获取我的歌单列表
* @return
*/
public List<PlayListInfo> getMyPlayList() {
List<PlayListInfo> playListInfos = new ArrayList<>();
Cursor cursor = db.query(DatabaseHelper.PLAY_LIST_TABLE, null, null, null, null, null, null);
Cursor cursorCount = null;
while (cursor.moveToNext()) {
PlayListInfo playListInfo = new PlayListInfo();
int id = Integer.valueOf(cursor.getString(cursor.getColumnIndex(ID_COLUMN)));
playListInfo.setId(id);
playListInfo.setName(cursor.getString(cursor.getColumnIndex(DatabaseHelper.NAME_COLUMN)));
cursorCount = db.query(DatabaseHelper.PLAY_LISY_MUSIC_TABLE,null, ID_COLUMN + " = ?", new String[]{"" + id}, null,null,null);
playListInfo.setCount(cursorCount.getCount());
playListInfos.add(playListInfo);
}
if (cursor!=null){
cursor.close();
}
if (cursorCount!=null){
cursorCount.close();
}
return playListInfos;
}
public void createPlaylist(String name) {
ContentValues values = new ContentValues();
values.put(DatabaseHelper.NAME_COLUMN, name);
db.insert(DatabaseHelper.PLAY_LIST_TABLE, null, values);
}
public List<MusicInfo> getMusicListBySinger(String singer){
List<MusicInfo> musicInfoList = new ArrayList<>();
Cursor cursor = null;
db.beginTransaction();
try{
String sql = "select * from "+DatabaseHelper.MUSIC_TABLE+" where "+ DatabaseHelper.SINGER_COLUMN+" = ? ";
cursor = db.rawQuery(sql,new String[]{singer});
musicInfoList = cursorToMusicList(cursor);
db.setTransactionSuccessful();
}catch (Exception e){
e.printStackTrace();
}finally {
db.endTransaction();
if (cursor !=null)
cursor.close();
}
return musicInfoList;
}
public List<MusicInfo> getMusicListByAlbum(String album){
List<MusicInfo> musicInfoList = new ArrayList<>();
Cursor cursor = null;
db.beginTransaction();
try{
String sql = "select * from "+DatabaseHelper.MUSIC_TABLE+" where "+ DatabaseHelper.ALBUM_COLUMN+" = ? ";
cursor = db.rawQuery(sql,new String[]{album});
musicInfoList = cursorToMusicList(cursor);
db.setTransactionSuccessful();
}catch (Exception e){
e.printStackTrace();
}finally {
db.endTransaction();
if (cursor !=null)
cursor.close();
}
return musicInfoList;
}
public List<MusicInfo> getMusicListByFolder(String folder){
List<MusicInfo> musicInfoList = new ArrayList<>();
Cursor cursor = null;
db.beginTransaction();
try{
String sql = "select * from "+DatabaseHelper.MUSIC_TABLE+" where "+ DatabaseHelper.PARENT_PATH_COLUMN+" = ? ";
cursor = db.rawQuery(sql,new String[]{folder});
musicInfoList = cursorToMusicList(cursor);
db.setTransactionSuccessful();
}catch (Exception e){
e.printStackTrace();
}finally {
db.endTransaction();
if (cursor !=null)
cursor.close();
}
return musicInfoList;
}
public ArrayList<Integer> getMusicIdListByPlaylist(int playlistId){
Cursor cursor = null;
db.beginTransaction();
ArrayList<Integer> list = new ArrayList<Integer>();
try{
String sql = "select * from "+DatabaseHelper.PLAY_LISY_MUSIC_TABLE+" wher
没有合适的资源?快使用搜索试试~ 我知道了~
Android移动开发(音乐播放器 BlackMusic)
共209个文件
xml:78个
png:68个
java:50个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
5星 · 超过95%的资源 2 下载量 62 浏览量
2022-06-21
11:18:16
上传
评论 2
收藏 2.89MB RAR 举报
温馨提示
Android小项目(音乐播放器 BlackMusic) 本安卓项目包括:登录、注册、增删改查等App基本操作,可成功运行。 如遇导入运行失败等问题,请自行通过Google、百度等方式进行解决,楼主不负责远程项目部署、程序调试等内容。
资源推荐
资源详情
资源评论
收起资源包目录
Android移动开发(音乐播放器 BlackMusic) (209个子文件)
gradlew.bat 2KB
.gitignore 7B
build.gradle 2KB
build.gradle 580B
settings.gradle 15B
gradlew 5KB
gradle-wrapper.jar 52KB
DBManager.java 24KB
ModelActivity.java 19KB
MyMusicUtil.java 16KB
LastMyloveActivity.java 15KB
SingleFragment.java 14KB
ScanActivity.java 13KB
PlayActivity.java 12KB
HomeActivity.java 12KB
MusicPopMenuWindow.java 10KB
PlayBarFragment.java 10KB
PlaylistActivity.java 9KB
AddPlaylistWindow.java 9KB
PlayingPopWindow.java 8KB
RecyclerViewAdapter.java 8KB
ThemeActivity.java 7KB
SideBar.java 7KB
PlayerManagerReceiver.java 7KB
ScanView.java 7KB
HomeListViewAdapter.java 7KB
PlaylistAdapter.java 6KB
LocalMusicActivity.java 5KB
Constant.java 5KB
WelcomeActivity.java 4KB
MusicInfo.java 4KB
DividerItemDecoration.java 4KB
FolderAdapter.java 4KB
AlbumAdapter.java 4KB
SingerAdapter.java 4KB
DatabaseHelper.java 3KB
ChineseToEnglish.java 3KB
SingerFragment.java 3KB
FolderFragment.java 3KB
SelectorUtil.java 3KB
AlbumFragment.java 3KB
UpdateUIThread.java 2KB
CustomDividerView.java 2KB
AboutActivity.java 2KB
AlbumInfo.java 2KB
FolderInfo.java 2KB
ThemeInfo.java 2KB
BaseActivity.java 2KB
MusicPlayerService.java 2KB
SingerInfo.java 1KB
PlayListInfo.java 1KB
CustomFragment.java 1KB
MyApplication.java 991B
PlayBarBaseActivity.java 971B
MyViewPager.java 916B
CustomAttrValueUtil.java 665B
HttpUtil.java 503B
blackmusic.jks 2KB
bg_play.jpg 16KB
LICENSE 11KB
bg_playlist.png 2.46MB
playlist.png 34KB
disc.png 32KB
ic_launcher.png 24KB
ic_launcher.png 22KB
ic_launcher.png 21KB
ic_launcher.png 21KB
ic_launcher.png 20KB
bg_disc.png 17KB
play_btn_play_pressed.png 9KB
play_pause_pressed.png 8KB
album.png 6KB
singer.png 6KB
ic_play_btn_next_pressed.png 5KB
music_note.png 4KB
play_btn_prev_pressed.png 4KB
play_mode_random.png 4KB
play_btn_play.png 3KB
thumb_press.png 3KB
thumb_normal.png 3KB
play_mode_singel_cycle_pressed.png 3KB
folder.png 3KB
play_pause.png 3KB
play_mode_random_pressed.png 3KB
play_mode_cycle_pressed.png 3KB
ic_play_btn_next.png 3KB
play_btn_prev.png 3KB
play_mode_singel_cycle.png 3KB
pre_press.png 2KB
next_press.png 2KB
pre.png 2KB
play_mode_cycle.png 2KB
next.png 2KB
single_cycle_press.png 2KB
single_cycle.png 2KB
all_cycle.png 2KB
all_cycle_press.png 2KB
random.png 2KB
random_press.png 2KB
menu_press.png 2KB
共 209 条
- 1
- 2
- 3
栀陌笙晨
- 粉丝: 20
- 资源: 11
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
前往页