音乐播放器组件
Posted storm47
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了音乐播放器组件相关的知识,希望对你有一定的参考价值。
package com.hanqi.musicplay; import android.media.MediaPlayer; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.widget.ImageButton; import android.widget.ImageView; import android.widget.ProgressBar; import android.widget.TextView; public class MainActivity extends AppCompatActivity { ProgressBar pb; TextView tv1; MediaPlayer mediaPlayer; int i; ImageView iv; ImageButton ib; //Runnable r ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); pb=(ProgressBar)findViewById(R.id.pb); tv1=(TextView)findViewById(R.id.tv); iv=(ImageView)findViewById(R.id.iv); ib=(ImageButton)findViewById(R.id.ib); final int ii[] = {R.drawable.a,R.drawable.aa,R.drawable.aaa,}; final Handler handler=new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); if (msg.what==1&&i==0){ iv.setImageResource(ii[msg.arg1]); } } }; //图片轮播 分线程 new Thread(new Runnable() { @Override public void run() { for (int i=0;i<ii.length;i++){ //iv.setImageResource(ii[i]); Message message=new Message(); message.what=1; message.arg1=i; handler.sendMessage(message); if (i==ii.length-1){ i=-1; } try { Thread.sleep(1000); } catch (Exception e){ } } } }).start(); } public void b1(final View view){ if (mediaPlayer==null){ mediaPlayer=MediaPlayer.create(this,R.raw.blue); } //播放完成的监听 mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { mediaPlayer.reset(); mediaPlayer.release(); mediaPlayer = null; } }); i=0; //歌曲最大时间 pb.setMax(mediaPlayer.getDuration()); //设置进度 pb.setProgress(0); //开始播放 mediaPlayer.start(); ib.setImageResource(R.drawable.b); tv1.setText("开始播放"); new Thread(){ @Override public void run() { while (pb.getProgress()<pb.getMax()&&mediaPlayer!=null&&i!=1){ pb.setProgress(mediaPlayer.getCurrentPosition()); try { Thread.sleep(5000); Log.e("TAG", "aaa"); } catch (Exception e){ e.printStackTrace(); } } } }.start(); } public void b2(View view){ if (mediaPlayer!=null&&mediaPlayer.isPlaying()){ mediaPlayer.pause(); tv1.setText("暂停播放"); i=1; ib.setImageResource(R.drawable.bb); } } public void b3(View view){ if(mediaPlayer!=null){ mediaPlayer.stop(); mediaPlayer.reset(); mediaPlayer.release();ib.setImageResource(R.drawable.b); mediaPlayer=null; pb.setProgress(0); tv1.setText("暂无播放"); i=1; ib.setImageResource(R.drawable.bb); } } public void b4(View view){ b3(view); finish(); } }
视图
<TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tv"/> <ProgressBar android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/pb" style="@style/Widget.AppCompat.ProgressBar.Horizontal" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <ImageButton android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:onClick="b1" android:src="@drawable/bb" android:id="@+id/ib" /> <Button android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:onClick="b2" android:text="暂停播放"/> <Button android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:onClick="b3" android:text="停止播放"/> <Button android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:onClick="b4" android:text="退出"/> </LinearLayout> <ImageView android:layout_width="match_parent" android:layout_height="0dp" android:id="@+id/iv" android:layout_weight="1"/>
以上是关于音乐播放器组件的主要内容,如果未能解决你的问题,请参考以下文章