首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

android 信手记 videoview循环播放网络视频 和mediaplayer+sufaceview播放网络视频

2013-10-13 
android 随手记videoview循环播放网络视频和mediaplayer+sufaceview播放网络视频1:videoview循环播放视频1

android 随手记 videoview循环播放网络视频 和mediaplayer+sufaceview播放网络视频

1:videoview循环播放视频

    1》xml

package com.example.testhour;/* import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.MenuItem; import android.support.v4.app.NavUtils; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }*/import android.app.Activity;import android.media.AudioManager;import android.media.MediaPlayer;import android.net.Uri;import android.os.Bundle;import android.view.SurfaceHolder;import android.view.SurfaceHolder.Callback;import android.view.SurfaceView;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ImageButton;import android.widget.Toast;public class MainActivity2 extends Activity implements OnClickListener {private ImageButton btnplay, btnstop, btnpause;private SurfaceView surfaceView;private MediaPlayer mediaPlayer;private int position;private String url = "rtsp://184.72.239.149/vod/mp4:BigBuckBunny_115k.mov";public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main2);btnplay = (ImageButton) this.findViewById(R.id.btnplay);btnstop = (ImageButton) this.findViewById(R.id.btnstop);btnpause = (ImageButton) this.findViewById(R.id.btnpause);btnstop.setOnClickListener(this);btnplay.setOnClickListener(this);btnpause.setOnClickListener(this);mediaPlayer = new MediaPlayer();surfaceView = (SurfaceView) this.findViewById(R.id.surfaceView);// 设置SurfaceView自己不管理的缓冲区surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);surfaceView.getHolder().addCallback(new Callback() {@Overridepublic void surfaceDestroyed(SurfaceHolder holder) {}@Overridepublic void surfaceCreated(SurfaceHolder holder) {if (position > 0) {try {// 开始播放play();// 并直接从指定位置开始播放mediaPlayer.seekTo(position);position = 0;} catch (Exception e) {// TODO: handle exception}}System.out.println("surface create .............xxxxxxxxxxxxxxxx");}@Overridepublic void surfaceChanged(SurfaceHolder holder, int format,int width, int height) {}});}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.btnplay:play();break;case R.id.btnpause:if (mediaPlayer.isPlaying()) {mediaPlayer.pause();} else {mediaPlayer.start();}break;case R.id.btnstop:if (mediaPlayer.isPlaying()) {mediaPlayer.stop();}break;default:break;}}@Overrideprotected void onPause() {// 先判断是否正在播放if (mediaPlayer.isPlaying()) {// 如果正在播放我们就先保存这个播放位置position = mediaPlayer.getCurrentPosition();mediaPlayer.stop();}super.onPause();}private void play() {try {mediaPlayer.reset();mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);// 设置需要播放的视频Uri uri = Uri.parse(url);mediaPlayer.setDataSource(getApplicationContext(), uri);// 把视频画面输出到SurfaceViewmediaPlayer.setDisplay(surfaceView.getHolder());mediaPlayer.prepare();// 播放mediaPlayer.start();Toast.makeText(this, "开始播放!", Toast.LENGTH_LONG).show();} catch (Exception e) {// TODO: handle exception}}}


最后  因为播放是网络视频 千万不要忘了添加访问网络权限,

 <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.READ_LOGS" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_SETTINGS" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />

    

热点排行