layout文件:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:paddingBottom="@dimen/activity_vertical_margin" 7 android:paddingLeft="@dimen/activity_horizontal_margin" 8 android:paddingRight="@dimen/activity_horizontal_margin" 9 android:paddingTop="@dimen/activity_vertical_margin" 10 tools:context="com.hanqi.musicplay.MainActivity" 11 android:orientation="vertical"> 12 13 <TextView 14 android:layout_width="wrap_content" 15 android:layout_height="wrap_content" 16 android:text="播放状态:未播放" 17 android:id="@+id/tv_1"/> 18 <ProgressBar 19 android:layout_width="match_parent" 20 android:layout_height="wrap_content" 21 android:id="@+id/pb_1" 22 style="@android:style/Widget.ProgressBar.Horizontal"/> 23 <LinearLayout 24 android:layout_width="match_parent" 25 android:layout_height="wrap_content"> 26 <Button 27 android:layout_width="0dp" 28 android:layout_height="wrap_content" 29 android:layout_weight="1" 30 android:onClick="play_onClick" 31 android:text="播放" 32 android:background="@drawable/a4"/> 33 <Button 34 android:layout_width="0dp" 35 android:layout_height="wrap_content" 36 android:layout_weight="1" 37 android:onClick="pause_onClick" 38 android:text="暂停" 39 android:background="@drawable/a4"/> 40 <Button 41 android:layout_width="0dp" 42 android:layout_height="wrap_content" 43 android:layout_weight="1" 44 android:onClick="stop_onClick" 45 android:text="停止" 46 android:background="@drawable/a4"/> 47 <!--<Button--> 48 <!--android:layout_width="0dp"--> 49 <!--android:layout_height="wrap_content"--> 50 <!--android:layout_weight="1"--> 51 <!--android:onClick="exit_onClick"--> 52 <!--android:text="退出"/>--> 53 </LinearLayout> 54 <TextView 55 android:layout_width="match_parent" 56 android:layout_height="wrap_content" 57 android:id="@+id/tv_2"/> 58 <ImageView 59 android:layout_width="match_parent" 60 android:layout_height="wrap_content" 61 android:id="@+id/iv_1"/> 62 </LinearLayout>
java类:
1 package com.hanqi.musicplay; 2 3 import android.media.MediaPlayer; 4 import android.os.Handler; 5 import android.os.Message; 6 import android.support.v7.app.AppCompatActivity; 7 import android.os.Bundle; 8 import android.view.View; 9 import android.widget.ImageView; 10 import android.widget.ProgressBar; 11 import android.widget.TextView; 12 13 public class MainActivity extends AppCompatActivity { 14 15 ProgressBar pb_1; 16 TextView tv_1; 17 MediaPlayer mediaPlayer; 18 ImageView iv_1; 19 Handler handler; 20 @Override 21 protected void onCreate(Bundle savedInstanceState) { 22 super.onCreate(savedInstanceState); 23 setContentView(R.layout.activity_main); 24 pb_1 = (ProgressBar)findViewById(R.id.pb_1); 25 tv_1 = (TextView)findViewById(R.id.tv_1); 26 iv_1 = (ImageView)findViewById(R.id.iv_1); 27 28 final int[]iv = {R.drawable.b11,R.drawable.b21,R.drawable.b3,R.drawable.b41}; 29 handler = new Handler(){ 30 @Override 31 public void handleMessage(Message msg) { 32 super.handleMessage(msg); 33 iv_1.setImageResource(iv[msg.arg1]); 34 } 35 }; 36 //图片轮播 在分线程 37 new Thread(new Runnable() { 38 @Override 39 public void run() { 40 41 for (int i = 0;i<iv.length;i++) 42 { 43 //iv_1.setImageResource(iv[i]); 44 Message message = new Message(); 45 message.what = 0; 46 message.arg1 = i; 47 handler.sendMessage(message); 48 try { 49 Thread.sleep(2000); 50 } 51 catch (Exception e) 52 { 53 54 } 55 if (i == iv.length-1)i=-1; 56 } 57 } 58 }).start(); 59 60 } 61 //播放 62 public void play_onClick(View v) 63 { 64 //构造mediaPlayer 65 if (mediaPlayer == null) 66 { 67 //用静态方法构造 68 mediaPlayer = MediaPlayer.create(this,R.raw.test); 69 mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { 70 @Override 71 public void onCompletion(MediaPlayer mp) { 72 tv_1.setText("播放状态:播放完成"); 73 mediaPlayer.reset(); 74 mediaPlayer.release(); 75 mediaPlayer = null; 76 } 77 }); 78 //设置进度条的最大长度 79 pb_1.setMax(mediaPlayer.getDuration()); 80 //启动分线程 81 new Thread(){ 82 @Override 83 public void run() { 84 //循环条件 85 while (mediaPlayer != null) 86 { 87 //更新进度 88 //设置当前进度值:和播放器的当前进度有关 89 pb_1.setProgress(mediaPlayer.getCurrentPosition()); 90 try { 91 Thread.sleep(500); 92 } 93 catch (Exception e) 94 { 95 96 } 97 } 98 } 99 }.start(); 100 } 101 mediaPlayer.start(); 102 tv_1.setText("播放状态:正在播放"); 103 } 104 //停止 105 public void stop_onClick(View v) 106 { 107 if (mediaPlayer != null) 108 { 109 mediaPlayer.stop(); 110 mediaPlayer.reset(); 111 mediaPlayer.release(); 112 mediaPlayer = null; 113 pb_1.setProgress(0); 114 tv_1.setText("播放状态:停止播放"); 115 } 116 } 117 //暂停 118 public void pause_onClick(View v) 119 { 120 if (mediaPlayer != null && mediaPlayer.isPlaying()) 121 { 122 mediaPlayer.pause(); 123 tv_1.setText("播放状态:暂停播放"); 124 } 125 126 } 127 //退出 128 // public void exit_onClick(View v) 129 // { 130 // stop_onClick(v); 131 // finish(); 132 // } 133 }
效果为: