解决方法:
1. 在res/layout中的activity_main.xml添加一个按钮,命名为button1。
2. 在MainActivity.java中添加按钮事件绑定:
View btn1 = this.findViewById(R.id.button1);
btn1.setOnClickListener(new ButtonClickListener());
3. 重写ButtonClickListener函数:
private class ButtonClickListener implements View.OnClickListener{
public void onClick(View v) {
if(R.id.button1 == v.getId())
{
MediaPlayer mediaPlayer01;
(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/android/)mediaPlayer01 = MediaPlayer.create(getBaseContext(), R.raw.ring);
mediaPlayer01.start();
}
}
}
在音乐播放器中,播放模式一般放在音乐播放完毕后自动识别,如下给出逻辑代码,可根据需要添加合适的代码
public static final int ALL = 0;//全部循环
public static final int SINGLE = 1;//单曲循环
public static final int RANDOM = 2;//随机
// 一首歌播放完成后(这里设置播放模式)
public class Completed implements OnCompletionListener
{
@Override
public void onCompletion(MediaPlayer mp)
{
if(playMode == ALL)//全部循环
{
if (position == listMusic.size() - 1)//默认循环播放
{
position = 0;// 第一首
}
else
{
position++;
}
}
else if(playMode TuLaoShi.com== SINGLE)//单曲循环
(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/android/){
//position不需要更改
}
else if(playMode == RANDOM)//随机
{
position = (int)(Math.random()*listMusic.size());//随机播放
}
elseSet();//其他播放的处理
}
}
制作单曲循环时,最好不要像如上代码所设计,由于没有给出实际的position,程序可能跳出BUG,甚至崩溃,我所实现的单曲循环是监听用户的模式选择,当音乐播放完毕之后,重新启动MediaPlayer就可以了
main.xml
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="fill_parent"android:layout_height="fill_parent"android:gravity="center_horizontal"android:orientation="vertical"Buttonandroid:id="@+id/listDialogButton"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/listDialog_button" android:layout_marginTop="100dip"android:textSize="20sp" /Buttonandroid:id="@+id/progressDialogButton"android:layout_width="wrap_content"android:layout_height=(图老师整理)"wrap_content"android:text="@string/progressDialog_button" android:layout_marginTop="100dip"android:textSize="20sp" //LinearLayout
MainActivity
import android.app.Activity;import android.app.AlertDialog.Builder;import android.app.Dialog;import android.app.ProgressDialog;import android.content.DialogInterface;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.Toast;public class MainActivity extends Activity {private Button mListButton;private Button mProgressButton;private Dialog mListDialog;private ProgressDialog mProgressDialog;private int currentProgress=0;private int maxProgress=100;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);init();}private void init(){mListButton=(Button) findViewById(R.id.listDialogButton);mListButton.setOnClickListener(new ButtonOnClickListenerImpl());mProgressButton=(Button) findViewById(R.id.progressDialogButton);mProgressButton.setOnClickListener(new ButtonOnClickListenerImpl());}private class ButtonOnClickListenerImpl implements OnClickListener {@Overridepublic void onClick(View view) {switch (view.getId()) {case R.id.listDialogButton: Builder listBuilder=new Builder(MainActivity.this); listBuilder.setIcon(R.drawable.ic_launcher); listBuilder.setTitle(getResources().getString(R.string.list_dialog_title)); listBuilder.setItems(getResources().getStringArray(R.array.listDialogArray), new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {String selected=getResources().getStringArray(R.array.listDialogArray)[which];Toast.makeText(MainActivity.this,selected, Toast.LENGTH_SHORT).show();}}); mListDialog=listBuilder.create(); mListDialog.show(); break;case R.id.progressDialogButton:mProgressDialog=new ProgressDialog(MainActivity.this);mProgressDialog.setMax(maxProgress);mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); mProgressDialog.setTitle(R.string.progress_dialog_title);mProgressDialog.setIcon(R.drawable.ic_launcher);mProgressDialog.setMessage(getResources().getString(R.string.progress_dialog_message));mProgressDialog.setCancelable(false); mProgressDialog.show(); new Thread(){ public void run() { try { while(currentProgress=maxProgress){ mProgressDialog.setProgress(currentProgress++); Thread.sleep(500); } mProgressDialog.cancel();} catch (Exception e) {mProgressDialog.cancel();} }; }.start(); break;default:break;}}}}