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 
93 
94 
95 
96 
97 
98 
99 
100 
101 
102 
103 
104 
105 
106 
107 
108 
109 
110 
111 
112 
113 
114 
115 
116 
117 
118 
119 
120 
121 
122 
123 
124 
125 
126 
127 
128 
129 
130 
131 
132 
133 
134 
135 
136 
137 
138 
139 
140 
141 
142 
143 
144 
145 
146 
147 
148 
149 
150 
151 
152 
153 
154 
155 
156 
157 
158 
159 
160 
161 
162 
163 
164 
165 
166 
167 
168 
169 
170 
171 
172 
173 
174 
175 
176 
177 
178 
179 
180 
181 
182 
183 
184 
185 
186 
187 
188 
189 
190 
 | 
 package com.example.boostcoursepractice; 
import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.hardware.Camera; 
import android.media.MediaPlayer; 
import android.media.MediaRecorder; 
import android.net.Uri; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.os.Environment; 
import android.provider.MediaStore; 
import android.util.Log; 
import android.view.View; 
import androidx.annotation.Nullable; 
import androidx.appcompat.app.AppCompatActivity; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.ImageView; 
import android.widget.MediaController; 
import android.widget.TextView; 
import android.widget.Toast; 
import android.widget.VideoView; 
import java.io.File; 
import java.net.URL; 
import java.util.HashMap; 
public class MainActivity extends AppCompatActivity { 
    MediaRecorder recorder; 
    String filename; 
    MediaPlayer player; 
    int position = 0; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 
        File sdcard = Environment.getExternalStorageDirectory(); 
        File file = new File(sdcard, "recorded.mp4"); 
        filename = file.getAbsolutePath(); 
        Log.d("MainActivity", "저장할 파일명 : " + filename); 
        Button button = (Button) findViewById(R.id.button); 
        button.setOnClickListener(new View.OnClickListener() { 
            @Override 
            public void onClick(View view) { 
                palyAudio(); 
            } 
        }); 
        Button button2 = (Button) findViewById(R.id.button2); 
        button2.setOnClickListener(new View.OnClickListener() { 
            @Override 
            public void onClick(View view) { 
                pauseAudio(); 
            } 
        }); 
        Button button3 = (Button) findViewById(R.id.button3); 
        button3.setOnClickListener(new View.OnClickListener() { 
            @Override 
            public void onClick(View view) { 
                resumeAudio(); 
            } 
        }); 
        Button button4 = (Button) findViewById(R.id.button4); 
        button4.setOnClickListener(new View.OnClickListener() { 
            @Override 
            public void onClick(View view) { 
                stopAudio(); 
            } 
        }); 
        Button button5 = (Button) findViewById(R.id.button5); 
        button5.setOnClickListener(new View.OnClickListener() { 
            @Override 
            public void onClick(View view) { 
                recordAudio(); 
            } 
        }); 
        Button button6 = (Button) findViewById(R.id.button6); 
        button6.setOnClickListener(new View.OnClickListener() { 
            @Override 
            public void onClick(View view) { 
                stopRecording(); 
            } 
        }); 
    } 
    public void palyAudio() { 
        try { 
            closePlayer(); 
            player = new MediaPlayer(); 
            player.setDataSource(filename); 
            player.prepare(); 
            player.start(); 
            Toast.makeText(this, "재생 시작됨", Toast.LENGTH_LONG).show(); 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } 
    } 
    public void pauseAudio() { 
        if (player != null && player.isPlaying()) { 
            position = player.getCurrentPosition(); 
            player.pause(); 
            Toast.makeText(this, "일시 정지지됨", Toast.LENGTH_LONG).show(); 
        } 
    } 
    public void resumeAudio() { 
        if (player != null && !player.isPlaying()) { 
            player.seekTo(position); 
            player.start(); 
            Toast.makeText(this, "재시작됨", Toast.LENGTH_LONG).show(); 
        } 
    } 
    public void stopAudio() { 
        if (player != null && player.isPlaying()) { 
            player.stop(); 
            Toast.makeText(this, "중지됨", Toast.LENGTH_LONG).show(); 
        } 
    } 
    public void closePlayer() { 
        if (player != null) { 
            player.release(); 
            player = null; 
        } 
    } 
    public void recordAudio() { 
        recorder = new MediaRecorder(); 
        recorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
        recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); 
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); 
        recorder.setOutputFile(filename); 
        try { 
            recorder.prepare(); 
            recorder.start(); 
            Toast.makeText(this, "녹음 시작됨", Toast.LENGTH_LONG).show(); 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } 
    } 
    public void stopRecording(){ 
        if(recorder != null){ 
            recorder.stop(); 
            recorder.release(); 
            recorder = null ; 
            Toast.makeText(this, "녹음 중지됨", Toast.LENGTH_LONG).show(); 
        } 
    } 
} 
 | 
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 
 | 
 <?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 
    <Button 
        android:id="@+id/button5" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:text="녹음시작" /> 
    <Button 
        android:id="@+id/button6" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:text="녹음중지" /> 
    <Button 
        android:id="@+id/button" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:text="재생" /> 
    <Button 
        android:id="@+id/button2" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:text="일시정지" /> 
    <Button 
        android:id="@+id/button3" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:text="재시작" /> 
    <Button 
        android:id="@+id/button4" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:text="정지" /> 
</LinearLayout> 
 | 
cs | 
기존에 진행 했던 '7-2-1 음악 재생하기'에서 좀 더 확장하여 구현하였다. 항상 종료가 될 때는 아래처럼
recorder.stop();
recorder.release();
recorder = null ;
자원을 해제하는 것을 잊지 말자

'2019 summer 부스트코스 에이스(안드로이드 프로그래밍) > 7. 멀티미디어' 카테고리의 다른 글
| 7-4-1 리싸이클러뷰 (0) | 2019.08.31 | 
|---|---|
| 7-2-2 동영상 재생하기 (0) | 2019.08.30 | 
| 7-2-1 음악 재생하기 (0) | 2019.08.30 | 
| 7-1-1 사진 찍어 저장하기 (0) | 2019.08.29 |