package com.example.myfirst;
import java.io.File;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
private MediaPlayer player;
private boolean isPause =false;
private File file;
private TextView hint;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button1=(Button)findViewById(R.id.button1);
final Button button2=(Button)findViewById(R.id.button2);
final Button button3=(Button)findViewById(R.id.button3);
hint=(TextView)findViewById(R.id.hint);
file=new File(“/sdcard/ninan.mp3”);
if(file.exists()){
player=MediaPlayer.create(this, Uri.parse(file.getAbsolutePath()));
}else{
hint.setText(“音频文件不存在”);
button1.setEnabled(false);
return;
}
player.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
play();
}
});
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
play();
if(isPause){
button2.setText(“暂停”);
isPause=false;
}
button2.setEnabled(true);
button3.setEnabled(true);
button1.setEnabled(false);
}
});
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(player.isPlaying()&&!isPause){
player.pause();
isPause=true;
((Button)v).setText(“继续”);
hint.setText(“暂停播放音频。。。。。”);
button1.setEnabled(true);
}else{
player.start();
((Button)v).setText(“暂停”);
hint.setText(“继续播放音频。。。”);
isPause=false;
button1.setEnabled(false);
}
}
});
button3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
player.stop();
hint.setText(“停止播放。。。”);
button2.setEnabled(false);
button3.setEnabled(false);
button1.setEnabled(true);
}
});
}
private void play(){
try{
player.reset();
player.setDataSource(file.getAbsolutePath());
player.prepare();
player.start();
hint.setText(“正在播放音频”);
}catch(Exception e){
e.printStackTrace();
}
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
if(player.isPlaying()){
player.stop();
}
player.release();
super.onDestroy();
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
}
@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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
还没有评论,来说两句吧...