編輯:關於Android編程
package com.wwj.download.util; import java.io.IOException; import java.util.Timer; import java.util.TimerTask; import android.media.AudioManager; import android.media.MediaPlayer; import android.media.MediaPlayer.OnBufferingUpdateListener; import android.media.MediaPlayer.OnCompletionListener; import android.media.MediaPlayer.OnPreparedListener; import android.os.Handler; import android.util.Log; import android.widget.SeekBar; public class Player implements OnBufferingUpdateListener, OnCompletionListener, OnPreparedListener { public MediaPlayer mediaPlayer; // 媒體播放器 private SeekBar seekBar; // 拖動條 private Timer mTimer = new Timer(); // 計時器 // 初始化播放器 public Player(SeekBar seekBar) { super(); this.seekBar = seekBar; try { mediaPlayer = new MediaPlayer(); mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);// 設置媒體流類型 mediaPlayer.setOnBufferingUpdateListener(this); mediaPlayer.setOnPreparedListener(this); } catch (Exception e) { e.printStackTrace(); } // 每一秒觸發一次 mTimer.schedule(timerTask, 0, 1000); } // 計時器 TimerTask timerTask = new TimerTask() { @Override public void run() { if (mediaPlayer == null) return; if (mediaPlayer.isPlaying() && seekBar.isPressed() == false) { handler.sendEmptyMessage(0); // 發送消息 } } }; Handler handler = new Handler() { public void handleMessage(android.os.Message msg) { int position = mediaPlayer.getCurrentPosition(); int duration = mediaPlayer.getDuration(); if (duration > 0) { // 計算進度(獲取進度條最大刻度*當前音樂播放位置 / 當前音樂時長) long pos = seekBar.getMax() * position / duration; seekBar.setProgress((int) pos); } }; }; public void play() { mediaPlayer.start(); } /** * * @param url * url地址 */ public void playUrl(String url) { try { mediaPlayer.reset(); mediaPlayer.setDataSource(url); // 設置數據源 mediaPlayer.prepare(); // prepare自動播放 } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } // 暫停 public void pause() { mediaPlayer.pause(); } // 停止 public void stop() { if (mediaPlayer != null) { mediaPlayer.stop(); mediaPlayer.release(); mediaPlayer = null; } } // 播放准備 @Override public void onPrepared(MediaPlayer mp) { mp.start(); Log.e("mediaPlayer", "onPrepared"); } // 播放完成 @Override public void onCompletion(MediaPlayer mp) { Log.e("mediaPlayer", "onCompletion"); } /** * 緩沖更新 */ @Override public void onBufferingUpdate(MediaPlayer mp, int percent) { seekBar.setSecondaryProgress(percent); int currentProgress = seekBar.getMax() * mediaPlayer.getCurrentPosition() / mediaPlayer.getDuration(); Log.e(currentProgress + "% play", percent + " buffer"); } }
package com.wwj.download; import java.io.File; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import android.app.Activity; import android.os.Bundle; import android.os.Environment; import android.os.Handler; import android.os.Message; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.ProgressBar; import android.widget.SeekBar; import android.widget.SeekBar.OnSeekBarChangeListener; import android.widget.TextView; import android.widget.Toast; import com.wwj.download.util.Player; import com.wwj.net.download.DownloadProgressListener; import com.wwj.net.download.FileDownloader; public class MainActivity extends Activity { private static final int PROCESSING = 1; private static final int FAILURE = -1; private EditText pathText; // url地址 private TextView resultView; private Button downloadButton; private Button stopButton; private ProgressBar progressBar; private Button playBtn; private Player player; // 播放器 private SeekBar musicProgress; // 音樂進度 private Handler handler = new UIHandler(); private final class UIHandler extends Handler { public void handleMessage(Message msg) { switch (msg.what) { case PROCESSING: // 更新進度 progressBar.setProgress(msg.getData().getInt("size")); float num = (float) progressBar.getProgress() / (float) progressBar.getMax(); int result = (int) (num * 100); // 計算進度 resultView.setText(result + "%"); if (progressBar.getProgress() == progressBar.getMax()) { // 下載完成 Toast.makeText(getApplicationContext(), R.string.success, Toast.LENGTH_LONG).show(); } break; case FAILURE: // 下載失敗 Toast.makeText(getApplicationContext(), R.string.error, Toast.LENGTH_LONG).show(); break; } } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); pathText = (EditText) findViewById(R.id.path); resultView = (TextView) findViewById(R.id.resultView); downloadButton = (Button) findViewById(R.id.downloadbutton); stopButton = (Button) findViewById(R.id.stopbutton); progressBar = (ProgressBar) findViewById(R.id.progressBar); ButtonClickListener listener = new ButtonClickListener(); downloadButton.setOnClickListener(listener); stopButton.setOnClickListener(listener); playBtn = (Button) findViewById(R.id.btn_online_play); playBtn.setOnClickListener(listener); musicProgress = (SeekBar) findViewById(R.id.music_progress); player = new Player(musicProgress); musicProgress.setOnSeekBarChangeListener(new SeekBarChangeEvent()); } private final class ButtonClickListener implements View.OnClickListener { @Override public void onClick(View v) { switch (v.getId()) { case R.id.downloadbutton: // 開始下載 // http://abv.cn/music/光輝歲月.mp3,可以換成其他文件下載的鏈接 String path = pathText.getText().toString(); String filename = path.substring(path.lastIndexOf('/') + 1); try { // URL編碼(這裡是為了將中文進行URL編碼) filename = URLEncoder.encode(filename, "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } path = path.substring(0, path.lastIndexOf("/") + 1) + filename; if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) { // File savDir = // Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES); // 保存路徑 File savDir = Environment.getExternalStorageDirectory(); download(path, savDir); } else { Toast.makeText(getApplicationContext(), R.string.sdcarderror, Toast.LENGTH_LONG).show(); } downloadButton.setEnabled(false); stopButton.setEnabled(true); break; case R.id.stopbutton: // 暫停下載 exit(); Toast.makeText(getApplicationContext(), "Now thread is Stopping!!", Toast.LENGTH_LONG).show(); downloadButton.setEnabled(true); stopButton.setEnabled(false); break; case R.id.btn_online_play: new Thread(new Runnable() { @Override public void run() { player.playUrl(pathText.getText().toString()); } }).start(); break; } } /* * 由於用戶的輸入事件(點擊button, 觸摸屏幕....)是由主線程負責處理的,如果主線程處於工作狀態, * 此時用戶產生的輸入事件如果沒能在5秒內得到處理,系統就會報“應用無響應”錯誤。 * 所以在主線程裡不能執行一件比較耗時的工作,否則會因主線程阻塞而無法處理用戶的輸入事件, * 導致“應用無響應”錯誤的出現。耗時的工作應該在子線程裡執行。 */ private DownloadTask task; private void exit() { if (task != null) task.exit(); } private void download(String path, File savDir) { task = new DownloadTask(path, savDir); new Thread(task).start(); } /** * * UI控件畫面的重繪(更新)是由主線程負責處理的,如果在子線程中更新UI控件的值,更新後的值不會重繪到屏幕上 * 一定要在主線程裡更新UI控件的值,這樣才能在屏幕上顯示出來,不能在子線程中更新UI控件的值 * */ private final class DownloadTask implements Runnable { private String path; private File saveDir; private FileDownloader loader; public DownloadTask(String path, File saveDir) { this.path = path; this.saveDir = saveDir; } /** * 退出下載 */ public void exit() { if (loader != null) loader.exit(); } DownloadProgressListener downloadProgressListener = new DownloadProgressListener() { @Override public void onDownloadSize(int size) { Message msg = new Message(); msg.what = PROCESSING; msg.getData().putInt("size", size); handler.sendMessage(msg); } }; public void run() { try { // 實例化一個文件下載器 loader = new FileDownloader(getApplicationContext(), path, saveDir, 3); // 設置進度條最大值 progressBar.setMax(loader.getFileSize()); loader.download(downloadProgressListener); } catch (Exception e) { e.printStackTrace(); handler.sendMessage(handler.obtainMessage(FAILURE)); // 發送一條空消息對象 } } } } // 進度改變 class SeekBarChangeEvent implements OnSeekBarChangeListener { int progress; @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { // 原本是(progress/seekBar.getMax())*player.mediaPlayer.getDuration() this.progress = progress * player.mediaPlayer.getDuration() / seekBar.getMax(); } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { // seekTo()的參數是相對與影片時間的數字,而不是與seekBar.getMax()相對的數字 player.mediaPlayer.seekTo(progress); } } @Override protected void onDestroy() { super.onDestroy(); if (player != null) { player.stop(); player = null; } } }
本文主要介紹Android應用程序簽名的相關理論知識以及如何發布Android應用程序。 1.簽名的概念 為大家所熟知的日常生活中的簽名,它是代表某個人的特殊標記,用
Action Bar介紹Action Bar 是Google 在Android 3.0之後推出的一種全新用戶操作方式 目的是用來替換掉菜單按鍵功能,長按操作功能,提供一種
近來項目有個需求,要做個和QQ空間類似的菜單欄透明度漸變和下拉刷新帶有阻尼回彈的效果。於是花點時間動手試了試,基本上達到了QQ空間的效果,截圖如下:通過觀察QQ空間的運行
android studio升級到stable 2.2之後,發現還有了個ConstraintLayout。看名字就是約束布局,用各種約束來確定widget的展示。該Con