android 系统播放器播放https,android:自己实现能播放网络视频url的播放器

android原生自带的播放器一般只能播放本地视频,而没有播放url的功能。不过android系统中有一个VideoView的控件可以实现这一功能。

简单实现如下:

public class MyVideoPlay extends Activity implements MediaPlayer.OnErrorListener,

MediaPlayer.OnCompletionListener {

public static final String TAG = "MyVideoPlay";

private VideoView mVideoView;

private Uri mUri;

private int mPositionWhenPaused = -1;

private MediaController mMediaController;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.video_view);

//Set the screen to landscape.

this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

mVideoView = (VideoView)findViewById(R.id.my_video_view);

//Video file

Intent it = getIntent();

mUri = Uri.parse(it.getStringExtra("netUrl"));

//Create media controller

mMediaController = new MediaController(this);

mVideoView.setMediaController(mMediaController);

mVideoView.setOnCompletionListener(new OnCompletionListener() {

@Override

public void onCompletion(MediaPlayer mp) {

// TODO Auto-generated method stub

MyVideoPlay.this.finish();

}

});

}

public void onStart() {

// Play Video

if (mVideoView != null && mUri != null) {

mVideoView.setVideoURI(mUri);

mVideoView.start();

} else {

Toast.makeText(MyVideoPlay.this, "发生错误", Toast.LENGTH_SHORT).show();

}

super.onStart();

}

public void onPause() {

mPositionWhenPaused = mVideoView.getCurrentPosition();

mVideoView.stopPlayback();

super.onPause();

}

public void onResume() {

// Resume video player

if(mPositionWhenPaused >= 0) {

mVideoView.seekTo(mPositionWhenPaused);

mPositionWhenPaused = -1;

}

super.onResume();

}

public boolean onError(MediaPlayer player, int arg1, int arg2) {

return false;

}

public void onCompletion(MediaPlayer mp) {

MyVideoPlay.this.finish();

}

}

截图:

69e449e5a8230a522370ec5039cccaa5.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值