536
No
class MainActivity
package kr.co.braverokmc.audioplayer;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
private static final String TAG="MainActivity";
MediaPlayer player;
String url ="https://sites.google.com/site/ubiaccessmobile/sample_audio.amr";
Toast toast;
int position; // 일시정지시 현재 위치 저장
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//시작
public void onButton1Clicked(View v){
Log.i(TAG, "시작 버튼 클릭 됨");
try {
killPlayer();
player =new MediaPlayer();
player.setDataSource(url);
player.prepare();
player.start();
} catch (IOException e) {
e.printStackTrace();
}
toastShow("재생을 시작합니다.");
}
private void killPlayer(){
if(player!=null){
player.release(); //리소스를 전부 해제
player=null;
}
}
//일시정지
public void onButton2Clicked(View v){
Log.i(TAG, "일시정지 클릭 됨");
if(player!=null && player.isPlaying()){
position=player.getCurrentPosition();
player.pause();
toastShow("재생을 일시정지합니다.");
}
}
//재시작
public void onButton3Clicked(View v){
Log.i(TAG, "재시작 클릭 됨");
if(player!=null && !player.isPlaying()){
player.start();
player.seekTo(position);// 정지 위치 찾아가기
toastShow("재생을 재시작합니다.");
}
}
//중지
public void onButton4Clicked(View v){
Log.i(TAG, "중지 버튼 클릭 됨");
if(player!=null && player.isPlaying()){
player.stop();
}
toastShow("재생을 중지합니다.");
}
private void toastShow(String text){
if(toast==null){
toast=Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT);
}else {
toast.setText(text);
}
toast.show();
}
@Override
protected void onDestroy() {
super.onDestroy();
killPlayer();
}
}
R.layout.activity_main
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="kr.co.braverokmc.audioplayer.MainActivity">
<Button
android:text="시작"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="116dp"
android:layout_marginStart="116dp"
android:id="@+id/button1"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:onClick="onButton1Clicked"
/>
<Button
android:text="일시정지"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="28dp"
android:id="@+id/button2"
android:layout_below="@+id/button1"
android:layout_alignLeft="@+id/button1"
android:layout_alignStart="@+id/button1"
android:onClick="onButton2Clicked"
/>
<Button
android:text="재시작"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="35dp"
android:id="@+id/button3"
android:layout_below="@+id/button2"
android:layout_alignLeft="@+id/button2"
android:layout_alignStart="@+id/button2"
android:onClick="onButton3Clicked"
/>
<Button
android:text="중지"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button3"
android:layout_alignRight="@+id/button3"
android:layout_alignEnd="@+id/button3"
android:layout_marginTop="49dp"
android:id="@+id/button4"
android:onClick="onButton4Clicked"
/>
</RelativeLayout>
AndroidMainfest.xml
<uses-permission android:name="android.permission.INTERNET"/>
111강
112강
댓글 ( 4)
댓글 남기기