android视频旋转处理方法
这几天在写视频播放器,采用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.*/@Overridepublic 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 listenersmMediaPlayer = 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();}}@Overrideprotected void onPause() {super.onPause();releaseMediaPlayer();doCleanUp();}@Overrideprotected 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 heightnewWidth = viewWidth;newHeight = (int) (viewWidth * aspectRatio);} else {// limited by short height; restrict widthnewWidth = (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 funtxform.postTranslate(xoff, yoff);mTextureView.setTransform(txform);}@Overridepublic void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {playVideo(surface);}@Overridepublic void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {}@Overridepublic boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {return false;}@Overridepublic 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.*/@Overridepublic 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 listenersmMediaPlayer = 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();}}@Overrideprotected void onPause() {super.onPause();releaseMediaPlayer();doCleanUp();}@Overrideprotected 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();}@Overridepublic void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {playVideo(surface);}@Overridepublic void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {}@Overridepublic boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {return false;}@Overridepublic void onSurfaceTextureUpdated(SurfaceTexture surface) {}}
android视频旋转处理方法相关推荐
- Android 视频旋转、缩放与回弹动效实现(二)
文章目录 Android 视频旋转.缩放与回弹动效实现(二) 功能需求 实现思路 1. 旋转识别 旋转识别:RotateGestureDetector 2. 旋转处理 旋转处理:VideoTouchR ...
- android 视频图片,Android - 视频提取图片方法
下面看使用方法 MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever(); mediaMetadataR ...
- Android 视频手势缩放与回弹动效实现(一)
文章目录 Android 视频手势缩放与回弹动效实现(一) 1. 功能需求 2. 实现原理 2.1 如何检测手势缩放? 1. View.onTouchEvent关键代码 2. ScaleGesture ...
- android 旋转视频_如何在Android上旋转视频
android 旋转视频 There's a war going on out there. You won't see it on the news, you won't read about it ...
- Android屏幕旋转时Activity不重新调用onCreate的方法
2019独角兽企业重金招聘Python工程师标准>>> android屏幕旋转时Activity不重新调用onCreate的方法 当手机转屏时,Activity的onDestroy和 ...
- Android之MediaPlayer播放网络视频的实现方法
前段时间忙于工作,现在有时间来分享一下: 这篇文章主要介绍了Android的MediaPlayer播放网络视频的实现方法,是一个非常实用的功能,需要的朋友可以参考下 前面讲解了MediaPlayer播 ...
- 将视频旋转90度的解决方法
我使用的是HTC G7的手机,用手机拍到的视频拿到电脑上来播放,需要偏着头来看,有时视频甚至是倒立着的.因此,我一直在寻思着如何解决这个问题,但在今天,我终于找到了一个完美的解决方案,能将视频按照需要 ...
- android无法播放视频文件格式,基于Android引入IjkPlayer无法播放mkv格式视频的解决方法...
写在前面 项目中直接引用或者直接编译源码得到的ijkplayer在播放mkv文件时出现(-10000)的错误,去项目github查看了才知道,默认是不支持mkv和rmvb格式视频的播放的. 用了一天时 ...
- Android电视开机倒计时,一种智能电视开机视频的倒计时方法与流程
本发明涉及智能电视领域,尤其涉及一种智能电视开机视频的倒计时方法. 背景技术: 当前智能电视厂商为了提高智能电视广告的带来利润,大都会在智能电视开机过程中内嵌开机视频广告,但是有时广告时间长有时广告时 ...
最新文章
- 夏夏的php开发笔记开写啦
- jmeter正则表达式提取器多模块相互调用
- php判断秒为两位数,判“新”函数:得到今天与明天的秒数
- Pytest跳过执行之@pytest.mark.skip()详解大全
- inkscape使用_如何用Inkscape制作万圣节灯笼
- 第0章 Oracle的安装及相关配置
- CodeForces 780B ——The Meeting Place Cannot Be Changed(二分法)
- pdg转pdf的正确方法!!
- 《中国区块链产业园15强名录》
- 偏微分方程数值解程序设计与实现——数学基础
- jspx格式手机打开,jspx来自埃及的java web快速开发框架
- 不确定性原理的前世今生(转载)
- 当 TiDB 遇到图数据库 | TiDB Hackathon 2020 优秀项目分享
- oracle 范鑫_快速理解数据库中的索引(Indexes in Database)
- Linux 下修改时间和时区
- Typora基本技巧
- HBuilder升级失败,/HBuilder/plugins 被另—个程序占用,请退出占用程序或者重启计算机后重试
- 条码追溯系统解决外贸企业进销存管理
- 用20门编程语言说生日快乐/我爱你
- 计算机ps2定义,软件硬件界面接口定义 bt656 硬件接口定义