可以对音乐文件实现播放、暂停、重播和停止功能。退出应用和回到桌面时音乐停止。
主界面:
主界面配置文件mian.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.musicplay.MainActivity" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/filename" /> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="0.00" android:background="#B0C4DE" android:text="Payphone.mp3" android:id="@+id/filename" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/playbutton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="mediaplay" android:text="@string/playbutton" /> <Button android:id="@+id/pausebutton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="mediaplay" android:text="@string/pausebutton" /> <Button android:id="@+id/resetbutton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="mediaplay" android:text="@string/resetbutton" /> <Button android:id="@+id/stopbutton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="mediaplay" android:text="@string/stopbutton" /> </LinearLayout> </LinearLayout>
主界面的Activity
MainActivity.java:
package com.example.musicplay; import java.io.File; import android.app.Activity; import android.content.Context; import android.media.MediaPlayer; import android.media.MediaPlayer.OnPreparedListener; import android.os.Bundle; import android.os.Environment; import android.telephony.PhoneStateListener; import android.telephony.TelephonyManager; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { private EditText nameText; private String path; private int position; private MediaPlayer mediaplayer; private boolean pause; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); nameText=(EditText) this.findViewById(R.id.filename); mediaplayer=new MediaPlayer(); } //以下方法会造成只要应用在后台音乐都会停止播放 @Override //当应用不在前台时,停止播放 protected void onPause() { if(mediaplayer.isPlaying()){ position=mediaplayer.getCurrentPosition(); mediaplayer.stop(); } super.onPause(); } @Override protected void onResume() { if(position>0&&path!=null){ play(); mediaplayer.seekTo(position); position=0; } super.onResume(); } @Override protected void onDestroy() { mediaplayer.release(); mediaplayer=null; super.onDestroy(); } public void mediaplay(View v){ switch (v.getId()) { case R.id.playbutton: String filename=nameText.getText().toString(); //Environment.getExternalStorageDirectory()检查外部存储设备的可用性 File audio=new File(Environment.getExternalStorageDirectory(),filename); if(audio.exists()){ //获取SDCard目录,2.2的时候为:/mnt/sdcart 2.1的时候为:/sdcard,所以使用静态方法得到路径会好一点。 path=audio.getAbsolutePath(); play(); } else{ path=null; Toast.makeText(getApplicationContext(), R.string.error, 1).show(); } break; case R.id.pausebutton: if(mediaplayer.isPlaying()){ mediaplayer.pause(); pause=true; ((Button)v).setText(R.string.continues); }else{ if(pause){ mediaplayer.start(); pause=false; ((Button)v).setText(R.string.pausebutton); } } break; case R.id.resetbutton: if(mediaplayer.isPlaying()){ mediaplayer.seekTo(0);//从开始位置播放 }else{ if(path!=null){ play(); } } break; case R.id.stopbutton: if(mediaplayer.isPlaying()){ mediaplayer.stop(); } break; default: break; } } private void play() { try { mediaplayer.reset();//把各项参数恢复到初始化状态 mediaplayer.setDataSource(path); mediaplayer.prepare();//进行缓冲 //设置缓冲监听器 mediaplayer.setOnPreparedListener(new OnPreparedListener() { //缓冲完毕后调用onPrepared方法 public void onPrepared(MediaPlayer mp) { // 里面写缓冲完要干的事 mediaplayer.start(); } }); } catch (Exception e) { e.printStackTrace(); } } }
转载请注明出处!程序猿之洞:http://blog.csdn.net/acmman