Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android 下的一段視屏播放的邏輯

android 下的一段視屏播放的邏輯

編輯:關於Android編程

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;
}

}
}


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved