android使用MediaPlayer播放音频

<?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" 
    android:background="@drawable/background02"
    
    >
   <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >


        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:text="@string/a"
            style="?android:attr/borderlessButtonStyle" 
            />


        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/b"
            style="?android:attr/borderlessButtonStyle" 
             />


        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:text="@string/c"
            style="?android:attr/borderlessButtonStyle" 
            />
         
    </LinearLayout>
 <TextView 
              android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:id="@+id/hint"
             />

</LinearLayout>

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);
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值