회원로그인
안드로이드 미디어 플레이어 (MediaPlayer) 예제
10년 전
SeekBar + Thread 사용하여 미디어 플레이 하기를 구현한 예제입니다
[시작] 버튼, [일지정지] 버튼, [재시작] 버튼, [정지] 버튼 의 기능도 구현해 봅니다.
1. 미디어 파일 (첨부) 준비, raw 폴더 밑에 별도의 폴더 만들어 복사-붙여넣기로 끌어옵니다.
2. MediaPlayer 객체, SeekBar 객체 생성
3. Start를 누르면 MediaPlayer.create() 로 MediaPlayer 객체 생성 (이때 raw 에 복사한 미디어 파일 지정, 소스에서는 R.raw.chacha). 그리고 start() 메소드로 연주 시작. 이때 SeekBar의 쓰레드를 start() 시켜 줍니다.
4. SeekBar.setOnSeekBarChangeListener() 구현하여 임의의 지점에서 플레이 가능케 함
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="MediaPlayer 활용"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:text="시작" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:visibility="invisible"
android:text="일시정지" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button2"
android:layout_below="@+id/button2"
android:visibility="invisible"
android:text="재시작" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button3"
android:layout_below="@+id/button3"
android:visibility="invisible"
android:text="종료" />
<SeekBar
android:id="@+id/seekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button4"
android:layout_below="@+id/button4"
android:layout_marginTop="18dp" />
</RelativeLayout>
메인액티비티소스
public class MainActivity extends ActionBarActivity {
MediaPlayer mp; // 음악 재생을 위한 객체
int pos; // 재생 멈춘 시점
private Button bStart;
private Button bPause;
private Button bRestart;
private Button bStop;
SeekBar sb; // 음악 재생위치를 나타내는 시크바
boolean isPlaying = false; // 재생중인지 확인할 변수
class MyThread extends Thread {
@Override
public void run() { // 쓰레드가 시작되면 콜백되는 메서드
// 씨크바 막대기 조금씩 움직이기 (노래 끝날 때까지 반복)
while(isPlaying) {
sb.setProgress(mp.getCurrentPosition());
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sb = (SeekBar)findViewById(R.id.seekBar1);
sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
public void onStopTrackingTouch(SeekBar seekBar) {
isPlaying = true;
int ttt = seekBar.getProgress(); // 사용자가 움직여놓은 위치
mp.seekTo(ttt);
mp.start();
new MyThread().start();
}
public void onStartTrackingTouch(SeekBar seekBar) {
isPlaying = false;
mp.pause();
}
public void onProgressChanged(SeekBar seekBar,int progress,boolean fromUser) {
if (seekBar.getMax()==progress) {
bStart.setVisibility(View.VISIBLE);
bStop.setVisibility(View.INVISIBLE);
bPause.setVisibility(View.INVISIBLE);
bRestart.setVisibility(View.INVISIBLE);
isPlaying = false;
mp.stop();
}
}
});
bStart = (Button)findViewById(R.id.button1);
bPause = (Button)findViewById(R.id.button2);
bRestart=(Button)findViewById(R.id.button3);
bStop = (Button)findViewById(R.id.button4);
bStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// MediaPlayer 객체 초기화 , 재생
mp = MediaPlayer.create(
getApplicationContext(), // 현재 화면의 제어권자
R.raw.chacha); // 음악파일
mp.setLooping(false); // true:무한반복
mp.start(); // 노래 재생 시작
int a = mp.getDuration(); // 노래의 재생시간(miliSecond)
sb.setMax(a);// 씨크바의 최대 범위를 노래의 재생시간으로 설정
new MyThread().start(); // 씨크바 그려줄 쓰레드 시작
isPlaying = true; // 씨크바 쓰레드 반복 하도록
bStart.setVisibility(View.INVISIBLE);
bStop.setVisibility(View.VISIBLE);
bPause.setVisibility(View.VISIBLE);
}
});
bStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 음악 종료
isPlaying = false; // 쓰레드 종료
mp.stop(); // 멈춤
mp.release(); // 자원 해제
bStart.setVisibility(View.VISIBLE);
bPause.setVisibility(View.INVISIBLE);
bRestart.setVisibility(View.INVISIBLE);
bStop.setVisibility(View.INVISIBLE);
sb.setProgress(0); // 씨크바 초기화
}
});
bPause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 일시중지
pos = mp.getCurrentPosition();
mp.pause(); // 일시중지
bPause.setVisibility(View.INVISIBLE);
bRestart.setVisibility(View.VISIBLE);
isPlaying = false; // 쓰레드 정지
}
});
bRestart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 멈춘 지점부터 재시작
mp.seekTo(pos); // 일시정지 시점으로 이동
mp.start(); // 시작
bRestart.setVisibility(View.INVISIBLE);
bPause.setVisibility(View.VISIBLE);
isPlaying = true; // 재생하도록 flag 변경
new MyThread().start(); // 쓰레드 시작
}
});
} // end of onCreate
@Override
protected void onPause() {
super.onPause();
isPlaying = false; // 쓰레드 정지
if (mp!=null) {
mp.release(); // 자원해제
}
bStart.setVisibility(View.VISIBLE);
bStop.setVisibility(View.INVISIBLE);
bPause.setVisibility(View.INVISIBLE);
bRestart.setVisibility(View.INVISIBLE);
}
} // end of class
추천 : 1001
추천
목록