您的位置:首页 > 移动开发 > Android开发

android使用MediaPlayer播放音频

2014-08-17 11:00 471 查看
<?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);

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: