使用VideoView播放rtsp視訊流

iteye_2973發表於2012-08-21

1.視訊播放控制

 

package com.example.rtspvideoview;

import android.app.ProgressDialog;
import android.content.Context;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.util.Log;
import android.widget.MediaController;
import android.widget.VideoView;

public class VideoViewController {

	private ProgressDialog loadingDialog; // loading
	private VideoView videoView;
	private Context context;

	public VideoViewController(Context contxt, VideoView mVideoView) {

		videoView = mVideoView;
		context = contxt;
	}

	public void start(String videoUrl) {

		loadingDialog = new ProgressDialog(context);
		loadingDialog.setMessage("Loading...");
		loadingDialog.show();
		// loadingDialog.setCancelable(false);

		final MediaController controll = new MediaController(context);
		controll.setMediaPlayer(videoView);
		videoView.setOnPreparedListener(new OnPreparedListener() {
			@Override
			public void onPrepared(MediaPlayer mp) {
				Log.i("************", "call back after media file registing.");
				loadingDialog.dismiss();
			}
		});
		videoView.setVideoPath(videoUrl);
		videoView.setMediaController(controll);
		videoView.requestFocus();
		videoView.start();
		controll.show();
	}
}

 

 

2.主控制Activity

 

package com.example.rtspvideoview;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;
import android.widget.VideoView;

public class MainActivity extends Activity {

	private VideoView mVideoView;
	private ProgressDialog loadingDialog; // loading
	private Button btn;
	private VideoViewController con;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		btn = (Button) findViewById(R.id.control);
		mVideoView = (VideoView) findViewById(R.id.video);
		String url = "rtsp://media2.tripsmarter.com/LiveTV/BTV/";
		con = new VideoViewController(MainActivity.this, mVideoView);

		con.start(url);
		btn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {

				Toast.makeText(MainActivity.this, "點選了按鈕", Toast.LENGTH_LONG)
						.show();
				String url="rtsp://218.205.231.149:554/live/1/35AB46A109903442/0037f42790b7cb14.sdp?id=guest&t=1305311828&en=a4d5d7adaf0eca0d907875009a901a85&rs=wap";
				con.start(url);
			}
		});		
	}

}

 

 3.xml配置檔案 activity_main.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" >

    
    <Button android:id="@+id/control" android:layout_width="match_parent" android:layout_height="match_parent"/>
    <VideoView android:id="@+id/video" android:layout_width="fill_parent" android:layout_height="fill_parent"/>

</RelativeLayout>
 

注意:此程式碼只能在android 3.0以上的系統使用,由於android3.0以上的系統才能解析rtsp視訊流。

相關文章