Android 音乐播放器-带seekBar滑动

XML布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<Button android:id="@+id/play"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="播放"
/>
<Button android:id="@+id/pause"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="暂停"
/>
<SeekBar android:id="@+id/sb"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:max="100" 
/>
</LinearLayout>

JAVA代码

package com.pocketdigi;

import android.app.Activity;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class main extends Activity {
    /** Called when the activity is first created. */
    Button play,pause;
    MediaPlayer mp;
    SeekBar sb;
    Handler handler=new Handler();
    int Duration;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        play=(Button)findViewById(R.id.play);
        pause=(Button)findViewById(R.id.pause);
        sb=(SeekBar)findViewById(R.id.sb);
        //找到相应View
        mp =MediaPlayer.create(this,Uri.parse("/sdcard/徐若瑄-爱笑的眼睛.mp3"));
        //后面的参数必须是URI形式的,所以要把相应路径转换成URI
        play.setOnClickListener(playlis);
        pause.setOnClickListener(pauselis);
        sb.setOnSeekBarChangeListener(sbLis);   
        //监听器
        Duration=mp.getDuration();
        //音乐文件持续时间
        sb.setMax(Duration);
        //设置SeekBar最大值为音乐文件持续时间
    }

    private OnClickListener playlis=new OnClickListener(){

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            handler.post(start);
            //调用handler播放

        }

    };
    Runnable start=new Runnable(){

        @Override
        public void run() {
            // TODO Auto-generated method stub
            mp.start();
            handler.post(updatesb);
            //用一个handler更新SeekBar
        }

    };
    Runnable updatesb =new Runnable(){

        @Override
        public void run() {
            // TODO Auto-generated method stub
            sb.setProgress(mp.getCurrentPosition());
            handler.postDelayed(updatesb, 1000);
            //每秒钟更新一次
        }

    };
    private OnClickListener pauselis=new OnClickListener(){

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            mp.pause();        
            //暂停
        }

    };
    private OnSeekBarChangeListener sbLis=new OnSeekBarChangeListener(){

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress,
                boolean fromUser) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub
            mp.seekTo(sb.getProgress());
            //SeekBar确定位置后,跳到指定位置
        }

    };
}

转载请注明出处
本文链接:https://zdran.com/20180601.html