媒體播放自訂介面

來源:互聯網
上載者:User

標籤:

public class VideoActivity extends Activity implements Callback{
private SurfaceView view;
private SurfaceHolder holder;
private MediaPlayer mPlayer;

private String path;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.video_main);

initWidgets();
}

private void initWidgets() {
view=(SurfaceView) findViewById(R.id.surfaceView);

holder=view.getHolder();
holder.setFixedSize(320, 480);
holder.setKeepScreenOn(true);
//設定回調的監聽
holder.addCallback(this);

path=getIntent().getStringExtra("path");
}

@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {

}

@Override
public void surfaceCreated(SurfaceHolder arg0) {
mPlayer=new MediaPlayer();
try {
mPlayer.setDataSource(path);
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mPlayer.setDisplay(holder);

mPlayer.prepare();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
mPlayer.release();
}

public void play(View v){
if (!mPlayer.isPlaying()) {
mPlayer.start();
}
}
}

 

 

 

public class MainActivity extends Activity implements SurfaceHolder.Callback {//2.實現回調SurfaceHolder.Callback
private VideoView videoView;

// 第二種介面
private SurfaceView view;
private SurfaceHolder holder;

private MediaPlayer mPlayer;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.vedio_main);

// String path = Environment.getExternalStorageDirectory()+"/move.mp4";
// //初始化視頻介面
// videoView = (VideoView)findViewById(R.id.video);
//
// //向視頻介面載入視頻源
// videoView.setVideoPath(path);
//
// MediaController controller = new MediaController(this);//控制中心
// controller.setMediaPlayer(videoView);//將播放介面用controller來控制
//
// videoView.setMediaController(controller);//介面與控制中心綁定
// videoView.requestFocus();//設定視頻有焦點

// 自訂的介面:視頻播放器,SurfaceView是專做介面的容器
view = (SurfaceView) findViewById(R.id.view);

// 1.綁定介面並設定參數
holder = view.getHolder();// 執行個體化視頻Holder對象,此時由holder來設定參數
holder.setFixedSize(320, 480);// 視頻解析度,不寫就是預設
holder.setKeepScreenOn(true);// 螢幕一直開著

//設定回調的監聽 
holder.addCallback(this);
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {//介面改變時

}

@Override
public void surfaceCreated(SurfaceHolder holder) {//建立介面的時候
mPlayer = new MediaPlayer();
try {
//Cursor cursor = getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, null,null,null,null);
//while(cursor.moveToNext()){
//Log.i("path", cursor.getString(cursor.getColumnIndex(Media.DATA)));
//}

mPlayer.setDataSource(Environment.getExternalStorageDirectory()+"/move.mp4");
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);//音源類型
mPlayer.setDisplay(holder);//載入設定好的自訂介面
mPlayer.prepare();//準備
} catch (Exception e) {
e.printStackTrace();
}
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {//銷毀介面
if(mPlayer.isPlaying()){
mPlayer.stop();
mPlayer.release();
}
}

//播放按鈕
public void play(View v){
if(!mPlayer.isPlaying()){
mPlayer.start();
}
}
}

媒體播放自訂介面

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.