반응형

[Gradle 설정]

1
id 'kotlin-android-extensions'
cs

<MainActivity>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package com.cookandroid.project13_1
 
import android.content.Context
import android.media.MediaPlayer
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Environment
import android.view.View
import android.widget.ArrayAdapter
import android.widget.ListView
import android.widget.Toast
import androidx.appcompat.widget.AppCompatCheckedTextView
import androidx.core.app.ActivityCompat
import kotlinx.android.synthetic.main.activity_main.*
import java.io.File
 
class MainActivity : AppCompatActivity() {
 
    lateinit var mp3List : ArrayList<String>
    lateinit var selectedMP3 : String
 
    var mp3Path = Environment.getExternalStorageDirectory().path + "/"
    lateinit var mPlayer : MediaPlayer
 
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
 
        title = "간단 MP3 플레이어"
 
        ActivityCompat.requestPermissions(this, arrayOf(android.Manifest.permission.WRITE_EXTERNAL_STORAGE), 0)
 
        mp3List = ArrayList()
 
        var listFiles = File(mp3Path).listFiles()
        var fileName : String
        var extName : String
        for (file in listFiles!!){
            fileName = file.name
            extName = fileName.substring(fileName.length - 3)
            if(extName == "mp3")
                mp3List.add(fileName)
 
        }
 
        var adapter = ArrayAdapter(this, android.R.layout.simple_list_item_single_choice, mp3List)
        listViewMP3.choiceMode = ListView.CHOICE_MODE_SINGLE
        listViewMP3.adapter = adapter
        listViewMP3.setItemChecked(0, true)
 
        listViewMP3.setOnItemClickListener { parent, view, position, id ->
            selectedMP3 = mp3List[position]
        }
        selectedMP3 = mp3List[0]
 
        btnPlay.setOnClickListener {
            mPlayer = MediaPlayer()
            mPlayer.setDataSource(mp3Path + selectedMP3)
            mPlayer.prepare()
            mPlayer.start()
            btnPlay.isClickable = false
            btnStop.isClickable = true
            btnPause.isClickable =  true
            tvMP3.text = "실행중인 음악 : ${selectedMP3}"
            pbMP3.visibility = View.VISIBLE
 
        }
        btnPause.setOnClickListener {
            if(btnPause.text == "일시정지"){
                btnPause.text = "이어듣기"
                mPlayer.pause()
            }else{
                btnPause.text = "일시정지"
                mPlayer.start()
 
            }
        }
        btnStop.setOnClickListener {
            mPlayer.stop()
            mPlayer.reset()
            btnPlay.isClickable = true
            btnStop.isClickable = false
            btnPause.isClickable = false
            btnPause.text = "일시정지"
            tvMP3.text = "실행중인 음악 : "
            pbMP3.visibility = View.INVISIBLE
        }
        btnStop.isClickable = false
        btnPause.isClickable = false
 
    }
}
cs

<activity_main.xml>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
 
    <LinearLayout
        android:background="@color/purple_200"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">
        <ListView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/listViewMP3">
        </ListView>
    </LinearLayout>
 
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
 
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:id="@+id/btnPlay"
            android:text="듣기"/>
 
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:id="@+id/btnPause"
            android:text="일시정지"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:id="@+id/btnStop"
            android:text="중지"/>
 
    </LinearLayout>
 
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center_vertical">
 
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tvMP3"
            android:text="실행중인 음악:"/>
        <ProgressBar
            android:id="@+id/pbMP3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="invisible"/>
 
    </LinearLayout>
 
</LinearLayout>
cs
반응형

+ Recent posts