mp3播放器
1、视图
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" 6 tools:context=".MainActivity" > 7 8 <EditText 9 android:id="@+id/et_path" 10 android:hint="请输入音频文件" 11 android:layout_width="fill_parent" 12 android:layout_height="wrap_content" 13 android:text="/sdcard/ss.mp3" 14 /> 15 <LinearLayout 16 android:layout_width="fill_parent" 17 android:layout_height="wrap_content" 18 android:orientation="horizontal" 19 > 20 <Button 21 android:id="@+id/bt_play" 22 android:layout_weight="1" 23 android:layout_width="0dip" 24 android:layout_height="wrap_content" 25 android:text="播放" 26 /> 27 <Button 28 android:id="@+id/bt_pause" 29 android:layout_weight="1" 30 android:layout_width="0dip" 31 android:layout_height="wrap_content" 32 android:text="暂停" 33 /> 34 <Button 35 android:id="@+id/bt_replay" 36 android:layout_weight="1" 37 android:layout_width="0dip" 38 android:layout_height="wrap_content" 39 android:text="重播" 40 /> 41 <Button 42 android:id="@+id/bt_stop" 43 android:layout_weight="1" 44 android:layout_width="0dip" 45 android:layout_height="wrap_content" 46 android:text="停止" 47 /> 48 49 </LinearLayout> 50 51 </LinearLayout>
3、播放java代码
1 package com.example.mp3player; 2 3 import java.io.File; 4 import java.io.IOException; 5 6 import android.media.AudioManager; 7 import android.media.MediaPlayer; 8 import android.media.MediaPlayer.OnCompletionListener; 9 import android.os.Bundle; 10 import android.app.Activity; 11 import android.view.Menu; 12 import android.view.View; 13 import android.view.View.OnClickListener; 14 import android.widget.Button; 15 import android.widget.EditText; 16 import android.widget.Toast; 17 18 public class MainActivity extends Activity implements OnClickListener { 19 private EditText et_path; 20 private Button bt_play,bt_pause,bt_replay,bt_stop; 21 private MediaPlayer mediaPlayer; 22 @Override 23 protected void onCreate(Bundle savedInstanceState) { 24 super.onCreate(savedInstanceState); 25 setContentView(R.layout.activity_main); 26 27 et_path = (EditText) findViewById(R.id.et_path); 28 bt_play = (Button) findViewById(R.id.bt_play); 29 bt_pause = (Button) findViewById(R.id.bt_pause); 30 bt_replay = (Button) findViewById(R.id.bt_replay); 31 bt_stop = (Button) findViewById(R.id.bt_stop); 32 33 bt_play.setOnClickListener(this); 34 bt_pause.setOnClickListener(this); 35 bt_replay.setOnClickListener(this); 36 bt_stop.setOnClickListener(this); 37 38 } 39 40 41 @Override 42 public void onClick(View v) { 43 switch(v.getId()){ 44 case R.id.bt_play: 45 play(); 46 break; 47 case R.id.bt_pause: 48 pause(); 49 break; 50 case R.id.bt_replay: 51 replay(); 52 break; 53 case R.id.bt_stop: 54 stop(); 55 break; 56 } 57 58 } 59 60 //暂停 61 private void pause() { 62 if("继续".equals(bt_pause.getText().toString().trim())){ 63 mediaPlayer.start(); 64 bt_pause.setText("暂停"); 65 return; 66 } 67 if(mediaPlayer != null && mediaPlayer.isPlaying()){ 68 mediaPlayer.pause();//暂停 69 bt_pause.setText("继续"); 70 return; 71 } 72 73 } 74 75 //重播 76 private void replay() { 77 if(mediaPlayer != null && mediaPlayer.isPlaying()){ 78 mediaPlayer.seekTo(0);//把进度调为0 79 return; 80 } 81 play(); 82 83 } 84 85 //停止播放音乐 86 private void stop() { 87 // TODO Auto-generated method stub 88 if(mediaPlayer != null && mediaPlayer.isPlaying()){ 89 mediaPlayer.stop(); 90 mediaPlayer.release(); 91 mediaPlayer = null; 92 bt_play.setEnabled(true); 93 } 94 } 95 96 97 private void play(){ 98 String path = et_path.getText().toString().trim(); 99 File file = new File(path); 100 if(file.exists() && file.length() > 0){ 101 try { 102 mediaPlayer = new MediaPlayer(); 103 mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);//设置音频流类型 104 mediaPlayer.setDataSource(path);//设置音频源 105 mediaPlayer.prepare(); 106 mediaPlayer.start(); 107 //监听是否播放完 108 mediaPlayer.setOnCompletionListener(new OnCompletionListener() { 109 110 @Override 111 public void onCompletion(MediaPlayer mp) { 112 // TODO Auto-generated method stub 113 bt_play.setEnabled(true); 114 115 } 116 }); 117 bt_play.setEnabled(false);//设置为不可用 118 } catch (Exception e) { 119 // TODO Auto-generated catch block 120 Toast.makeText(this, "播放失败!", 0).show(); 121 e.printStackTrace(); 122 } 123 }else{ 124 Toast.makeText(this, "音频文件不存在!", 0).show(); 125 } 126 } 127 128 }

浙公网安备 33010602011771号