update
This commit is contained in:
parent
e97a408286
commit
f8955cb1e4
1
.idea/.name
generated
Normal file
1
.idea/.name
generated
Normal file
@ -0,0 +1 @@
|
||||
HiMelody
|
||||
@ -16,8 +16,8 @@ android {
|
||||
applicationId = "com.hi.melody.music.noise"
|
||||
minSdk = 24
|
||||
targetSdk = 34
|
||||
versionCode = 1
|
||||
versionName = "1.0.1"
|
||||
versionCode = 2
|
||||
versionName = "1.0.2"
|
||||
|
||||
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
|
||||
|
||||
|
||||
@ -21,15 +21,16 @@ import androidx.core.content.ContextCompat
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.recyclerview.widget.LinearLayoutManager
|
||||
import com.gyf.immersionbar.ktx.immersionBar
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import melody.offline.music.App
|
||||
import melody.offline.music.R
|
||||
import melody.offline.music.activity.SettingsActivity
|
||||
import melody.offline.music.adapter.ParentsVoiceAdapter
|
||||
import melody.offline.music.bean.Audio
|
||||
import melody.offline.music.databinding.FragmentImportBinding
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
class ImportFragment : Fragment() {
|
||||
private lateinit var binding: FragmentImportBinding
|
||||
@ -59,7 +60,7 @@ class ImportFragment : Fragment() {
|
||||
checkAndRequestPermissions()
|
||||
}
|
||||
importAdapterList.clear()
|
||||
importAdapterList.addAll(melody.offline.music.App.importList)
|
||||
importAdapterList.addAll(App.importList)
|
||||
parentsVoiceAdapter = ParentsVoiceAdapter(requireActivity(), importAdapterList)
|
||||
binding.importRv.layoutManager =
|
||||
LinearLayoutManager(requireActivity(), LinearLayoutManager.VERTICAL, false)
|
||||
@ -105,12 +106,12 @@ class ImportFragment : Fragment() {
|
||||
binding.noContentLayout.visibility = View.VISIBLE
|
||||
}
|
||||
|
||||
if (melody.offline.music.App.currentPlayingAudio != null) {
|
||||
if (melody.offline.music.App.importList.isNotEmpty()) {
|
||||
if (App.currentPlayingAudio != null) {
|
||||
if (App.importList.isNotEmpty()) {
|
||||
importAdapterList.clear()
|
||||
importAdapterList.addAll(melody.offline.music.App.importList)
|
||||
importAdapterList.addAll(App.importList)
|
||||
for ((index, audio) in importAdapterList.withIndex()) {
|
||||
if (audio.file == melody.offline.music.App.currentPlayingAudio?.file) {
|
||||
if (audio.file == App.currentPlayingAudio?.file) {
|
||||
parentsVoiceAdapter?.notifyDataSetChanged()
|
||||
break
|
||||
}
|
||||
@ -149,66 +150,70 @@ class ImportFragment : Fragment() {
|
||||
@SuppressLint("NotifyDataSetChanged")
|
||||
fun getMusicFiles(context: Context): List<String> {
|
||||
val musicFiles = mutableListOf<String>()
|
||||
val contentResolver: ContentResolver = context.contentResolver
|
||||
|
||||
// 定义查询参数
|
||||
val projection = arrayOf(
|
||||
MediaStore.Audio.Media._ID,
|
||||
MediaStore.Audio.Media.DISPLAY_NAME,
|
||||
MediaStore.Audio.Media.DATA
|
||||
)
|
||||
|
||||
val selection = "${MediaStore.Audio.Media.IS_MUSIC} != 0"
|
||||
val sortOrder = "${MediaStore.Audio.Media.DISPLAY_NAME} ASC"
|
||||
|
||||
// 查询音频文件
|
||||
val cursor = contentResolver.query(
|
||||
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
|
||||
projection,
|
||||
selection,
|
||||
null,
|
||||
sortOrder
|
||||
)
|
||||
|
||||
cursor?.use { cursor ->
|
||||
val idColumn = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID)
|
||||
val nameColumn = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME)
|
||||
val dataColumn = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA)
|
||||
|
||||
while (cursor.moveToNext()) {
|
||||
val id = cursor.getLong(idColumn)
|
||||
val name = cursor.getString(nameColumn)
|
||||
val data = cursor.getString(dataColumn)
|
||||
val contentUri = ContentUris.withAppendedId(
|
||||
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
|
||||
id
|
||||
CoroutineScope(Dispatchers.IO).launch {
|
||||
try {
|
||||
val contentResolver: ContentResolver = context.contentResolver
|
||||
// 定义查询参数
|
||||
val projection = arrayOf(
|
||||
MediaStore.Audio.Media._ID,
|
||||
MediaStore.Audio.Media.DISPLAY_NAME,
|
||||
MediaStore.Audio.Media.DATA
|
||||
)
|
||||
val duration2 = getAudioDuration(contentUri)
|
||||
val audio = Audio(name, contentUri.toString(), "", duration2, false)
|
||||
|
||||
musicFiles.add(contentUri.toString())
|
||||
// 如果你想要获取文件的具体路径,可以使用 data 变量
|
||||
// musicFiles.add(data)
|
||||
val selection = "${MediaStore.Audio.Media.IS_MUSIC} != 0"
|
||||
val sortOrder = "${MediaStore.Audio.Media.DISPLAY_NAME} ASC"
|
||||
|
||||
CoroutineScope(Dispatchers.IO).launch {
|
||||
if (audio.duration > 0) {
|
||||
melody.offline.music.App.databaseManager.insertAudioFile(audio)
|
||||
}
|
||||
withContext(Dispatchers.Main) {
|
||||
melody.offline.music.App.initImportAudio {
|
||||
// 查询音频文件
|
||||
val cursor = contentResolver.query(
|
||||
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
|
||||
projection,
|
||||
selection,
|
||||
null,
|
||||
sortOrder
|
||||
)
|
||||
|
||||
cursor?.use { cursor ->
|
||||
val idColumn = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID)
|
||||
val nameColumn =
|
||||
cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME)
|
||||
val dataColumn = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA)
|
||||
|
||||
while (cursor.moveToNext()) {
|
||||
val id = cursor.getLong(idColumn)
|
||||
val name = cursor.getString(nameColumn)
|
||||
val data = cursor.getString(dataColumn)
|
||||
val contentUri = ContentUris.withAppendedId(
|
||||
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
|
||||
id
|
||||
)
|
||||
val duration2 = getAudioDuration(contentUri)
|
||||
val audio = Audio(name, contentUri.toString(), "", duration2, false)
|
||||
|
||||
musicFiles.add(contentUri.toString())
|
||||
// 如果你想要获取文件的具体路径,可以使用 data 变量
|
||||
// musicFiles.add(data)
|
||||
if (audio.duration > 0) {
|
||||
App.databaseManager.insertAudioFile(audio)
|
||||
}
|
||||
App.initImportAudio {
|
||||
importAdapterList.clear()
|
||||
importAdapterList.addAll(melody.offline.music.App.importList)
|
||||
importAdapterList.addAll(App.importList)
|
||||
parentsVoiceAdapter?.notifyDataSetChanged()
|
||||
if (importAdapterList.isNotEmpty()) {
|
||||
binding.noContentLayout.visibility = View.GONE
|
||||
} else {
|
||||
binding.noContentLayout.visibility = View.VISIBLE
|
||||
showNoDataDialog()
|
||||
}
|
||||
binding.loadingLayout.visibility = View.GONE
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
} finally {
|
||||
withContext(Dispatchers.Main) {
|
||||
if (importAdapterList.isNotEmpty()) {
|
||||
binding.noContentLayout.visibility = View.GONE
|
||||
} else {
|
||||
binding.noContentLayout.visibility = View.VISIBLE
|
||||
showNoDataDialog()
|
||||
}
|
||||
binding.loadingLayout.visibility = View.GONE
|
||||
}
|
||||
}
|
||||
}
|
||||
return musicFiles
|
||||
|
||||
Loading…
Reference in New Issue
Block a user