音乐播放:
音频资源文件: /sdcard/filename
MediaPlayer mp = new MediaPlayer();
AudioManager am = (AudioManager)this.getSystemService(this.AUDIO_SERVICE);
private boolean pauseFlag = false;
音乐播放:
try{mp.setDataSource("/sdcard/filename");} catch(Exception e){e.printStackTrace();}
try{mp.prepare();} catch(Exception e){e.printStackTrace();}
mp.start();
音乐暂停:
if(mp.isPlaying()){
mp.pause();
pauseFlag = true;
} else if(pauseFlag){
mp.start();
pauseFlag = false;
}
音乐停止:
mp.stop();
mp.reset();
try{mp.setDataSource("/sdcard/filename");} catch(Exception e){e.printStackTrace();}
try{mp.prepare();} catch(Exception e){e.printStackTrace();}
音量增加:
am.adjustVolume(AudioManager.ADJUST_RAISE,0);
音量减小:
am.adjustVolume(AudioManager.ADJUST_LOWER,0);
代码如下:
/***************** TestMusic.java *****************************************/
package com.litsoft;
import java.io.IOException;
import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class TestMusic extends Activity {
private MediaPlayer mp;
private AudioManager am;
private Button bPlay;
private Button bPause;
private Button bStop;
private Button bUp;
private Button bDown;
private boolean pauseFlag = false;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mp = new MediaPlayer();
am = (AudioManager)this.getSystemService(this.AUDIO_SERVICE);
bPlay = (Button)findViewById(R.id.button01);
bPause = (Button)findViewById(R.id.button02);
bStop = (Button)findViewById(R.id.button03);
bUp = (Button)findViewById(R.id.button04);
bDown = (Button)findViewById(R.id.button05);
bPlay.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
try{
mp.setDataSource("/sdcard/perfict.mp3");
} catch(Exception e){
e.printStackTrace();
}
try{
mp.prepare();
} catch(Exception e){
e.printStackTrace();
}
mp.start();
}
});
bPause.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(mp.isPlaying()){
mp.pause();
pauseFlag = true;
} else if(pauseFlag){
mp.start();
pauseFlag = false;
}
}
});
bStop.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
mp.stop();
mp.reset();
try{
mp.setDataSource("/sdcard/perfict.mp3");
} catch(Exception e){
e.printStackTrace();
}
try{
mp.prepare();
} catch(Exception e){
e.printStackTrace();
}
}
});
bUp.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
am.adjustVolume(AudioManager.ADJUST_RAISE, 0);
}
});
bDown.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
am.adjustVolume(AudioManager.ADJUST_LOWER, 0);
}
});
}
}
/********************* 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">
<Button android:id="@+id/button01" android:text="播放"
android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>
<Button android:id="@+id/button02" android:text="暂停"
android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>
<Button android:id="@+id/button03" android:text="停止"
android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>
<Button android:id="@+id/button04" android:text="音量增大"
android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>
<Button android:id="@+id/button05" android:text="音量减小"
android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>
</LinearLayout>