Android 下的一段視屏播放的邏輯只是Activity的一部分邏輯源碼
DemoActivity:
public class DemoActivity extends Activity implements OnClickListener {
EditText et_path;
Button bt_start;
Button bt_pause;
Button bt_restart;
Button bt_stop;
SurfaceView sv;
SurfaceHolder holder;
MediaPlayer player;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
et_path = (EditText) this.findViewById(R.id.et_path);
bt_start = (Button) this.findViewById(R.id.bt_start);
bt_pause = (Button) this.findViewById(R.id.bt_pause);
bt_restart = (Button) this.findViewById(R.id.bt_restart);
bt_stop = (Button) this.findViewById(R.id.bt_stop);
sv = (SurfaceView) this.findViewById(R.id.sv);
bt_start.setOnClickListener(this);
bt_pause.setOnClickListener(this);
bt_restart.setOnClickListener(this);
bt_stop.setOnClickListener(this);
holder = sv.getHolder();
// 設置緩沖區的內容向holder推送的方式
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
holder.addCallback(new MyHolderCallback());
}
/**
* surface的聲明周期
* @author Administrator
*
*/
private class MyHolderCallback implements SurfaceHolder.Callback{
@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder arg0) {
// TODO Auto-generated method stub
}
@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
// TODO Auto-generated method stub
}
}
@Override
public void onClick(View v) {
String path = et_path.getText().toString().trim();
switch(v.getId()){
case R.id.bt_start:
player = new MediaPlayer();
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setDisplay(holder);
if("".equals(path)){//如果輸入的鏈接為空的話
Toast.makeText(this, "文件為空,請輸入文件的鏈接", Toast.LENGTH_SHORT).show();
return;
}
try {
player.setDataSource(path);
player.prepare();
} catch (Exception e) {
Toast.makeText(this, "文件訪問出錯", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
player.setOnCompletionListener(new OnCompletionListener(){//當媒體播放是否完成的監聽器
@Override
public void onCompletion(MediaPlayer mp) {
Toast.makeText(DemoActivity.this, "播放完成", 0).show();
player.release();//播放完成的時候,要釋放資源
player = null;
bt_start.setEnabled(true);
}
});
player.setOnErrorListener(new OnErrorListener(){
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
Toast.makeText(DemoActivity.this, "播放完成出現錯誤", 0).show();
player.release();//出現錯誤的時候要釋放資源
player = null;
bt_start.setEnabled(true);
return false;
}
});
player.start();
bt_start.setEnabled(false);
break;
case R.id.bt_pause:
if(player != null && player.isPlaying()){//如果媒體正在播放的話
player.pause();
bt_pause.setText(">>");
return;
}
if(player != null && ">>".equals(bt_pause.getText())){
player.start();
bt_pause.setText("||");
return;
}
break;
case R.id.bt_restart:
if(player != null && player.isPlaying()){//媒體正在播放的話
player.seekTo(0);
}else{
Toast.makeText(DemoActivity.this,"需要重新博凡", Toast.LENGTH_SHORT).show();
}
break;
case R.id.bt_stop:
if(player != null && player.isPlaying()){//媒體正在播放的話
player.stop();//如果是停止播放一段媒體的話,肯定是要先停止,然後再釋放資源
player.release();
player = null;
bt_start.setEnabled(true);//設置開始按鈕為可點擊
} else{
player.release();
player = null;
bt_start.setEnabled(true);//設置開始按鈕為可點擊
bt_start.setClickable(true);
}
break;
}
}
}
Ubuntu 14.04 x64配置Android 4.4 kitkat編譯環境的方法 http://www.linuxidc.com/Linux/2014-05/101148.htm
Ubuntu 12.04搭建Android開發環境 http://www.linuxidc.com/Linux/2012-09/69961.htm
Ubuntu 14.04 配置 Android SDK 開發環境 http://www.linuxidc.com/Linux/2014-05/101039.htm
64位Ubuntu 11.10下Android開發環境的搭建(JDK+Eclipse+ADT+Android SDK詳細) http://www.linuxidc.com/Linux/2013-06/85303.htm
Ubuntu 12.10 x64 安裝 Android SDK http://www.linuxidc.com/Linux/2013-03/82005.htm
更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11