<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/hint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="单击播放开始播放音乐" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="播放" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="暂停" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止" />
</LinearLayout>
</LinearLayout>
private MediaPlayer mp;//mediaPlayer对象 private Button play,pause,stop;//播放 暂停/继续 停止 按钮 private TextView hint;//显示当前播放状态 private boolean isPause=false;//是否暂停
play=(Button) findViewById(R.id.button1); pause=(Button) findViewById(R.id.button2); stop=(Button) findViewById(R.id.button3); hint=(TextView) findViewById(R.id.hint); hint.setTextSize(20); mp=MediaPlayer.create(MainActivity.this, R.raw.sound);//创建mediaplayer对象
private void play(){
try{
mp.reset();
mp=MediaPlayer.create(MainActivity.this, R.raw.sound);//重新设置要播放的音频
mp.start();//开始播放
hint.setText("正在播放音频...");
play.setEnabled(false);
pause.setEnabled(true);
stop.setEnabled(true);
}catch(Exception e){
e.printStackTrace();//输出异常信息
}
}
mp.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer arg0) {
// TODO Auto-generated method stub
play();//重新开始播放
}
});
play.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
play();
if(isPause){
pause.setText("暂停");
isPause=false;
}
}
});
pause.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(mp.isPlaying()&&!isPause){
mp.pause();
isPause=true;
pause.setText("继续");
hint.setText("暂停播放音频...");
play.setEnabled(true);
}else{
mp.start();
pause.setText("暂停");
hint.setText("继续播放音频...");
isPause=false;
play.setEnabled(false);
}
}
});
stop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mp.stop();
hint.setText("停止播放音频...");
pause.setEnabled(false);
stop.setEnabled(false);
play.setEnabled(true);
}
});
protected void onDestroy() {
// TODO Auto-generated method stub
if(mp.isPlaying()){
mp.stop();
}
mp.release();//释放资源
super.onDestroy();
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有