 |
|
|  |
¾Èµå·ÎÀÌµå ¹Ìµð¾î Ç÷¹À̾î (MediaPlayer) ¿¹Á¦ |
|
|
 |
8³â Àü |
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
|
|
̵̧ : 850 |
̵̧
¸ñ·Ï
|
|
|  |
|