注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

姑射道人的博客

博客新地址:nixuchen.com

 
 
 

日志

 
 

rtsp播放器  

2013-11-19 15:45:57|  分类: android |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
package com.example.rtsptest;

import java.io.IOException;
import android.app.Activity;
import android.os.Bundle;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnInfoListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.media.MediaPlayer.OnSeekCompleteListener;
import android.media.MediaPlayer.OnVideoSizeChangedListener;
import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.MediaController;

public class MainActivity extends Activity implements OnCompletionListener,
OnErrorListener, OnInfoListener, OnBufferingUpdateListener,
OnPreparedListener, OnSeekCompleteListener, OnVideoSizeChangedListener,
SurfaceHolder.Callback, MediaController.MediaPlayerControl {

MediaController controller;
Display currentDisplay;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
MediaPlayer mediaPlayer;
View mainView;

TextView statusView;
int videoWidth = 0;
int videoHeight = 0;
boolean readyToPlay = false;
public final static String LOGTAG = "STREAMING_VIDEO_PLAYER";
String url1 = "rtsp://192.168.168.238/a.mp4";
String url2="rtmp://192.168.168.238/vod/mp4:cf.mp4";
String url3="rtsp://192.168.168.238/cf.mp4";
String url4="rtsp://184.72.239.149/vod/mp4:BigBuckBunny_115k.mov";
String url5="rtsp://192.168.168.238/marry.mp4";

@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

mainView = this.findViewById(R.id.MainView);
statusView = (TextView) this.findViewById(R.id.StatusTextView);
surfaceView = (SurfaceView) this.findViewById(R.id.SurfaceView);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
mediaPlayer = new MediaPlayer();
statusView.setText("MediaPlayer Created");
mediaPlayer.setOnCompletionListener(this);
mediaPlayer.setOnErrorListener(this);
mediaPlayer.setOnInfoListener(this);
mediaPlayer.setOnPreparedListener(this);
mediaPlayer.setOnSeekCompleteListener(this);
mediaPlayer.setOnVideoSizeChangedListener(this);
mediaPlayer.setOnBufferingUpdateListener(this);

String filePath = url5;
try {
mediaPlayer.setDataSource(filePath);
} catch (IllegalArgumentException e) {
Log.v(LOGTAG, e.getMessage());
finish();
} catch (IllegalStateException e) {
Log.v(LOGTAG, e.getMessage());
finish();
} catch (IOException e) {
Log.v(LOGTAG, e.getMessage());
finish();
}
statusView.setText("MediaPlayer DataSource Set");
currentDisplay = getWindowManager().getDefaultDisplay();
controller = new MediaController(this);
controller.setKeepScreenOn(true);
}

@Override
public boolean canPause() {
// TODO Auto-generated method stub
return true; 
}

@Override
public boolean canSeekBackward() {
// TODO Auto-generated method stub
return true;
}

@Override
public boolean canSeekForward() {
// TODO Auto-generated method stub
return true;
}

@Override
public int getAudioSessionId() {
// TODO Auto-generated method stub
return 0;
}

@Override
public int getBufferPercentage() {
// TODO Auto-generated method stub
return 0;
}

@Override
public int getCurrentPosition() {
// TODO Auto-generated method stub
return mediaPlayer.getCurrentPosition();
}

@Override
public int getDuration() {
// TODO Auto-generated method stub
return mediaPlayer.getDuration();
}

@Override
public boolean isPlaying() {
// TODO Auto-generated method stub
return mediaPlayer.isPlaying();
}

@Override
public void pause() {
// TODO Auto-generated method stub

if (mediaPlayer.isPlaying()) { 
mediaPlayer.pause(); 
}

@Override
public void seekTo(int pos) {
// TODO Auto-generated method stub
mediaPlayer.seekTo(pos);
}

@Override
public void start() {
// TODO Auto-generated method stub
mediaPlayer.start(); 
}

@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
Log.v(LOGTAG, "surfaceChanged Called");

}

@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
Log.v(LOGTAG, "surfaceCreated Called");
mediaPlayer.setDisplay(holder);
statusView.setText("MediaPlayer Display Surface Set");
try {
mediaPlayer.prepareAsync();
} catch (IllegalStateException e) {
Log.v(LOGTAG, "IllegalStateException " + e.getMessage());
finish();
}
statusView.setText("MediaPlayer Preparing");
}

@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
// TODO Auto-generated method stub
Log.v(LOGTAG, "surfaceDestroyed Called");
}

@Override
public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
// TODO Auto-generated method stub
Log.v(LOGTAG, "onVideoSizeChanged Called"); 
videoWidth = mp.getVideoWidth(); 
videoHeight = mp.getVideoHeight(); 
Log.v(LOGTAG, "Width: " + videoWidth); 
Log.v(LOGTAG, "Height: " + videoHeight); 
if (videoWidth > currentDisplay.getWidth() 
|| videoHeight > currentDisplay.getHeight()) { 
float heightRatio = (float) videoHeight 
/ (float) currentDisplay.getHeight(); 
float widthRatio = (float) videoWidth 
/ (float) currentDisplay.getWidth(); 
if (heightRatio > 1 || widthRatio > 1) { 
if (heightRatio > widthRatio) { 
videoHeight = (int) Math.ceil((float) videoHeight 
/ (float) heightRatio); 
videoWidth = (int) Math.ceil((float) videoWidth 
/ (float) heightRatio); 
} else { 
videoHeight = (int) Math.ceil((float) videoHeight 
/ (float) widthRatio); 
videoWidth = (int) Math.ceil((float) videoWidth 
/ (float) widthRatio); 
surfaceView.setLayoutParams( 
new LinearLayout.LayoutParams(videoWidth, videoHeight)); 
}

@Override
public void onSeekComplete(MediaPlayer arg0) {
// TODO Auto-generated method stub
Log.v(LOGTAG, "onSeekComplete Called");
}

@Override
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
Log.v(LOGTAG, "onPrepared Called"); 
statusView.setText("MediaPlayer Prepared"); 
videoWidth = mp.getVideoWidth(); 
videoHeight = mp.getVideoHeight(); 
Log.v(LOGTAG, "video Width: " + videoWidth); 
Log.v(LOGTAG, "Height: " + videoHeight); 
if (videoWidth > currentDisplay.getWidth() 
|| videoHeight > currentDisplay.getHeight()) { 
float heightRatio = (float) videoHeight 
/ (float) currentDisplay.getHeight(); 
float widthRatio = (float) videoWidth 
/ (float) currentDisplay.getWidth(); 
if (heightRatio > 1 || widthRatio > 1) { 
if (heightRatio > widthRatio) { 
videoHeight = (int) Math.ceil((float) videoHeight 
/ (float) heightRatio); 
videoWidth = (int) Math.ceil((float) videoWidth 
/ (float) heightRatio); 
} else { 
videoHeight = (int) Math.ceil((float) videoHeight 
/ (float) widthRatio); 
videoWidth = (int) Math.ceil((float) videoWidth 
/ (float) widthRatio); 
}
surfaceView.setLayoutParams( 
new LinearLayout.LayoutParams(videoWidth, videoHeight)); 
controller.setMediaPlayer(this); 
controller.setAnchorView(this.findViewById(R.id.MainView)); 
controller.setEnabled(true); 
controller.show(); 
mp.start(); 
statusView.setText("MediaPlayer Started"); 

}

public void onBufferingUpdate(MediaPlayer mp, int bufferedPercent) { 
statusView.setText("MediaPlayer Buffering: " + bufferedPercent + "%"); 
Log.v(LOGTAG, "MediaPlayer Buffering: " + bufferedPercent + "%"); 

public boolean onInfo(MediaPlayer mp, int whatInfo, int extra) {
statusView.setText("MediaPlayer onInfo Called");
if (whatInfo == MediaPlayer.MEDIA_INFO_BAD_INTERLEAVING) {
Log.v(LOGTAG, "Media Info, Media Info Bad Interleaving " + extra);
} else if (whatInfo == MediaPlayer.MEDIA_INFO_NOT_SEEKABLE) {
Log.v(LOGTAG, "Media Info, Media Info Not Seekable " + extra);

} else if (whatInfo == MediaPlayer.MEDIA_INFO_UNKNOWN) {
Log.v(LOGTAG, "Media Info, Media Info Unknown " + extra);
} else if (whatInfo == MediaPlayer.MEDIA_INFO_VIDEO_TRACK_LAGGING) {
Log.v(LOGTAG, "MediaInfo, Media Info Video Track Lagging " + extra);
} else if (whatInfo == MediaPlayer.MEDIA_INFO_METADATA_UPDATE) {
Log.v(LOGTAG, "MediaInfo, Media Info Metadata Update " + extra);
}
return false;
}

@Override
public boolean onError(MediaPlayer mp, int whatError, int extra) {
// TODO Auto-generated method stub

Log.v(LOGTAG, "onError Called");
statusView.setText("MediaPlayer Error");
if (whatError == MediaPlayer.MEDIA_ERROR_SERVER_DIED) {
Log.v(LOGTAG, "Media Error, Server Died " + extra);
} else if (whatError == MediaPlayer.MEDIA_ERROR_UNKNOWN) {
Log.v(LOGTAG, "Media Error, Error Unknown " + extra);
}
return false;

}

@Override
public void onCompletion(MediaPlayer arg0) {
// TODO Auto-generated method stub
Log.v(LOGTAG, "onCompletion Called");
statusView.setText("MediaPlayer Playback Completed");
}

@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
if (controller.isShowing()) { 
//controller.hide(); 
} else { 
controller.show(); 
return false;
}

@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
mediaPlayer=null;
controller=null;
}
}

  评论这张
 
阅读(1313)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017