音乐播放器

MainActivity.java

public class MainActivity extends AppCompatActivity {
    private SeekBar sb_id;
    private MediaPlayer mediaPlayer;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sb_id = (SeekBar) findViewById(R.id.sb_id); //SeekBar

        //给进度条设置滑动的监听
        sb_id.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                int progress=seekBar.getProgress();
                //在当前位置播放
                mediaPlayer.seekTo(progress);
            }
        });

    }

    public void button(View view){
        final ImageButton imageButton= (ImageButton) view;
        if(mediaPlayer ==null){
            mediaPlayer = MediaPlayer.create(this, R.raw.sa);//播放内存音频
            mediaPlayer.start();  //播放停止
            //获取音乐的总时长
            int duration=mediaPlayer.getDuration();
            //将进度条设置最大值为:音乐的总时长
            sb_id.setMax(duration);
            imageButton.setImageResource(android.R.drawable.ic_media_pause);
        }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_id.getProgress()<=sb_id.getMax()){
                //获取音乐当前播放的位置
                int position=mediaPlayer.getCurrentPosition();
                sb_id.setProgress(position);
            }
        }
    }
}



MainAction.java

public class MainAction01 extends AppCompatActivity {

    private SeekBar sb_id;
    private MediaPlayer mediaPlayer;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sb_id = (SeekBar) findViewById(R.id.sb_id);

        //给进度条设置滑动的监听
        sb_id.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                int progress=seekBar.getProgress();
                //在当前位置播放
                mediaPlayer.seekTo(progress);
            }
        });
    }

    public void button(View view){
        final ImageButton imageButton= (ImageButton) view;
        if(mediaPlayer==null){
            //播放内存卡中文件
            mediaPlayer=new MediaPlayer();
            try {
                mediaPlayer.setDataSource(this, Uri.parse("http://193.168.4.135/internet.mp3"));
                mediaPlayer.prepareAsync();
            } catch (IOException e) {
                e.printStackTrace();
            }
            //设置准备完成的监听
            mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mediaPlayer) {
                    //下载操作(复制)
                    mediaPlayer.start();
                    imageButton.setImageResource(android.R.drawable.ic_media_pause);
                    //获取音乐的总时长
                    int duration=mediaPlayer.getDuration();
                    //将进度条设置最大值为:音乐的总时长
                    sb_id.setMax(duration);

                    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_id.getProgress()<=sb_id.getMax()){
                //获取音乐当前播放的位置
                int position=mediaPlayer.getCurrentPosition();
                sb_id.setProgress(position);
            }
        }
    }
}


阅读更多
换一批

没有更多推荐了,返回首页