你的分享就是我们的动力 ---﹥

MediaPlayer播放音频

时间:2013-06-05 15:26来源:www.chengxuyuans.com 点击:
package com.mediaPlayer;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.annotation.SuppressLint;
import android.app.Activity;

public class MainActivity extends Activity implements OnClickListener{

private Button start ,pause,stop;
private MediaPlayer player;
@SuppressLint("SdCardPath")
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);


start=(Button) findViewById(R.id.start);
pause=(Button) findViewById(R.id.pause);
stop=(Button) findViewById(R.id.stop);

start.setOnClickListener(this);
pause.setOnClickListener(this);
stop.setOnClickListener(this);


//获取raw资源目录下的音频文件
player=MediaPlayer.create(this,R.raw.bloom );

//获取sd卡上的音频文件
// String path="/mnt/sdcard/bloom.mp3";
// Uri uri=Uri.parse(path);
// player=MediaPlayer.create(this, uri);
}

public void onClick(View v) {
switch (v.getId()) {
case R.id.start:
player.start();
break;
case R.id.pause:
//判断文件是否在暂停
if(player.isPlaying()){
player.pause();
}
break;
case R.id.stop:
if(player.isPlaying()){
player.stop();
}
break;

default:
break;
}
}



}
(文:程序员之家)

转载注明地址:http://www.chengxuyuans.com/Android/63938.html