Android MediaPlayer实现音乐播放

时间:2022-06-03 19:44:09

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>


<EditText
android:id="@+id/edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Demons.mp3"/>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>

<Button
android:id="@+id/player"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始播放"/>

<Button
android:id="@+id/pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="暂停"/>

<Button
android:id="@+id/restrat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="重播"/>

<Button
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止"/>



</LinearLayout>

</LinearLayout>

MediaActivity 类

package com.sun.activity;

import java.io.File;
import java.io.IOException;

import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MediaActivity extends Activity implements OnClickListener{
private MediaPlayer mediaPlayer;
private EditText editText;
private Button buttonPause;
private int posi=0;//用于记录播放的位置
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Button buttonStrat=(Button) findViewById(R.id.player);
buttonPause=(Button) findViewById(R.id.pause);
Button buttonRestrat=(Button) findViewById(R.id.restrat);
Button buttonStop=(Button) findViewById(R.id.stop);
editText=(EditText) findViewById(R.id.edit);
//添加单击事件
buttonStrat.setOnClickListener(this);
buttonPause.setOnClickListener(this);
buttonRestrat.setOnClickListener(this);
buttonStop.setOnClickListener(this);
//创建播放器对象
mediaPlayer= new MediaPlayer();

}



/*@Override
protected void onPause() {
super.onPause();
if(mediaPlayer.isPlaying()){
//用于记录播放的位置
posi=mediaPlayer.getCurrentPosition();
mediaPlayer.pause();
}
}



@Override
protected void onStart() {
super.onStart();

if(posi!=0){
mediaPlayer.seekTo(posi);
mediaPlayer.start();
}

}
*/



@Override
public void onClick(View v) {
switch(v.getId())
{

case R.id.player://播放

play();
break;
case R.id.pause://暂停
if(mediaPlayer.isPlaying())
{
//用于记录播放的位置
posi=mediaPlayer.getCurrentPosition();
mediaPlayer.pause();
buttonPause.setText("继续播放");
}else{
mediaPlayer.seekTo(posi);
mediaPlayer.start();
buttonPause.setText("暂停");
}

break;
case R.id.restrat://重播

if(mediaPlayer.isPlaying())
{

mediaPlayer.seekTo(0);
}else{
play();
}

break;
case R.id.stop://停止
if(mediaPlayer.isPlaying())


mediaPlayer.stop();
break;
}
}
public void play()
{
String mp3Name=editText.getText().toString();
String path=Environment.getExternalStorageDirectory().toString();
File file=new File(Environment.getExternalStorageDirectory(), mp3Name);
if(file.exists())
{
try {
//告诉mediaplayer播放文件的位置
mediaPlayer.setDataSource(file.getPath());
//加载缓存
mediaPlayer.prepare();
//用于监听是否缓存完成
mediaPlayer.setOnPreparedListener(new Prepared());
} catch (Exception e) {
e.printStackTrace();
}
}else{
Toast.makeText(this, "没有该文件", 1).show();
}
}


class Prepared implements OnPreparedListener{

//缓存完毕后会调用onPrepared
public void onPrepared(MediaPlayer mp) {
mediaPlayer.start();
}

}


}