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