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

1:videoview循环播放视频

    1》xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context=".MainActivity" >



    <VideoView

        android:id="@+id/vv_player"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:layout_centerInParent="true"/>



</RelativeLayout>

 

 

   2》java代码

   

package com.example.testhour;



import android.annotation.SuppressLint;

import android.app.Activity;

import android.graphics.Bitmap;

import android.media.MediaMetadataRetriever;

import android.media.MediaPlayer;

import android.media.MediaPlayer.OnCompletionListener;

import android.media.MediaPlayer.OnPreparedListener;

import android.net.Uri;

import android.os.Bundle;

import android.os.Handler;

import android.view.Menu;

import android.view.SurfaceView;

import android.widget.MediaController;

import android.widget.VideoView;



public class MainActivity extends Activity {



	private VideoView videoview;

	private MediaController mMediaController;

	private String url1 = "http://g3.letv.cn/24/6/3/letv-uts/2522145-AVC-1611234-AAC-122967-2076960-463162247-274c0504575322ddd3fbc512818de8ae-1366891476558.flv";

	private String url2 = "http://g3.letv.cn/27/9/3/letv-uts/2522159-AVC-1610564-AAC-123276-2196960-489820604-1b165c6eec261a12e925f48f7ca35892-1366904195636.flv";

    private String url = "rtsp://184.72.239.149/vod/mp4:BigBuckBunny_115k.mov";

    

    private SurfaceView surfaceview;

	@Override

	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_main2);

		initview();

		loadView2(url);

	}



	private void initview() {

		videoview = (VideoView) findViewById(R.id.vv_player);

		

//		mMediaController = new MediaController(this);

//		videoview.setMediaController(mMediaController);

	}



	

	public void loadView2(String path) {



		Uri uri = Uri.parse(path);

		videoview.setVideoURI(uri);

		videoview.start();



		videoview.setOnPreparedListener(new OnPreparedListener() {



			@Override

			public void onPrepared(MediaPlayer mp) {



				mp.start();// 播放

				mp.setLooping(true);



			  

			}

		});



		videoview.setOnCompletionListener(new OnCompletionListener() {



			@Override

			public void onCompletion(MediaPlayer mp) {

				android.util.Log.i("test", "播放完毕");

				Uri uri = Uri.parse(url);

				videoview.setVideoURI(uri);

				videoview.start();// 播放



			}

		});



	}



	@Override

	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.

		getMenuInflater().inflate(R.menu.activity_main, menu);

		return true;

	}



	private Handler mHandler = new Handler() {

		@SuppressLint("NewApi")

		public void handleMessage(android.os.Message msg) {



			switch (msg.what) {



			case 33:

				loadView2(url2);

				break;



			}



		};

	};

}


2,mediaplayer+sufaceview播放视频

 

   1》xml

        

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical" >



    <SurfaceView

        android:id="@+id/surfaceView"

        android:layout_width="fill_parent"

        android:layout_height="500px" />



    <LinearLayout

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:gravity="center_horizontal"

        android:orientation="horizontal" >



        <ImageButton

            android:id="@+id/btnplay"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

           

            android:src="@drawable/ic_launcher" />



        <ImageButton

            android:id="@+id/btnpause"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

             />



        <ImageButton

            android:id="@+id/btnstop"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            />

    </LinearLayout>



</LinearLayout>


2:java代码

 

    

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() {

			@Override

			public void surfaceDestroyed(SurfaceHolder holder) {



			}



			@Override

			public 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");



			}



			@Override

			public void surfaceChanged(SurfaceHolder holder, int format,

					int width, int height) {



			}

		});

	}



	@Override

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

		}



	}



	@Override

	protected 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);

			// 把视频画面输出到SurfaceView

			mediaPlayer.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" />

    

 

你可能感兴趣的:(mediaplayer)