SimpleAudioTest
문제
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".MainActivity">
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#8BC34A"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_marginBottom="50dp"
android:orientation="horizontal">
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10px"
android:layout_weight="1"
android:text="듣기" />
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10px"
android:layout_weight="1"
android:text="일시정지" />
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10px"
android:layout_weight="1"
android:text="중지" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="0dp">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="실행중인 음악: " />
<ProgressBar
android:id="@+id/pb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible" />
</LinearLayout>
</RelativeLayout>
MainActivity.java
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.Manifest;
import android.content.pm.PackageManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ListView listView;
Button Play, Pause, Stop;
TextView textView;
ProgressBar pb;
ArrayList<String> mp3List;
String selected;
String Path = "/sdcard/";
MediaPlayer Player;
boolean PAUSED = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mp3List = new ArrayList<String>();
//File[] listFiles() : 디렉토리의 파일목록(디렉토리 포함)을 File배열로 반환한다.
File[] listFiles = new File(Path).listFiles();
String fileName, extName;
for (File listFile : listFiles) {
fileName = listFile.getName();
//substring은 해당 인덱스부터 끝까지 추출
extName = fileName.substring(fileName.length() - 3);
if (extName.equals(("mp3")))
mp3List.add(fileName);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice, mp3List);
listView = (ListView) findViewById(R.id.list);
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
listView.setAdapter(adapter);
listView.setItemChecked(0, true);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> adapterView, View v, int position, long i) {
selected = mp3List.get(position);
}
});
//selected = mp3List.get(0);
Play = (Button) findViewById(R.id.button1);
Pause = (Button) findViewById(R.id.button2);
Stop = (Button) findViewById(R.id.button3);
textView = (TextView) findViewById(R.id.textView);
pb = (ProgressBar) findViewById(R.id.pb);
Play.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
Player = new MediaPlayer();
Player.setDataSource(Path + selected);
Player.prepare();
Player.start();
Play.setClickable(false);
Pause.setClickable(true);
Stop.setClickable(true);
textView.setText("실행중인 음악 : " + selected);
pb.setVisibility(View.VISIBLE);
} catch (IOException e) {
}
}
});
Pause.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (PAUSED == false) {
Player.pause();
Pause.setText("이어듣기");
PAUSED = true;
pb.setVisibility(View.INVISIBLE);
} else {
Player.start();
PAUSED = false;
Pause.setText("일시정지");
pb.setVisibility(View.VISIBLE);
}
}
});
Pause.setClickable(false);
Stop.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Player.stop();
Player.reset();
Play.setClickable(true);
Pause.setClickable(false);
Pause.setText("일시정지");
Stop.setClickable(false);
textView.setText("실행중인 음악 : ");
pb.setVisibility(View.INVISIBLE);
}
});
Stop.setClickable(false);
}
}