Android会检测来自网址的视频是否为纵向

时间:2014-02-17 17:06:57

标签: android video orientation android-videoview

在我的Android应用中,我有一个自定义视频播放器,可以播放来自网址的视频。但是,当以纵向拍摄视频时,视频会在应用程序中逆时针旋转90度。

有没有办法让我获得视频的方向并相应地旋转视图?我需要动态检测方向,因为应用仍然可以播放风景视频(它是目前没有方向问题)。

以下是显示视频的内容:

// create the video controller
videoController = new MediaController(this);
videoControllerExists = true;
videoController.setAnchorView(mediaHolder);
videoController.setPadding(0, 0, 0, bottomButtonHeight);

s3Video = new VideoView(MediaPreview.this);
s3Video.setVideoURI(Uri.parse(mediaURL));
s3Video.setMediaController(videoController);
// do something here to determine if video is in portrait orientation
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int h = displaymetrics.heightPixels;
int w = displaymetrics.widthPixels;

// add the video player to the view and play
mediaHolder.addView(s3Video);
s3Video.start();
s3Video.bringToFront();
videoController.requestFocus();

这是一个纵向视频,如果你正在尝试这个,你可以测试它: https://dl.dropboxusercontent.com/u/1000620/portrait.mp4

修改

在查看一些示例后,我从VideoView切换到了TextureView,并且能够旋转视频。我仍然不知道如何从URL检测视频的方向,因此所有视频现在都旋转。

如何从网址检测视频的方向?

这是我更新的代码:

public class MediaPreview extends SherlockActivity implements YouTubePlayer.OnInitializedListener, TextureView.SurfaceTextureListener, OnBufferingUpdateListener, OnCompletionListener, OnPreparedListener,
OnVideoSizeChangedListener, MediaPlayerControl {

    private MediaPlayer s3VideoPlayer;
    private TextureView s3TextureView;
    private Surface s;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

    // set up textureview
        s3TextureView = (TextureView) findViewById(R.id.s3_video);
        s3TextureView.setSurfaceTextureListener(this);
    }

    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width,
            int height) {
        s = new Surface(surface);
        if(isFromVideo){
            playAmazonVideo(mediaHolder);
        }

    }

    private void playAmazonVideo(FrameLayout mediaHolder){

            // set up mediacontroller
            videoController = new MediaController(this);
            videoControllerExists = true;
            videoController.setAnchorView(mediaHolder);
            videoController.setPadding(0, 0, 0, bottomButtonHeight);

            // set scale for textureview
            DisplayMetrics displaymetrics = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
            int h = displaymetrics.heightPixels;
            int w = displaymetrics.widthPixels;
            LayoutParams videoLayoutParams = new LayoutParams(h, w);
            videoLayoutParams.gravity = Gravity.CENTER;
            s3TextureView.setScaleX(h/w);
            s3TextureView.setLayoutParams(videoLayoutParams);

       // Setting up video view using Media Player
        try{
            s3VideoPlayer = MediaPlayer.create(this, Uri.parse(mediaURL));
            s3VideoPlayer.setSurface(s);
            s3VideoPlayer.setOnBufferingUpdateListener(this);
            s3VideoPlayer.setOnCompletionListener(this);
            s3VideoPlayer.setOnPreparedListener(this);
            s3VideoPlayer.setScreenOnWhilePlaying(true);
            s3VideoPlayer.setOnVideoSizeChangedListener(this);
            s3VideoPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            s3VideoPlayer.start();
    }

    @Override
    public void onPrepared(MediaPlayer mp) {
        if(isAmazon && mediaURL.length() > 0){
            mediaProgress.setVisibility(View.GONE);
            videoController.setMediaPlayer(this);
            videoController.setEnabled(true);
            videoController.show();     
        }    
    }

    @Override
    public boolean onTouchEvent(MotionEvent event){
        if(isAmazon){
            Log.d(TAG, "showing video controller");
            videoController.show();    
        }
        return false;
    }

 @Override
    public boolean canPause() {
        return true;
    }

    @Override
    public boolean canSeekBackward() {
        return true;
    }

    @Override
    public boolean canSeekForward() {
        return true;
    }

   @Override
    public int getCurrentPosition() {
        return s3VideoPlayer.getCurrentPosition();
    }

    @Override
    public int getDuration() {
        return s3VideoPlayer.getDuration();        
    }

    @Override
    public boolean isPlaying() {
        return s3VideoPlayer.isPlaying();
    }

 @Override
    public void pause() {
        s3VideoPlayer.pause();
    }

    @Override
    public void seekTo(int pos) {
        s3VideoPlayer.seekTo(pos);

    }

    @Override
    public void start() {
        s3VideoPlayer.start();

    }


    }

编辑#2

我一直在研究检测视频方向的选项,并找到了http://wseemann.github.io/FFmpegMediaMetadataRetriever/

不幸的是,这似乎没有获得视频的旋转,即使我看了视频文件的exif,我可以看到方向值是90.所以,如果有人有任何其他解决方案,请分享!

编辑#3

这是我尝试过的其他内容。我看到你可以为MediaPlayer设置一个OnVideoSizeChangedListener来获取视频的宽度和高度。但是对于我正在使用的视频,宽度总是大于高度,并且玩家根据旋转EXIF值知道是否旋转视频。所以我也不能用它......

3 个答案:

答案 0 :(得分:2)

我无法找出轮换问题,所以我最后只更新了我的Rails应用程序,以便在更新时保存视频的轮播。然后,当我从网站上获取视频到Android应用程序时,我还检索视频的旋转并相应地更新纹理视图旋转。它真的不是一个理想的方法,但我找不到任何其他解决方案。

这就是它最终的样子:

        // set scale for textureview
        final Intent i = getIntent();
        videoRotation = i.getIntExtra("videoRotation", 0);
        Log.d(TAG, "setting video rotation...");
        DisplayMetrics displaymetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
        int h = displaymetrics.heightPixels;
        int w = displaymetrics.widthPixels;
        Log.d(TAG, "hxw: " + h + w);
        LayoutParams videoLayoutParams = new LayoutParams(h, w);
        videoLayoutParams.gravity = Gravity.CENTER;
        s3TextureView.setScaleX(h/w);    
        s3TextureView.setRotation(Math.abs(360-videoRotation));
        s3TextureView.setLayoutParams(videoLayoutParams);
        Log.d(TAG, "done setting video rotation");

答案 1 :(得分:0)

您可以获得视频的宽度和高度,然后进行测试 例如if(width> 800)和(!isTablet)然后强制活动为横向。

答案 2 :(得分:0)

以下代码可以提供帮助吗?

activity.setRequestedOrientation(videoView.getWidth() < videoView.getHeight() ? ActivityInfo.SCREEN_ORIENTATION_PORTRAIT : ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);