Android Media(多媒體)獲取手機記憶體卡下的音樂並播放

Mr_LDL發表於2017-07-20

程式碼實現:


package com.example.g160628_android_27_media;

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.SeekBar;

import java.io.IOException;

public class SdcardMactivity extends AppCompatActivity {

    private MediaPlayer mediaPlayer;
    private SeekBar sb_main_bar;

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

        //給進度條設定滑動監聽
        sb_main_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 currentPosition=seekBar.getProgress();
                //跳轉到某個位置進行播放
                mediaPlayer.seekTo(currentPosition);
            }
        });



    }
    public void playOrPause(View view){
        final ImageButton imageButton= (ImageButton) view;

         //例項化MediaPlayer
        if(mediaPlayer==null){
            //mediaPlayer = MediaPlayer.create(this, R.raw.s8);
            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.setDataSource(this, Uri.parse("http://192.168.43.77:7788/s11.mp3"));

                    mediaPlayer.prepareAsync();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                //設定準備監聽
                mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                    @Override
                    public void onPrepared(MediaPlayer mp) {
                        mediaPlayer.start();
                        //暫停圖示
                        imageButton.setImageResource(android.R.drawable.ic_media_pause);

                        //獲取音樂的播放時間
                        int time=mediaPlayer.getDuration();

                        //設定進度條的最大值 為  音樂的播放時間
                        sb_main_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_main_bar.getProgress()<sb_main_bar.getMax()){
                 //獲取音樂當前的播放位置
                int currentPosition=mediaPlayer.getCurrentPosition();
                sb_main_bar.setProgress(currentPosition);
            }
        }
    }




}


相關文章