package com.tang.play; import java.io.File; import java.io.IOException; import android.app.Activity; import android.app.ActionBar; import android.app.Fragment; import android.drm.DrmStore.Playback; import android.media.AudioManager; import android.media.MediaPlayer; import android.media.MediaPlayer.OnCompletionListener; import android.media.MediaPlayer.OnErrorListener; import android.os.Bundle; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import android.os.Build; public class MainActivity extends Activity implements OnClickListener { private EditText et; private Button bt1; private Button bt2; private Button bt3; private Button bt4; private MediaPlayer mp; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et = (EditText) findViewById(R.id.et); bt1 = (Button) findViewById(R.id.bt1); bt2 = (Button) findViewById(R.id.bt2); bt3 = (Button) findViewById(R.id.bt3); bt4 = (Button) findViewById(R.id.bt4); bt1.setOnClickListener(this); bt2.setOnClickListener(this); bt3.setOnClickListener(this); bt4.setOnClickListener(this); } public void onClick(View v) { switch (v.getId()) { case R.id.bt1: play(); break; case R.id.bt2: pause(); break; case R.id.bt3: rePlay(); break; case R.id.bt4: stop(); break; } } /** * 中止 */ private void stop() { if (mp != null && mp.isPlaying()) { mp.start(); mp.release(); mp = null; bt1.setEnabled(true); return ; } } /** * 重複播放 */ private void rePlay() { if (mp != null && mp.isPlaying()) { mp.seekTo(0); return ; } play(); } /** * 暫停 */ private void pause() { if ("繼續".equals(bt2.getText().toString().trim())) { bt2.setText("暫停"); mp.start(); return; } if (mp != null && mp.isPlaying()) { mp.pause(); bt2.setText("繼續"); } } /** * 播放 */ private void play() { String path = et.getText().toString().trim(); File file = new File(path); if (file.exists() && file.length()>0) { mp = new MediaPlayer(); mp.setAudioStreamType(AudioManager.STREAM_MUSIC); try { mp.setDataSource(path); mp.prepare(); mp.start(); mp.setOnCompletionListener(new OnCompletionListener() { //播放完成的回調 public void onCompletion(MediaPlayer mediaPlay) { bt1.setEnabled(true); } }); mp.setOnErrorListener(new OnErrorListener() { @Override public boolean onError(MediaPlayer arg0, int arg1, int arg2) { bt1.setEnabled(true); return false; } }); bt1.setEnabled(false); } catch (Exception e) { Toast.makeText(this, "播放失敗", Toast.LENGTH_SHORT).show(); e.printStackTrace(); } } else { Toast.makeText(this, "文件不存在", Toast.LENGTH_SHORT).show(); } } }
##佈局文件java
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" > <EditText android:hint="請輸入mp3地址" android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/et" /> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:layout_weight="1" android:layout_height="wrap_content" android:layout_width="0dip" android:id="@+id/bt1" android:text="播放" /> <Button android:layout_weight="1" android:layout_height="wrap_content" android:layout_width="0dip" android:id="@+id/bt2" android:text="暫停" /> <Button android:layout_weight="1" android:layout_height="wrap_content" android:layout_width="0dip" android:id="@+id/bt3" android:text="重播" /> <Button android:layout_weight="1" android:layout_height="wrap_content" android:layout_width="0dip" android:id="@+id/bt4" android:text="中止" /> </LinearLayout> </LinearLayout>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
android