![]()
![]()
![]()
![]()
package com.example.sample3_2;
import java.io.IOException;
import com.example.sample3_2.R;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MyActivity extends Activity {//Activity创建时调用
private Button bPlay; //播放按钮
private Button bPause; //暂停按钮
private Button bStop; //停止按钮
private Button bAdd; //增大音量
private Button bReduce; //降低音量
private boolean pauseFlag=false; //暂停标记,true暂停态,false非暂停态
MediaPlayer mp; //MediaPlayer引用
AudioManager am; //AudioManager引用
@Override
protected void onCreate(Bundle savedInstanceState) {//Activity创建时调用
super.onCreate(savedInstanceState);
setContentView(R.layout.main); //设置Activity的显示内容
bPlay = (Button) findViewById(R.id.ButtonPlay); //播放按钮的实例化
bPause = (Button) findViewById(R.id.ButtonPause); //暂停按钮的实例化
bStop = (Button) findViewById(R.id.ButtonStop); //停止音量按钮的实例化
bAdd = (Button) findViewById(R.id.ButtonAdd); //增大音量按钮的实例化
bReduce = (Button) findViewById(R.id.ButtonReduce); //降低音量按钮的实例化
mp = new MediaPlayer();
am = (AudioManager) this.getSystemService(this.AUDIO_SERVICE);
bPlay.setOnClickListener(new View.OnClickListener() {//播放按钮的监听器
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
mp.setDataSource("/sdcard/dl.mid");//加载音频进入Initialized状态
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
mp.prepare(); //进入Prepared状态
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mp.start(); //播放音乐
Toast.makeText(MyActivity.this, "播放音乐", Toast.LENGTH_SHORT).show();
}
});
bPause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(mp.isPlaying()){ //如果是在播放状态
mp.pause(); //调用暂停方法
pauseFlag = true; //设置暂停标记
}else if(pauseFlag){
mp.start(); //播放音乐
pauseFlag = false; //设置暂停标记
Toast.makeText(MyActivity.this, "暂停播放", Toast.LENGTH_SHORT).show();
}
}
});
bStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mp.stop(); //停止播放
mp.reset(); //重置状态到uninitialized状态
try {
mp.setDataSource("/sdcard/dl.mid"); //加载音频进入Initialized状态
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
mp.prepare(); //进入Prepared状态
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Toast.makeText(MyActivity.this, "停止播放", Toast.LENGTH_SHORT).show();
}
});
bAdd.setOnClickListener(new View.OnClickListener() { //音量增大按钮添加监听器
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
am.adjustVolume(AudioManager.ADJUST_RAISE, 0); //增大音量
System.out.println("faaa");
Toast.makeText(MyActivity.this, "增大音量", Toast.LENGTH_SHORT).show();
}
});
bReduce.setOnClickListener(new View.OnClickListener() { //音量降低按钮添加监听器
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
am.adjustVolume(AudioManager.ADJUST_LOWER, 0); //减小音量
Toast.makeText(MyActivity.this, "减小音量", Toast.LENGTH_SHORT).show();
}
});
}
/* @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
*/
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sample3_2"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/icon"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.sample3_2.MyActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:text="播放音乐"
android:id="@+id/ButtonPlay"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button> <!-- 播放音乐按钮 -->
<Button
android:text="暂停音乐"
android:id="@+id/ButtonPause"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button> <!-- 暂停音乐按钮 -->
<Button
android:text="停止音乐"
android:id="@+id/ButtonStop"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button> <!-- 停止音乐按钮 -->
<Button
android:text="增大音乐"
android:id="@+id/ButtonAdd"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button> <!-- 增大音乐按钮 -->
<Button
android:text="降低音乐"
android:id="@+id/ButtonReduce"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button> <!-- 降低音乐按钮 -->
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Sample3_2</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
</resources>