반응형

[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
package com.cookandroid.project13_2
 
import android.content.Context
import android.media.MediaActionSound
import android.media.MediaPlayer
import android.os.Build.VERSION_CODES.M
import android.os.Build.VERSION_CODES.O
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Environment
import android.os.SystemClock
import android.view.View
import android.widget.ArrayAdapter
import android.widget.ListView
import android.widget.SeekBar
import android.widget.Toast
import androidx.appcompat.widget.AppCompatCheckedTextView
import androidx.core.app.ActivityCompat
import com.cookandroid.project13_2.R
import kotlinx.android.synthetic.main.activity_main.*
import java.io.File
import java.text.SimpleDateFormat
 
class MainActivity : AppCompatActivity() {
 
    lateinit var mPlayer : MediaPlayer
 
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
 
        title = "직접 풀어보기 13-2"
 
        mPlayer = MediaPlayer.create(this, R.raw.song1)
        seekBar1.max = mPlayer.duration
 
 
        switch1.setOnClickListener {
 
            if(switch1.isChecked == true) {
                mPlayer = MediaPlayer.create(this, R.raw.song1)
                mPlayer.seekTo(seekBar1.progress)
                mPlayer.start()
 
                object : Thread(){
                    override fun run(){
                        if(mPlayer == null)
                            return
                        while(mPlayer.isPlaying){
                            seekBar1.progress = mPlayer.currentPosition
                            SystemClock.sleep(200)
                        }
 
                    }
                }.start()
            }else{
                seekBar1.progress = 0
                mPlayer.stop()
                mPlayer.reset()
            }
        }
 
        seekBar1.setOnSeekBarChangeListener(
            object : SeekBar.OnSeekBarChangeListener{
                override fun onProgressChanged(
                    seekBar: SeekBar?,
                    progress: Int,
                    fromUser: Boolean
                ) {
                    if(fromUser){
                        if(switch1.isChecked) {
                            mPlayer.pause()
                            mPlayer.seekTo(progress)
                            mPlayer.start()
                        }
                    }
                }
 
                override fun onStartTrackingTouch(seekBar: SeekBar?) {
 
                }
 
                override fun onStopTrackingTouch(seekBar: SeekBar?) {
 
                }
 
            }
        )
    }
}
cs

<activity_main.xml>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?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">
    <Switch
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="음악 듣기"
        android:id="@+id/switch1"/>
    <SeekBar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="?android:attr/progressBarStyleHorizontal"
        android:padding="20dp"
        android:id="@+id/seekBar1"/>
 
 
</LinearLayout>
cs
반응형

+ Recent posts