android mediaplayer 视频修改视频大小 (屏幕尺寸mediaPlayer =...

1 主要是通过更改view 的大小来改变视频的大小。

2 在视频初始化时获得视频的高宽都为0 ,这个必须要注意,必须播放后延时 修改view 大小

3 sufaceview 的布局类是 android.widget.LinearLayout.LayoutParams 这个必须注意(在设置布局时一定要注意父对象的类型)

 

4 在 sufaceview surfaceChanged事件中加入 mediaPlayer.setDisplay(arg0) ,  setDisplay 方法在任意时刻都是可以调用的

 

    try {
				Log.e("m3u8", mediapath);
				mediaPlayer = new MediaPlayer();
				// 开始准备播放
				uiHandler.sendMessage(uiHandler
						.obtainMessage(MediaPlayerMessage.PREPARE));

				// 设置媒体的路径
				mediaPlayer.setDataSource(mediapath);
				// 设置边播放变缓冲
				mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
				mediaPlayer.prepareAsync();
			} catch (Exception e) {
				// TODO: handle exception
				Log.e("meidaplayer err ", e.toString());
			}

			/**
			 * 播放器异常事件
			 */
			mediaPlayer.setOnErrorListener(new OnErrorListener() {

				@Override
				public boolean onError(MediaPlayer mp, int what, int extra) {
					// TODO Auto-generated method stub
					mediaPlayer.release();
					return false;
				}
			});
                        //视频准备完成 可以播放
			mediaPlayer.setOnPreparedListener(new OnPreparedListener() {

				@Override
				public void onPrepared(MediaPlayer mp) {
					// TODO Auto-generated method stub
					try {
						mp.start();
                                                //给ui 界面发送消息 这里有个延时是设置 如果不设置延时 会出现 获得视频的高宽为零的文件 						
uiHandler.sendEmptyMessageDelayed(MediaPlayerMessage.SETSIZE, 1000);

					} catch (Exception e) {
						// TODO: handle exception
						Log.e("start mediaplayer", e.toString());
					}

				}
			});
    case MediaPlayerMessage.SETSIZE:
        int mVideoWidth = mediaPlayerView.mediaPlayer.getVideoWidth();
        int mVideoHeight = mediaPlayerView.mediaPlayer.getVideoHeight();
        int width=_frameLayout_main.getWidth();
	int height=_frameLayout_main.getHeight();
	android.widget.LinearLayout.LayoutParams sufaceviewParams = (android.widget.LinearLayout.LayoutParams) mediapaly_sufaceview
							.getLayoutParams(); 	if ( mVideoWidth * height  > width * mVideoHeight ) { 
		//Log.i("@@@", "image too tall, correcting"); 
		sufaceviewParams.height = width * mVideoHeight / mVideoWidth; 
		} else if ( mVideoWidth * height  < width * mVideoHeight ) { 
		//Log.i("@@@", "image too wide, correcting"); 
		sufaceviewParams.width = height * mVideoWidth / mVideoHeight; 
		}else{
	            sufaceviewParams.height=height;
	            sufaceviewParams.width = width;
	     }
	sufaceviewParams.gravity=Gravity.CENTER;

	mediapaly_sufaceview.setLayoutParams(sufaceviewParams);
	break;

//更在 surface 大小后事件 @Override public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) { // TODO Auto-generated method stub Log.e("Surface h w", String.valueOf(arg2) + " " + String.valueOf(arg3)); if (mediaPlayer != null) { mediaPlayer.setDisplay(arg0); } }

你可能感兴趣的:(android;视频屏幕大小;)