<SeekBar android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/sb_mian_bar" /> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@android:drawable/ic_media_play" android:onClick="playorpause" />
import android.media.AudioManager; import android.media.MediaPlayer; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.ImageButton; import android.widget.SeekBar; import java.io.IOException; public class MainActivity extends AppCompatActivity { private ImageButton imageButton; private MediaPlayer mediaPlayer; private SeekBar sb_mian_bar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sb_mian_bar = (SeekBar) findViewById(R.id.sb_mian_bar); //给进度条设置滑动监听 sb_mian_bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { //获取当前进度条的位置 int currtentposition=seekBar.getProgress(); //跳到某位置进行放播 mediaPlayer.seekTo(currtentposition); } }); } public void playorpause(View view){ imageButton = (ImageButton) view; if(mediaPlayer==null){ // mediaPlayer = MediaPlayer.create(this, R.raw.s2); if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ String sdPath=Environment.getExternalStorageDirectory().getAbsolutePath(); mediaPlayer=new MediaPlayer(); mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); try { mediaPlayer.setDataSource(this, Uri.parse("file://"+sdPath+"s1.mp3")); mediaPlayer.prepareAsync(); } catch (IOException e) { e.printStackTrace(); } mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { @Override public void onPrepared(MediaPlayer mp) { imageButton.setImageResource(android.R.drawable.ic_media_pause); int time=mediaPlayer.getDuration(); sb_mian_bar.setMax(time);//设置进度条最大值为音乐的时间 new mythread().start(); } }); } }else if (mediaPlayer.isPlaying()){ mediaPlayer.pause(); imageButton.setImageResource(android.R.drawable.ic_media_play); }else { mediaPlayer.start(); imageButton.setImageResource(android.R.drawable.ic_media_pause); } } class mythread extends Thread{ @Override public void run() { super.run(); while(sb_mian_bar.getProgress()<sb_mian_bar.getMax()){ //获取当前音乐的播放位置 int currentposition=mediaPlayer.getCurrentPosition(); sb_mian_bar.setProgress(currentposition); } } } }<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.g160628_android_y227_music"> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>记得加权限