1.通过意图使用内置的音频播放器
Intent intent=new Intent(android.content.Intent.ACTION_VIEW);
File audioFile=new File(Environment.getExternalStorageDirectory()+"/Music/mylove.mp3");
intent.setDataAndType(Uri.fromFile(audioFile), "audio/mp3");
startActivity(intent);
2.创建自定义播放应用(在onStart中初始化MediaPlayer对象和开始播放,可以在活动不再活跃时,停止播放)
MediaPlayer mediaPlayer;
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
mediaPlayer = MediaPlayer.create(this, R.raw.life);
mediaPlayer.setOnCompletionListener(this);
mediaPlayer.start();
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
mediaPlayer.stop();
mediaPlayer.release();
}
@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
mediaPlayer.start();
}
package com.example.testaudioedit;
import java.io.File;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
public class AudioPlayer extends Activity implements OnClickListener,
OnCompletionListener, OnTouchListener {
private MediaPlayer mediaPlayer;
private Button startButton, stopButton;
private int position = 0;// 保存播放位置
private View musicProgress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
// TODO Auto-generated method stub
startButton = (Button) findViewById(R.id.internal_play);
stopButton = (Button) findViewById(R.id.internal_stop);
musicProgress = findViewById(R.id.musicView);
startButton.setOnClickListener(this);
stopButton.setOnClickListener(this);
musicProgress.setOnTouchListener(this);
mediaPlayer = MediaPlayer.create(this, R.raw.life);//资源文件,在资源文件夹下创建raw文件夹,life表示的是要播放的音频文件
mediaPlayer.setOnCompletionListener(this);
mediaPlayer.start();
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
mediaPlayer.stop();
mediaPlayer.release();
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.internal_play:
if (!mediaPlayer.isPlaying())
mediaPlayer.start();
break;
case R.id.internal_stop:
if (mediaPlayer.isPlaying())
mediaPlayer.pause();
break;
default:
break;
}
}
@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
mediaPlayer.start();
mediaPlayer.seekTo(position);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if (event.getAction() == MotionEvent.ACTION_MOVE) {
if (mediaPlayer.isPlaying()) {
position = (int) (event.getX() * mediaPlayer.getDuration() / musicProgress
.getWidth());
mediaPlayer.seekTo(position);
}
}
return true;
}
}
activity_main.xml文件如下:
<RelativeLayout 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">
<Button
android:id="@+id/internal_play"
android:layout_width="150dp"
android:layout_height="60dp"
android:layout_marginLeft="10dp"
android:text="play"/>
<Button
android:id="@+id/internal_stop"
android:layout_toRightOf="@+id/internal_play"
android:layout_width="150dp"
android:layout_height="60dp"
android:text="stop"/>
<View
android:id="@+id/musicView"
android:layout_below="@+id/internal_play"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>