Skip to content

Commit

Permalink
修复和优化歌单导入以及歌词显示等问题
Browse files Browse the repository at this point in the history
  • Loading branch information
rRemix committed Mar 23, 2019
1 parent 8906225 commit 55624fa
Show file tree
Hide file tree
Showing 12 changed files with 121 additions and 33 deletions.
25 changes: 25 additions & 0 deletions UPDATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -394,3 +394,28 @@
1. 优化横屏下播放界面
2. 修复锁屏页封面与歌词不显示

### v1.4.9
1. 重构部分代码
2. 修复诸多小问题

### v1.4.9.1
1. 修复桌面歌词相关问题

### v1.5.0.0
1. 修复锁屏界面封面与歌词不显示
2. 新增语言选择

### v1.5.0.1
1. 修复播放队列设置失效
2. 优化播放界面歌词字体颜色

### v1.5.0.2
1. 修复打开软件总是选中第一首歌曲
2. 修复无法切换播放模式

### v1.5.1.0
1. 本地歌词支持翻译并优化显示
2. 桌面歌词自定义字体颜色

### v1.5.1.1
1. 优化本地歌词匹配与歌词显示
8 changes: 4 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ android {
applicationId "remix.myplayer"
minSdkVersion 17
targetSdkVersion 26
versionCode 1511
versionName "1.5.1.1-debug"
versionCode 1512
versionName "1.5.1.2"

flavorDimensions "default"
vectorDrawables.useSupportLibrary = true
ndk {
// abiFilters 'armeabi-v7a'
abiFilters 'armeabi-v7a', 'x86'
abiFilters 'armeabi-v7a'
// abiFilters 'armeabi-v7a', 'x86'
}
externalNativeBuild {
cmake {
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/remix/myplayer/db/room/AppDatabase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ abstract class AppDatabase : RoomDatabase() {
@Volatile
private var INSTANCE: AppDatabase? = null

@JvmStatic
fun getInstance(context: Context): AppDatabase =
INSTANCE ?: synchronized(this) {
INSTANCE ?: buildDatabase(context).also { INSTANCE = it }
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/java/remix/myplayer/db/room/DatabaseRepository.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package remix.myplayer.db.room

import android.arch.persistence.db.SimpleSQLiteQuery
import android.content.Context
import android.database.Cursor
import android.provider.MediaStore
Expand Down Expand Up @@ -216,6 +217,16 @@ class DatabaseRepository private constructor() {
}
}

/**
* 获取所有播放列表
*/
fun getSortPlayList(sortQuery: String): Single<List<PlayList>> {
return Single
.fromCallable {
db.playListDao().runtimeQuery(SimpleSQLiteQuery(sortQuery))
}
}


/**
* 修改播放列表
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/java/remix/myplayer/db/room/dao/PlayListDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ package remix.myplayer.db.room.dao

import android.arch.persistence.room.*
import remix.myplayer.db.room.model.PlayList
import android.arch.persistence.db.SupportSQLiteQuery
import android.arch.persistence.room.RawQuery



/**
* Created by remix on 2019/1/12
Expand All @@ -16,6 +20,9 @@ interface PlayListDao {
""")
fun selectAll(): List<PlayList>

@RawQuery
fun runtimeQuery(sortQuery: SupportSQLiteQuery): List<PlayList>


@Query("""
SELECT * FROM PlayList
Expand Down
40 changes: 34 additions & 6 deletions app/src/main/java/remix/myplayer/helper/M3UHelper.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package remix.myplayer.helper

import android.app.Activity
import android.content.Context
import android.provider.MediaStore
import io.reactivex.CompletableSource
import io.reactivex.Single
import io.reactivex.android.schedulers.AndroidSchedulers
Expand All @@ -11,11 +13,13 @@ import remix.myplayer.App
import remix.myplayer.R
import remix.myplayer.db.room.DatabaseRepository
import remix.myplayer.request.network.RxUtil.applySingleScheduler
import remix.myplayer.theme.Theme
import remix.myplayer.util.MediaStoreUtil
import remix.myplayer.util.ToastUtil
import java.io.BufferedWriter
import java.io.File
import java.io.FileWriter
import java.lang.ref.WeakReference

object M3UHelper {
private val databaseRepository = DatabaseRepository.getInstance()
Expand All @@ -29,7 +33,16 @@ object M3UHelper {
* 导入歌单
*/
@JvmStatic
fun importM3UFile(file: File, playlistName: String, newCreate: Boolean): Disposable? {
fun importM3UFile(activity: Activity, file: File, playlistName: String, newCreate: Boolean): Disposable? {
val activityRef = WeakReference<Activity>(activity)
val dialog = Theme.getBaseDialog(activity)
.title(R.string.saveing)
.content(R.string.please_wait)
.cancelable(false)
.progress(true, 0)
.progressIndeterminateStyle(false).build()
dialog.show()

return Single
.just(file)
.filter {
Expand All @@ -39,21 +52,30 @@ object M3UHelper {
if (newCreate) {
val newId = databaseRepository.insertPlayList(playlistName).blockingGet()
if (newId <= 0) {
throw RuntimeException("unknown error")
throw Exception("insert $playlistName failed")
}
}
}
.map {
val audioIds = ArrayList<Int>()
file.readLines().forEachWithIndex { i: Int, path: String ->
if (i != 0 && !path.startsWith(ENTRY)) {
val id: Int
// 先直接判断本地文件是否存在
val song = File(path)
if (song.exists() && song.isFile) {
audioIds.add(MediaStoreUtil.getSongIdByUrl(path))
id = if (song.exists() && song.isFile) {
MediaStoreUtil.getSongIdByUrl(path)
} else {
// 再根据歌曲名去查找
MediaStoreUtil.getSongId(MediaStore.Audio.Media.DATA + " like ?",
arrayOf("%" + path.replace("\\", "/")))
}
if (id > 0) {
audioIds.add(id)
}
}
}
audioIds
return@map audioIds
.filter { audioId ->
audioId > 0
}
Expand All @@ -64,9 +86,15 @@ object M3UHelper {
.compose(applySingleScheduler())
.subscribe(
{
activityRef.get()?.runOnUiThread {
dialog.dismiss()
}
ToastUtil.show(App.getContext(), App.getContext().getString(R.string.import_playlist_to_count, playlistName, it))
},
{
activityRef.get()?.runOnUiThread {
dialog.dismiss()
}
ToastUtil.show(App.getContext(), R.string.import_fail, it.toString())
})
}
Expand All @@ -80,7 +108,7 @@ object M3UHelper {
.map {
Single
.fromCallable {
databaseRepository.insertPlayList(it.key)
databaseRepository.insertPlayList(it.key).subscribe()
databaseRepository.insertToPlayList(it.value, it.key).blockingGet()
}
.onErrorResumeNext(Single.just(0))
Expand Down
21 changes: 12 additions & 9 deletions app/src/main/java/remix/myplayer/lyric/DefaultLrcParser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,13 @@ class DefaultLrcParser : ILrcParser {
strBuilder.append("[")
strBuilder.append(lrcRow.timeStr)
strBuilder.append("]")
strBuilder.append(lrcRow.content)
strBuilder.append(if (!TextUtils.isEmpty(lrcRow.translate)) "\r\n" + lrcRow.translate + "\r\n" else "\r\n")
Timber.d(strBuilder.toString())
strBuilder.append(lrcRow.content).append("\n")
if (lrcRow.hasTranslate()) {
strBuilder.append("[")
strBuilder.append(lrcRow.timeStr)
strBuilder.append("]")
strBuilder.append(lrcRow.translate).append("\n")
}
appendText(strBuilder.toString())
}
}
Expand Down Expand Up @@ -94,14 +98,15 @@ class DefaultLrcParser : ILrcParser {
lrcRows.addAll(rows)
}

lrcRows.sort()
//合并翻译
val combineLrcRows = ArrayList<LrcRow>()
var index = 0
while (index < lrcRows.size - 1) {
while (index < lrcRows.size) {
// 判断下一句歌词和当前歌词的时间是否一致,一致则认为下一句是当前歌词的翻译
val currentRow = lrcRows[index]
val nextRow = lrcRows[index + 1]
if (currentRow.time == nextRow.time) { // 带翻译的歌词
val nextRow = lrcRows.getOrNull(index + 1)
if (currentRow.time == nextRow?.time && !currentRow.content.isNullOrBlank()) { // 带翻译的歌词
val tmp = LrcRow()
tmp.content = currentRow.content
tmp.time = currentRow.time
Expand All @@ -114,15 +119,13 @@ class DefaultLrcParser : ILrcParser {
}
index++
}
// if(combineLrcRows.size.toFloat() / lrcRows.size >= THRESHOLD_PROPORTION){

lrcRows.clear()
lrcRows.addAll(combineLrcRows)
// }


if (lrcRows.size == 0)
return lrcRows
//为歌词排序
lrcRows.sort()
//每行歌词的时间
for (i in 0 until lrcRows.size - 1) {
Expand Down
5 changes: 2 additions & 3 deletions app/src/main/java/remix/myplayer/lyric/LyricSearcher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import android.util.Base64
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import io.reactivex.Observable
import io.reactivex.Single
import io.reactivex.functions.Function
import org.jaudiotagger.tag.FieldKey
import remix.myplayer.App
Expand Down Expand Up @@ -197,7 +196,7 @@ class LyricSearcher {
MediaStore.Files.FileColumns.DATA + " like ? or " +
MediaStore.Files.FileColumns.DATA + " like ? or " +
MediaStore.Files.FileColumns.DATA + " like ? or " +
MediaStore.Files.FileColumns.DATA + " like ? ",
MediaStore.Files.FileColumns.DATA + " like ?",
getLocalSearchKey(),
null)
.use { filesCursor ->
Expand All @@ -206,8 +205,8 @@ class LyricSearcher {
Timber.v("file: %s", file.absolutePath)
if (file.exists() && file.isFile && file.canRead()) {
path = file.absolutePath
break
}

}
return path
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -424,9 +424,8 @@ public void accept(List<String> allPlayListsName) throws Exception {
position == 0 && text.toString().endsWith(
"(" + getString(R.string.new_create) + ")");
mDisposables.add(
importM3UFile(file, chooseNew ? newPlaylistName : text.toString(), chooseNew));
importM3UFile(SettingActivity.this,file, chooseNew ? newPlaylistName : text.toString(), chooseNew));
})
.positiveText(R.string.confirm)
.show();
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import butterknife.ButterKnife;
import butterknife.OnClick;
import java.util.List;
import org.jetbrains.annotations.Nullable;
import remix.myplayer.R;
import remix.myplayer.bean.mp3.Song;
import remix.myplayer.db.room.DatabaseRepository;
Expand Down Expand Up @@ -129,6 +130,11 @@ protected Loader<List<Song>> getLoader() {
return new AsyncSongLoader(mContext);
}

@Override
public void saveSortOrder(@Nullable String sortOrder) {

}

private static class AsyncSongLoader extends AppWrappedAsyncTaskLoader<List<Song>> {

private AsyncSongLoader(Context context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ class LyricFragment : BaseMusicFragment() {
}
}, Consumer {
Timber.v(it)
ToastUtil.show(requireContext(), "读取歌词失败: $it")
if (id == info?.id) {
lrcView.lrcRows = null
lrcView.setText(getStringSafely(R.string.no_lrc))
Expand Down
26 changes: 17 additions & 9 deletions app/src/main/java/remix/myplayer/ui/fragment/PlayListFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,31 @@

import static remix.myplayer.ui.adapter.HeaderAdapter.LIST_MODE;

import android.arch.persistence.db.SimpleSQLiteQuery;
import android.content.Context;
import android.database.Cursor;
import android.support.v4.content.Loader;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.View;
import butterknife.BindView;
import io.reactivex.Single;
import io.reactivex.SingleSource;
import io.reactivex.functions.Function;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Callable;
import remix.myplayer.App;
import remix.myplayer.R;
import remix.myplayer.db.room.AppDatabase;
import remix.myplayer.db.room.DatabaseRepository;
import remix.myplayer.db.room.model.PlayList;
import remix.myplayer.db.room.model.PlayQueue;
import remix.myplayer.helper.SortOrder;
import remix.myplayer.helper.SortOrder.PlayListSortOrder;
import remix.myplayer.misc.asynctask.WrappedAsyncTaskLoader;
import remix.myplayer.misc.interfaces.LoaderIds;
import remix.myplayer.misc.interfaces.OnItemClickListener;
Expand All @@ -25,6 +36,7 @@
import remix.myplayer.ui.widget.fastcroll_recyclerview.FastScrollRecyclerView;
import remix.myplayer.util.Constants;
import remix.myplayer.util.SPUtil;
import remix.myplayer.util.SPUtil.SETTING_KEY;
import remix.myplayer.util.ToastUtil;
import timber.log.Timber;

Expand Down Expand Up @@ -91,7 +103,7 @@ public PlayListAdapter getAdapter() {

@Override
public void onPlayListChanged(String name) {
if(name.equals(PlayList.TABLE_NAME)){
if (name.equals(PlayList.TABLE_NAME)) {
onMediaStoreChanged();
}
}
Expand All @@ -114,14 +126,10 @@ public AsyncPlayListLoader(Context context) {

@Override
public List<PlayList> loadInBackground() {
List<PlayList> playLists = DatabaseRepository.getInstance()
.getAllPlaylist()
.onErrorReturn(throwable -> {
Timber.v(throwable);
return Collections.emptyList();
})
.blockingGet();
return playLists;
final String sort = SPUtil.getValue(App.getContext(), SETTING_KEY.NAME, SETTING_KEY.PLAYLIST_SORT_ORDER,
PlayListSortOrder.PLAYLIST_A_Z);
return DatabaseRepository.getInstance()
.getSortPlayList("SELECT * FROM PlayList ORDER BY " + sort).blockingGet();
}
}
}

0 comments on commit 55624fa

Please sign in to comment.