android视频旋转处理方法
来源:互联网 发布:mac系统安装 编辑:程序博客网 时间:2024/06/09 19:41
这几天在写视频播放器,采用surfaceview搭配mediaplayer或者VideoView进行视频播放,一切都还顺风顺水,当我播放一个方向不对的视频的时候没能自动转换成正确的方向。这时只能靠自己,将视频方向旋转90度。有想法就要开始动手了,可是不论怎么旋转,其他东西都旋转了,可是视频还是纹丝不动,百度了很久没有结果,stack overflow几分钟看不懂英文没办法,可是看到了TextureView这个类,拿来已用,进行旋转是多么的方便多么的快乐啊,终于能够正常的播放了,太开心了。
/* * Copyright (C) 2013 yixia.com * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package io.vov.vitamio.demo;import android.annotation.SuppressLint;import android.app.Activity;import android.graphics.Matrix;import android.graphics.SurfaceTexture;import android.media.AudioManager;import android.os.Bundle;import android.os.Environment;import android.util.Log;import android.view.Surface;import android.view.TextureView;import android.widget.Toast;import io.vov.vitamio.LibsChecker;import io.vov.vitamio.MediaPlayer;import io.vov.vitamio.MediaPlayer.OnBufferingUpdateListener;import io.vov.vitamio.MediaPlayer.OnCompletionListener;import io.vov.vitamio.MediaPlayer.OnPreparedListener;@SuppressLint("NewApi")public class MediaPlayerDemo_setSurface extends Activity implements OnBufferingUpdateListener, OnCompletionListener, OnPreparedListener, TextureView.SurfaceTextureListener { private static final String TAG = "MediaPlayerDemo"; private int mVideoWidth; private int mVideoHeight; private MediaPlayer mMediaPlayer; private TextureView mTextureView; private String path; private Surface surf; private boolean mIsVideoSizeKnown = false; private boolean mIsVideoReadyToBePlayed = false; /** * * Called when the activity is first created. */ @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); if (!LibsChecker.checkVitamioLibs(this)) return; setContentView(R.layout.mediaplayer_3); mTextureView = (TextureView) findViewById(R.id.surface); mTextureView.setSurfaceTextureListener(this); mTextureView.setRotation(90); } @SuppressLint("NewApi") private void playVideo(SurfaceTexture surfaceTexture) { doCleanUp(); try { path = Environment.getExternalStorageDirectory()+"/"+"1.mp4"; if (path == "") { // Tell the user to provide a media file URL. Toast.makeText( MediaPlayerDemo_setSurface.this, "Please edit MediaPlayerDemo_setSurface Activity, " + "and set the path variable to your media file path." + " Your media file must be stored on sdcard.", Toast.LENGTH_LONG).show(); return; } // Create a new media player and set the listeners mMediaPlayer = new MediaPlayer(this, true); mMediaPlayer.setDataSource(path); if (surf == null) { surf = new Surface (surfaceTexture); } mMediaPlayer.setSurface(surf); mMediaPlayer.prepareAsync(); mMediaPlayer.setOnBufferingUpdateListener(this); mMediaPlayer.setOnCompletionListener(this); mMediaPlayer.setOnPreparedListener(this); setVolumeControlStream(AudioManager.STREAM_MUSIC); } catch (Exception e) { Log.e(TAG, "error: " + e.getMessage(), e); } } public void onBufferingUpdate(MediaPlayer arg0, int percent) { // Log.d(TAG, "onBufferingUpdate percent:" + percent); } public void onCompletion(MediaPlayer arg0) { Log.d(TAG, "onCompletion called"); } public void onPrepared(MediaPlayer mediaplayer) { Log.d(TAG, "onPrepared called"); mIsVideoReadyToBePlayed = true; if (mIsVideoReadyToBePlayed) { startVideoPlayback(); } } @Override protected void onPause() { super.onPause(); releaseMediaPlayer(); doCleanUp(); } @Override protected void onDestroy() { super.onDestroy(); releaseMediaPlayer(); doCleanUp(); } private void releaseMediaPlayer() { if (mMediaPlayer != null) { mMediaPlayer.release(); mMediaPlayer = null; } } private void doCleanUp() { mVideoWidth = 0; mVideoHeight = 0; mIsVideoReadyToBePlayed = false; mIsVideoSizeKnown = false; } private void startVideoPlayback() { Log.v(TAG, "startVideoPlayback"); adjustAspectRatio(mMediaPlayer.getVideoWidth(), mMediaPlayer.getVideoHeight()); mMediaPlayer.start(); } /** * Sets the TextureView transform to preserve the aspect ratio of the video. */ private void adjustAspectRatio(int videoWidth, int videoHeight) { int viewWidth = mTextureView.getWidth(); int viewHeight = mTextureView.getHeight(); double aspectRatio = (double) videoHeight / videoWidth; int newWidth, newHeight; if (viewHeight > (int) (viewWidth * aspectRatio)) { // limited by narrow width; restrict height newWidth = viewWidth; newHeight = (int) (viewWidth * aspectRatio); } else { // limited by short height; restrict width newWidth = (int) (viewHeight / aspectRatio); newHeight = viewHeight; } int xoff = (viewWidth - newWidth) / 2; int yoff = (viewHeight - newHeight) / 2; Log.v(TAG, "video=" + videoWidth + "x" + videoHeight + " view=" + viewWidth + "x" + viewHeight + " newView=" + newWidth + "x" + newHeight + " off=" + xoff + "," + yoff); Matrix txform = new Matrix(); mTextureView.getTransform(txform); txform.setScale((float) newWidth / viewWidth, (float) newHeight / viewHeight); //txform.postRotate(10); // just for fun txform.postTranslate(xoff, yoff); mTextureView.setTransform(txform); } @Override public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) { playVideo(surface); } @Override public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) { } @Override public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) { return false; } @Override public void onSurfaceTextureUpdated(SurfaceTexture surface) { }}
TextureView类的介绍。http://www.bubuko.com/infodetail-656030.html
更新后的代码
/* * Copyright (C) 2013 yixia.com * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package io.vov.vitamio.demo;import android.annotation.SuppressLint;import android.app.Activity;import android.graphics.Matrix;import android.graphics.SurfaceTexture;import android.media.AudioManager;import android.os.Bundle;import android.os.Environment;import android.util.Log;import android.view.Surface;import android.view.TextureView;import android.widget.Toast;import io.vov.vitamio.LibsChecker;import io.vov.vitamio.MediaPlayer;import io.vov.vitamio.MediaPlayer.OnBufferingUpdateListener;import io.vov.vitamio.MediaPlayer.OnCompletionListener;import io.vov.vitamio.MediaPlayer.OnPreparedListener;@SuppressLint("NewApi")public class MediaPlayerDemo_setSurface extends Activity implements OnBufferingUpdateListener, OnCompletionListener, OnPreparedListener, TextureView.SurfaceTextureListener { private static final String TAG = "MediaPlayerDemo"; private int mVideoWidth; private int mVideoHeight; private MediaPlayer mMediaPlayer; private TextureView mTextureView; private String path; private Surface surf; private boolean mIsVideoSizeKnown = false; private boolean mIsVideoReadyToBePlayed = false; /** * * Called when the activity is first created. */ @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); if (!LibsChecker.checkVitamioLibs(this)) return; setContentView(R.layout.mediaplayer_3); mTextureView = (TextureView) findViewById(R.id.surface); mTextureView.setSurfaceTextureListener(this); mTextureView.setRotation(90); mTextureView.setScaleX(1280f/720); mTextureView.setScaleY(720f/1280); } @SuppressLint("NewApi") private void playVideo(SurfaceTexture surfaceTexture) { doCleanUp(); try { path = Environment.getExternalStorageDirectory()+"/"+"1.mp4"; if (path == "") { // Tell the user to provide a media file URL. Toast.makeText( MediaPlayerDemo_setSurface.this, "Please edit MediaPlayerDemo_setSurface Activity, " + "and set the path variable to your media file path." + " Your media file must be stored on sdcard.", Toast.LENGTH_LONG).show(); return; } // Create a new media player and set the listeners mMediaPlayer = new MediaPlayer(this, true); mMediaPlayer.setDataSource(path); if (surf == null) { surf = new Surface (surfaceTexture); } mMediaPlayer.setSurface(surf); mMediaPlayer.prepareAsync(); mMediaPlayer.setOnBufferingUpdateListener(this); mMediaPlayer.setOnCompletionListener(this); mMediaPlayer.setOnPreparedListener(this); setVolumeControlStream(AudioManager.STREAM_MUSIC); } catch (Exception e) { Log.e(TAG, "error: " + e.getMessage(), e); } } public void onBufferingUpdate(MediaPlayer arg0, int percent) { // Log.d(TAG, "onBufferingUpdate percent:" + percent); } public void onCompletion(MediaPlayer arg0) { Log.d(TAG, "onCompletion called"); } public void onPrepared(MediaPlayer mediaplayer) { Log.d(TAG, "onPrepared called"); mIsVideoReadyToBePlayed = true; if (mIsVideoReadyToBePlayed) { startVideoPlayback(); } } @Override protected void onPause() { super.onPause(); releaseMediaPlayer(); doCleanUp(); } @Override protected void onDestroy() { super.onDestroy(); releaseMediaPlayer(); doCleanUp(); } private void releaseMediaPlayer() { if (mMediaPlayer != null) { mMediaPlayer.release(); mMediaPlayer = null; } } private void doCleanUp() { mVideoWidth = 0; mVideoHeight = 0; mIsVideoReadyToBePlayed = false; mIsVideoSizeKnown = false; } private void startVideoPlayback() { Log.v(TAG, "startVideoPlayback"); mMediaPlayer.start(); } @Override public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) { playVideo(surface); } @Override public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) { } @Override public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) { return false; } @Override public void onSurfaceTextureUpdated(SurfaceTexture surface) { }}
0 0
- android视频旋转处理方法
- ffmpeg--处理视频旋转
- Android 屏幕旋转生命周期以及处理方法
- android webrtc 视频旋转
- android图片旋转处理
- android 图片旋转处理
- Android处理照片质量、分辨率、旋转角度一些方法
- 暴风影音怎么旋转视频?暴风影音旋转视频方法
- 【Android】 从头搭建视频播放器(4)——屏幕旋转处理
- 设备旋转的处理方法
- 实用视频处理方法
- Android 屏幕旋转的处理
- android 视频处理
- Android中视频处理
- android ffmpeg视频处理
- android videoview,旋转 视频 orientation,rotation 等
- android采集的视频进行旋转
- Android视频旋转并全屏展示
- opencv FullScreen curlib
- bat
- 嵌入式Linux-yum
- 聊天内容的动态加载
- 【反汇编分析】下标寻址和指针寻址
- android视频旋转处理方法
- C编译器剖析_5.3.1 中间代码生成及优化_If语句和复合语句的翻译
- 3-7(1)
- SDKD SingleTest B 题解 2015-04-03
- 将安卓驱动编译成moudle开机后自动启动
- 测土配方施肥专家系统
- Base64的java实现
- C++单例模式的创建
- 备份恢复MySQL数据库的命令