編輯:關於Android編程
音樂播放
MediaPlayer mediaPlayer = new MediaPlayer();
if (mediaPlayer.isPlaying()) {});
package cn.itcast.mp3; import java.io.File; import android.app.Activity; import android.media.MediaPlayer; import android.media.MediaPlayer.OnCompletionListener; import android.media.MediaPlayer.OnPreparedListener; import android.os.Bundle; import android.os.Handler; import android.os.SystemClock; import android.telephony.PhoneStateListener; import android.telephony.TelephonyManager; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Chronometer; import android.widget.Chronometer.OnChronometerTickListener; import android.widget.EditText; import android.widget.SeekBar; import android.widget.SeekBar.OnSeekBarChangeListener; import android.widget.Toast; public class DemoActivity extends Activity implements OnClickListener, OnChronometerTickListener, OnSeekBarChangeListener { private EditText et_path; private Chronometer et_time; private SeekBar sb; private Button bt_play, bt_pause, bt_replay, bt_stop; private MediaPlayer mediaPlayer; private TelephonyManager manager; /** * subtime:點擊“續播”到暫停時的間隔的和 beginTime:重新回到播放時的bash值 falgTime:點擊“播放”時的值 * pauseTime:“暫停”時的值 */ private long subtime = 0, beginTime = 0, falgTime = 0, pauseTime = 0; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); manager = (TelephonyManager) this.getSystemService(TELEPHONY_SERVICE); manager.listen(new MyListener(), PhoneStateListener.LISTEN_CALL_STATE); sb = (SeekBar) this.findViewById(R.id.sb); et_path = (EditText) this.findViewById(R.id.et_path); et_time = (Chronometer) this.findViewById(R.id.et_time); bt_play = (Button) this.findViewById(R.id.play); bt_pause = (Button) this.findViewById(R.id.pause); bt_replay = (Button) this.findViewById(R.id.replay); bt_stop = (Button) this.findViewById(R.id.stop); sb.setEnabled(false); sb.setOnSeekBarChangeListener(this); bt_play.setOnClickListener(this); bt_pause.setOnClickListener(this); bt_replay.setOnClickListener(this); bt_stop.setOnClickListener(this); et_time.setOnChronometerTickListener(this); } Handler handler = new Handler(); Runnable updateThread = new Runnable() { public void run() { // 獲得歌曲現在播放位置並設置成播放進度條的值 if (mediaPlayer != null) { sb.setProgress(mediaPlayer.getCurrentPosition()); // 每次延遲100毫秒再啟動線程 handler.postDelayed(updateThread, 100); } } }; public void onClick(View v) { String path; try { switch (v.getId()) { case R.id.play: falgTime = SystemClock.elapsedRealtime(); path = et_path.getText().toString().trim(); play(path); pauseTime = 0; et_time.setBase(falgTime); et_time.start(); break; case R.id.pause: pause(); break; case R.id.replay: if (mediaPlayer != null && mediaPlayer.isPlaying()) { mediaPlayer.seekTo(0); et_time.setBase(SystemClock.elapsedRealtime()); et_time.start(); } else { path = et_path.getText().toString().trim(); play(path); et_time.setBase(SystemClock.elapsedRealtime()); et_time.start(); } if ("續播".equals(bt_pause.getText().toString().trim())) { bt_pause.setText("暫停"); } falgTime = SystemClock.elapsedRealtime(); subtime = 0; break; case R.id.stop: if (mediaPlayer != null && mediaPlayer.isPlaying()) { mediaPlayer.stop(); mediaPlayer = null; et_time.setBase(SystemClock.elapsedRealtime()); et_time.start(); et_time.stop(); bt_play.setEnabled(true); bt_play.setClickable(true); } falgTime = 0; subtime = 0; sb.setProgress(0); sb.setEnabled(false); break; } } catch (Exception e) { e.printStackTrace(); Toast.makeText(getApplicationContext(), "文件播放出現異常", 0).show(); } } private void pause() { // 判斷音樂是否在播放 if (mediaPlayer != null && mediaPlayer.isPlaying()) { // 暫停音樂播放器 mediaPlayer.pause(); bt_pause.setText("續播"); sb.setEnabled(false); et_time.stop(); pauseTime = SystemClock.elapsedRealtime(); // System.out.println("1 pauseTime" + pauseTime); } else if (mediaPlayer != null && "續播".equals(bt_pause.getText().toString())) { subtime += SystemClock.elapsedRealtime() - pauseTime; // System.out.println("2 subtime:" + subtime); mediaPlayer.start(); bt_pause.setText("暫停"); sb.setEnabled(true); beginTime = falgTime + subtime; // System.out.println("3 beginTime" + beginTime); et_time.setBase(beginTime); et_time.start(); } } /** * 播放指定地址的音樂文件 .mp3 .wav .amr * * @param path */ private void play(String path) throws Exception { if ("".equals(path)) { Toast.makeText(getApplicationContext(), "路徑不能為空", 0).show(); return; } File file = new File(path); if (file.exists()) { mediaPlayer = new MediaPlayer(); mediaPlayer.setDataSource(path); // mediaPlayer.prepare(); // c/c++ 播放器引擎的初始化 // 同步方法 // 采用異步的方式 mediaPlayer.prepareAsync(); // 為播放器注冊 mediaPlayer.setOnPreparedListener(new OnPreparedListener() { public void onPrepared(MediaPlayer mp) { // TODO Auto-generated method stub mediaPlayer.start(); bt_play.setEnabled(false); bt_play.setClickable(false); sb.setMax(mediaPlayer.getDuration()); handler.post(updateThread); sb.setEnabled(true); } }); // 注冊播放完畢後的監聽事件 mediaPlayer.setOnCompletionListener(new OnCompletionListener() { public void onCompletion(MediaPlayer mp) { mediaPlayer.release(); mediaPlayer = null; bt_play.setEnabled(true); bt_play.setClickable(true); et_time.setBase(SystemClock.elapsedRealtime()); et_time.start(); et_time.stop(); sb.setProgress(0); } }); } else { Toast.makeText(getApplicationContext(), "文件不存在", 0).show(); return; } } private class MyListener extends PhoneStateListener { @Override public void onCallStateChanged(int state, String incomingNumber) { super.onCallStateChanged(state, incomingNumber); switch (state) { case TelephonyManager.CALL_STATE_RINGING: // 音樂播放器暫停 pause(); break; case TelephonyManager.CALL_STATE_IDLE: // 重新播放音樂 pause(); break; } } } public void onChronometerTick(Chronometer chronometer) { } public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { // TODO 自動生成的方法存根 if (fromUser == true && mediaPlayer != null) { mediaPlayer.seekTo(progress); falgTime = SystemClock.elapsedRealtime(); beginTime = falgTime - sb.getProgress(); et_time.setBase(beginTime); et_time.start(); } } public void onStartTrackingTouch(SeekBar seekBar) { // TODO 自動生成的方法存根 } public void onStopTrackingTouch(SeekBar seekBar) { // TODO 自動生成的方法存根 } }main.xml:
package cn.itcast.soundpool; import android.app.Activity; import android.media.AudioManager; import android.media.SoundPool; import android.os.Bundle; import android.view.View; public class DemoActivity extends Activity { int soundid; SoundPool pool; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); pool = new SoundPool(5, AudioManager.STREAM_MUSIC, 0); // 這語句代碼 是一個異步的操作 soundid = pool.load(this, R.raw.ring, 1); // 需要花費一定的時間 } public void shoot(View view) { // 不會播放 因為上面異步的加載聲音的操作 還沒完成 pool.play(soundid, 1.0f, 1.0f, 0, 0, 1.0f); // taking tom } }
一、說起進度條,必須說說條形進度條,經常都會使用到嘛,特別是下載文件進度等等,還有像騰訊QQ安裝進度條一樣,有個進度總給人良好的用戶體驗。先來找圖看看,做這
1,首先需要保證自己的jdk安裝配置完成http://www.eclipse.org/babel/downloads.php 2 sdk的下載,http:/
我們繼續來講IPC機制,在本篇中你將會學習到ContentProvider Socket Binder連接池一.使用ContentProvider ContentPro
前言:工欲善其事,必先利其器,工作一段時間後,對於以上十個字的感觸是最深的。剛參加工作的時候,並沒有對於要做的事情有著自己的理解,經常是上面分配了工作,自己就乖乖地跑去做