<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" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="62dp" android:layout_marginTop="89dp" android:text="Play" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="34dp" android:text="TextView" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/button1" android:layout_alignBottom="@+id/button1" android:layout_toRightOf="@+id/button1" android:text="Pause" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/button2" android:layout_alignBottom="@+id/button2" android:layout_toRightOf="@+id/button2" android:text="Stop" /> <SeekBar android:id="@+id/seekBar1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_above="@+id/button2" android:layout_alignParentLeft="true" /> </RelativeLayout>
package com.myapp; import java.io.IOException; import android.media.MediaPlayer; import android.os.Bundle; import android.os.Handler; import android.app.Activity; import android.view.Menu; import android.view.View; import android.view.View.OnTouchListener; import android.widget.Button; import android.view.MotionEvent; import android.widget.SeekBar; import android.widget.TextView; public class MainActivity extends Activity { private MediaPlayer mMedia; private Handler handler = new Handler(); private SeekBar seekBar; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if(mMedia != null){ mMedia.release(); } final TextView txtView = (TextView)findViewById(R.id.textView1); txtView.setText("Source : music.mp3"); /* Resource in R. * mMedia = MediaPlayer.create(this, R.raw.music); * mMedia.start(); */ /* * from DataSource * mMedia = new MediaPlayer(); * mMedia.setDataSource("https://www.thaicreate.com/music/mymusic.mp3"); * mMedia.start(); * */ mMedia = MediaPlayer.create(this, R.raw.music); seekBar = (SeekBar)findViewById(R.id.seekBar1); seekBar.setMax(mMedia.getDuration()); seekBar.setOnTouchListener(new OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { UpdateseekChange(v); return false; } }); final Button btn1 = (Button) findViewById(R.id.button1); // Start final Button btn2 = (Button) findViewById(R.id.button2); // Pause final Button btn3 = (Button) findViewById(R.id.button3); // Stop // Start btn1.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { txtView.setText("Playing : music.mp3...."); mMedia.start(); startPlayProgressUpdater(); btn1.setEnabled(false); btn2.setEnabled(true); btn3.setEnabled(true); } }); // Pause btn2.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { txtView.setText("Pause : music.mp3"); mMedia.pause(); btn1.setEnabled(true); btn2.setEnabled(false); btn3.setEnabled(false); } }); // Stop btn3.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { txtView.setText("Stop Play : music.mp3"); mMedia.stop(); btn1.setEnabled(true); btn2.setEnabled(false); btn3.setEnabled(false); try { mMedia.prepare(); mMedia.seekTo(0); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); } private void UpdateseekChange(View v){ if(mMedia.isPlaying()){ SeekBar sb = (SeekBar)v; mMedia.seekTo(sb.getProgress()); } } public void startPlayProgressUpdater() { seekBar.setProgress(mMedia.getCurrentPosition()); if (mMedia.isPlaying()) { Runnable notification = new Runnable() { public void run() { startPlayProgressUpdater(); } }; handler.postDelayed(notification,1000); } } @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); if(mMedia != null){ mMedia.release(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท