-
Notifications
You must be signed in to change notification settings - Fork 12
[김영수_Android] 10주차 과제 제출 #72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| package com.example.bcsd_android_2025_1 | ||
|
|
||
| import android.content.Intent | ||
| import android.view.LayoutInflater | ||
| import android.view.View | ||
| import android.view.ViewGroup | ||
| import android.widget.TextView | ||
| import androidx.core.content.ContextCompat | ||
| import androidx.recyclerview.widget.RecyclerView | ||
|
|
||
| class AudioAdapter : RecyclerView.Adapter<AudioAdapter.AudioViewHolder>(){ | ||
|
|
||
|
|
||
| private val items = mutableListOf<MainActivity.AudioItem>() | ||
|
|
||
| fun submitList(newList: List<MainActivity.AudioItem>) { | ||
| items.clear() | ||
| items.addAll(newList) | ||
| notifyDataSetChanged() | ||
| } | ||
|
|
||
| inner class AudioViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { | ||
| val titleTextView: TextView = itemView.findViewById(R.id.tv_item_title) | ||
| val artistTextView: TextView = itemView.findViewById(R.id.tv_item_artist) | ||
| val timeTextView: TextView = itemView.findViewById(R.id.tv_item_time) | ||
| } | ||
|
|
||
| override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AudioViewHolder { | ||
| val view = LayoutInflater.from(parent.context) | ||
| .inflate(R.layout.item, parent, false) | ||
| return AudioViewHolder(view) | ||
| } | ||
|
|
||
| override fun onBindViewHolder(holder: AudioViewHolder, position: Int) { | ||
| val item = items[position] | ||
| holder.titleTextView.text = item.name | ||
| holder.artistTextView.text = item.artist | ||
|
|
||
| val totalTime = item.duration/1000 | ||
| val hours = totalTime/3600 | ||
| val minutes = (totalTime%3600)/60 | ||
| val seconds = totalTime%60 | ||
| if (hours > 0){ | ||
| holder.timeTextView.text = String.format("%d:%02d:%02d", hours, minutes, seconds) | ||
| } else { | ||
| holder.timeTextView.text = String.format("%d:%02d", minutes, seconds) | ||
| } | ||
|
|
||
| holder.itemView.setOnClickListener { | ||
| val context = holder.itemView.context | ||
| val intent = Intent(context, AudioPlayer::class.java).apply{ | ||
| putExtra("AUDIO_URI",item.uri) | ||
| putExtra("AUDIO_TITLE",item.name) | ||
| } | ||
| ContextCompat.startForegroundService(context, intent) | ||
| } | ||
|
|
||
| } | ||
|
|
||
| override fun getItemCount(): Int = items.size | ||
|
|
||
|
|
||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| package com.example.bcsd_android_2025_1 | ||
|
|
||
| import android.app.Application | ||
| import android.content.ContentUris | ||
| import android.provider.MediaStore.Audio.Media | ||
| import androidx.lifecycle.AndroidViewModel | ||
| import androidx.lifecycle.LiveData | ||
| import androidx.lifecycle.MutableLiveData | ||
| import androidx.lifecycle.viewModelScope | ||
| import kotlinx.coroutines.Dispatchers | ||
| import kotlinx.coroutines.launch | ||
|
|
||
|
|
||
| class AudioListManager(application: Application) : AndroidViewModel(application) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ViewModel 사용을 시도한 것은 좋은데, ViewModel은 이렇게 사용하면 안됩니다. |
||
| private val audioListIn = MutableLiveData<List<MainActivity.AudioItem>>() | ||
| val audioListObserve: LiveData<List<MainActivity.AudioItem>> get() = audioListIn | ||
|
|
||
| fun loadAudioFiles() { | ||
| viewModelScope.launch(Dispatchers.IO) { | ||
| val list = mutableListOf<MainActivity.AudioItem>() | ||
| val resolver = getApplication<Application>().contentResolver | ||
|
|
||
| val projection = arrayOf( | ||
| Media._ID, | ||
| Media.DISPLAY_NAME, | ||
| Media.ARTIST, | ||
| Media.DURATION | ||
| ) | ||
|
|
||
| val selection= "${Media.IS_MUSIC} != 0 AND ${Media.DURATION} > 0" | ||
| val selectionArgs: Array<String>? = null | ||
|
|
||
| val sortOrder = "${Media.DATE_ADDED} DESC" | ||
|
|
||
| resolver.query( | ||
| Media.EXTERNAL_CONTENT_URI, | ||
| projection, | ||
| selection, | ||
| selectionArgs, | ||
| sortOrder | ||
| )?.use { cursor -> | ||
| val idColumn = cursor.getColumnIndexOrThrow(Media._ID) | ||
| val nameColumn = cursor.getColumnIndexOrThrow(Media.DISPLAY_NAME) | ||
| val artistColumn = cursor.getColumnIndexOrThrow(Media.ARTIST) | ||
| val durationColumn = cursor.getColumnIndexOrThrow(Media.DURATION) | ||
|
|
||
| while (cursor.moveToNext()) { | ||
| val id = cursor.getLong(idColumn) | ||
| val name = cursor.getString(nameColumn) | ||
| val artists = cursor.getString(artistColumn) | ||
| val duration = cursor.getLong(durationColumn) | ||
| val uri = ContentUris.withAppendedId(Media.EXTERNAL_CONTENT_URI, id) | ||
|
|
||
| list.add(MainActivity.AudioItem(id, name, artists, duration, uri)) | ||
|
|
||
| } | ||
| } | ||
|
|
||
| audioListIn.postValue(list) | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
별도의 확장함수로 분리하는게 좋을 것 같습니다