Android:如何从SD卡播放音频文件

时间:2015-12-04 04:53:28

标签: android audio

任何人都可以帮我解决这个问题。下面是我的代码运行正常,但我想播放SD卡的文件夹,如/sdcard/Music/song.mp3。

public class MainActivity extends Activity implements View.OnClickListener,SeekBar.OnSeekBarChangeListener {

    private MediaPlayer mediaPlayer;
    public TextView songName, duration,tvTotalTime;
    private double timeElapsed = 0, finalTime = 0;
    private int forwardTime = 2000, backwardTime = 2000;
    private Handler durationHandler = new Handler();
    private SeekBar seekbar;
    private ImageButton imgPlay,imgPause;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        getid();
        initializeViews();
        setListner();
    }

    private void setListner() {
        imgPlay.setOnClickListener(this);
        imgPause.setOnClickListener(this);
        seekbar.setOnSeekBarChangeListener(this);
    }

    private void getid() {

        songName = (TextView) findViewById(R.id.songName);
        tvTotalTime =(TextView)findViewById(R.id.tvTotalTime);
        mediaPlayer = MediaPlayer.create(this, R.raw.dheere);
        duration = (TextView) findViewById(R.id.duration);
        seekbar = (SeekBar) findViewById(R.id.seekBar);
        imgPlay =(ImageButton)findViewById(R.id.imgPlay);
        imgPause =(ImageButton)findViewById(R.id.imgPause);
    }

    public void initializeViews(){

        finalTime = mediaPlayer.getDuration();
        songName.setText("Sample_Song.mp3");
        seekbar.setMax((int) finalTime);
        seekbar.setClickable(true);

        }



    //handler to change seekBarTime
        private Runnable updateSeekBarTime = new Runnable() {
        public void run() {
            //get current position
            timeElapsed = mediaPlayer.getCurrentPosition();
            //set seekbar progress
            seekbar.setProgress((int) timeElapsed);


           tvTotalTime.setText(String.format("%d min, %d sec", TimeUnit.MILLISECONDS.toMinutes((long) finalTime), TimeUnit.MILLISECONDS.toSeconds((long) finalTime)));

            //set time remaing
            double timeRemaining = finalTime - timeElapsed;
            duration.setText(String.format("%d min, %d sec", TimeUnit.MILLISECONDS.toMinutes((long) timeRemaining), TimeUnit.MILLISECONDS.toSeconds((long) timeRemaining) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes((long) timeRemaining))));

            //repeat yourself that again in 100 miliseconds
            durationHandler.postDelayed(this, 100);
        }
    };


    @Override
        public void onClick(View v) {

        try {
            switch (v.getId())
            {
                case R.id.imgPlay:

                    mediaPlayer.start();
                    timeElapsed = mediaPlayer.getCurrentPosition();
                    seekbar.setProgress((int) timeElapsed);
                    durationHandler.postDelayed(updateSeekBarTime, 100);

                    break;

                case R.id.imgPause:

                    mediaPlayer.pause();

                    break;

            }
        } catch (IllegalStateException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

        if (fromUser) {
            mediaPlayer.seekTo(progress);
        }

    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }
}

2 个答案:

答案 0 :(得分:1)

只需设置mediaPlayer对象的路径并启动它即可播放...

MediaPlayer mp = new MediaPlayer();
mp.setDataSource("/mnt/sdcard/yourdirectory/youraudiofile.wav");
mp.prepare();
mp.start();

你可以在线程或AsyncTask中更新seekBar,直到音频片段继续播放为止,在你的线程中你必须更新你的seekBar

int mCurrentPosition = mp.getCurrentPosition() / 1000;
mSeekBar.setProgress(mCurrentPosition);

并确保你已经解决了seekBar的最大值

mSeekBar.setMax(mp.getDuration()); 

答案 1 :(得分:0)

它的工作原理如下:

mpintro = MediaPlayer.create(this, Uri.parse(Environment.getExternalStorageDirectory().getPath()+ "/Music/intro.mp3"));
mpintro.setLooping(true);
mpintro.start();