編輯:關於Android編程
package cn.itcast.videoplayer; import java.io.File; import android.app.Activity; import android.media.MediaPlayer; import android.media.MediaPlayer.OnPreparedListener; import android.os.Bundle; import android.os.Environment; import android.view.SurfaceHolder; import android.view.SurfaceHolder.Callback; import android.view.SurfaceView; import android.view.View; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { private EditText nameText; private String path; private MediaPlayer mediaPlayer; private SurfaceView surfaceView; private boolean pause; private int position; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mediaPlayer = new MediaPlayer(); nameText = (EditText) this.findViewById(R.id.filename); surfaceView = (SurfaceView) this.findViewById(R.id.surfaceView); //把輸送給surfaceView的視頻畫面,直接顯示到屏幕上,不要維持它自身的緩沖區 surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); surfaceView.getHolder().setFixedSize(176, 144); surfaceView.getHolder().setKeepScreenOn(true); surfaceView.getHolder().addCallback(new SurfaceCallback()); } private final class SurfaceCallback implements Callback{ public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } public void surfaceCreated(SurfaceHolder holder) { if(position>0 && path!=null){ play(position); position = 0; } } public void surfaceDestroyed(SurfaceHolder holder) { if(mediaPlayer.isPlaying()){ position = mediaPlayer.getCurrentPosition(); mediaPlayer.stop(); } } } @Override protected void onDestroy() { mediaPlayer.release(); mediaPlayer = null; super.onDestroy(); } public void mediaplay(View v){ switch (v.getId()) { case R.id.playbutton: String filename = nameText.getText().toString(); if(filename.startsWith("http")){ path = filename; play(0); }else{ File file = new File(Environment.getExternalStorageDirectory(), filename); if(file.exists()){ path = file.getAbsolutePath(); play(0); }else{ path = null; Toast.makeText(this, R.string.filenoexsit, 1).show(); } } break; case R.id.pausebutton: if(mediaPlayer.isPlaying()){ mediaPlayer.pause(); pause = true; }else{ if(pause){ mediaPlayer.start(); pause = false; } } break; case R.id.resetbutton: if(mediaPlayer.isPlaying()){ mediaPlayer.seekTo(0); }else{ if(path!=null){ play(0); } } break; case R.id.stopbutton: if(mediaPlayer.isPlaying()){ mediaPlayer.stop(); } break; } } private void play(int position) { try { mediaPlayer.reset(); mediaPlayer.setDataSource(path); mediaPlayer.setDisplay(surfaceView.getHolder()); mediaPlayer.prepare();//緩沖 mediaPlayer.setOnPreparedListener(new PrepareListener(position)); } catch (Exception e) { e.printStackTrace(); } } private final class PrepareListener implements OnPreparedListener{ private int position; public PrepareListener(int position) { this.position = position; } public void onPrepared(MediaPlayer mp) { mediaPlayer.start();//播放視頻 if(position>0) mediaPlayer.seekTo(position); } } }
因為Android的編譯系統不同於Linux Kernel的遞歸式的編譯系統,它的編譯系統是一種稱之為independent的模式,每個模塊基本獨立(它有可能依賴其他模塊
前言再次寫聊天的時候才發現,代碼積累是一件非常重要的事情,就如這篇博客的意圖其實就是代碼積累的目的,其實沒什麼難度,但是一件很瑣碎的事情真的也需要時間去完成和調試,所以,
又想到快要過年了,到時候還不知道群裡要發好多紅包,所以我將之前在網上宕的一份微信搶紅包的代碼修改了一下,實現了QQ搶紅包!可以支持搶QQ拼手氣紅包,普通紅包,口令紅包,現
引言 程序猿們,是否還在為你的老板辛辛苦苦的打工而拿著微薄的薪水呢,還是不知道如何用自己的應用或游戲來賺錢呢! 在這裡IQuick將教您如何同過自己的應用來賺取自己的