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
|
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.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.TextView;
import android.widget.Toast;
import java.io.File;
import java.net.URL;
import java.util.HashMap;
public class MainActivity extends AppCompatActivity {
public static String url = "http://sites.google.com/site/ubiaccessmobile/sample_audio.amr";
MediaPlayer player;
int position = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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();
}
});
}
public void palyAudio() {
try {
closePlayer();
player = new MediaPlayer();
player.setDataSource(url);
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 ;
}
}
}
|
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
|
<?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/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 |
음악을 재생하기 위해서는 MediaPlayer를 이용하면 된다.
구현은 위 코드대로 하면 되고 중요한 것은 스피커는 하나의 자원이기 때문에 항상 다 쓰고 나면 아래처럼
if(player != null){
player.release();
player = null ;
}
해제를 해줘야 한다.
'2019 summer 부스트코스 에이스(안드로이드 프로그래밍) > 7. 멀티미디어' 카테고리의 다른 글
7-4-1 리싸이클러뷰 (0) | 2019.08.31 |
---|---|
7-3-1 음성 녹음하기 (0) | 2019.08.30 |
7-2-2 동영상 재생하기 (0) | 2019.08.30 |
7-1-1 사진 찍어 저장하기 (0) | 2019.08.29 |