android 音樂播放器簡單實現

來源:互聯網
上載者:User

標籤:android   style   blog   io   ar   color   os   sp   java   

package com.zhangbz.musicplayer;import java.io.File;import android.app.Activity;import android.media.AudioManager;import android.media.MediaPlayer;import android.media.MediaPlayer.OnCompletionListener;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;public class MainActivity extends Activity {    private EditText et_path;    private MediaPlayer mediaPlayer;    private Button bt_play, bt_pause, bt_stop, bt_replay;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);                et_path = (EditText) findViewById(R.id.et_path);        bt_play = (Button) findViewById(R.id.bt_play);        bt_pause = (Button) findViewById(R.id.bt_pause);        bt_replay = (Button) findViewById(R.id.bt_replay);        bt_stop = (Button) findViewById(R.id.bt_stop);    }    /**     * 播放     * @param view     */    public void play(View view) {        String filepath = et_path.getText().toString().trim();        File file = new File(filepath);        if(file.exists()) {            try {                mediaPlayer = new MediaPlayer();                mediaPlayer.setDataSource(filepath);//設定播放的資料來源                mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);                mediaPlayer.prepare();//準備開始播放,播放的邏輯是c代碼在新的線程執行。                mediaPlayer.start();                bt_play.setEnabled(false);                mediaPlayer.setOnCompletionListener(new OnCompletionListener() {                                        public void onCompletion(MediaPlayer arg0) {                        bt_play.setEnabled(true);                    }                });            } catch (Exception e) {                Toast.makeText(this, "播放失敗", 0).show();                e.printStackTrace();            }        } else {            Toast.makeText(this, "檔案不存在,請檢查檔案路徑", 0).show();        }    }    /**     * 暫停     * @param view     */    public void pause(View view) {        if("繼續".equals(bt_pause.getText().toString().trim())) {            mediaPlayer.start();            bt_pause.setText("暫停");            return;        }        if(mediaPlayer != null && mediaPlayer.isPlaying()) {            mediaPlayer.pause();            bt_pause.setText("繼續");        }    }    /**     * 停止     * @param view     */    public void stop(View view) {        if(mediaPlayer != null && mediaPlayer.isPlaying()) {            mediaPlayer.stop();            mediaPlayer.release();            mediaPlayer = null;        }        bt_pause.setText("暫停");        bt_play.setEnabled(true);    }    /**     * 重播     * @param view     */    public void replay(View view) {        if(mediaPlayer != null && mediaPlayer.isPlaying()) {            mediaPlayer.seekTo(0);        } else {            play(view);        }        bt_replay.setText("暫停");    }}

 

android 音樂播放器簡單實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.