旋转的机器人VideoView [英] Rotating an android VideoView

查看:433
本文介绍了旋转的机器人VideoView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序只提供了人像模式。在肖像的活动我有一个全屏VideoView。我想要做的就是旋转VideoView(实际视频,videobuffer)90度横向模式。启用活动上Lanscape模式是不是一种选择。 扩展VideoView和画布旋转,也不会因为它的工作是一个SurfaceView不是实际的看法。 有没有什么办法来实现与一个videoView?

My application offers only Portrait mode. In a portrait activity I have a fullscreen VideoView. What I want to do is rotate the VideoView ( the actual video, videobuffer) 90 degrees at Landscape mode. Enabling activity to be on Lanscape mode is not an option. Extending VideoView and canvas rotate will not work as it is a SurfaceView not an actual view. Is there any way to achieve that with a videoView?

推荐答案

VideoView不支持视频的旋转,即使组成矩阵是否设置正确和旋转属性被使用。

VideoView does not support rotation of video even if composition matrix is set correctly and rotation attribute is used.

你可以做的就是用TextureView并设置其属性轮换=90(例如)。然后,它会旋转框架,但宽高比是,你需要处理你自己的东西。为了做到这一点,你可以使用textTureView.setScaleX((screenHeight * 1.0F)/屏幕宽度)

What you can do is to use TextureView and set its attribute rotation="90" (for example). It then will rotate the frames but the aspect ratio is something that you need to handle your self. In order to do so you can use textTureView.setScaleX((screenHeight * 1.0f) / screenWidth)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextureView
        android:id="@+id/playback_video"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true"
        android:rotation="90" />
</RelativeLayout>

然后在code。它应该处理流视频过。但请把它作为一个例子,而不是释放准备code。我改名了一些东西,删除他人,这没有关系的问题,这可能会破坏一些东西。

Then in the code. It should handle streamed video too. But please think of it as of an example rather than release ready code. I renamed some things and removed others, which don't have relation to the question and this might break something.

public class PlaybackActivity extends Activity implements MediaPlayer.OnErrorListener, OnPreparedListener,
        OnCompletionListener, OnVideoSizeChangedListener, OnBufferingUpdateListener, OnInfoListener,
        SurfaceTextureListener
{

    private MediaPlayer mediaPlayer;
    private TextureView videoView;
    private boolean startedPlayback = false;
    private boolean playerReady = false;
    public static final int MEDIA_INFO_NETWORK_BANDWIDTH = 703;

    private void createMediaPlayer() {
        mediaPlayer = new MediaPlayer();
    }

    private void releaseMediaPlayer() {
        if (mediaPlayer != null) {
            mediaPlayer.setSurface(null);
            mediaPlayer.release();
            mediaPlayer = null;
        }
    }

    public void onCompletion(MediaPlayer mp) {
        Log.w(TAG, "Video playback finished");
    }

    @Override
    public boolean onError(MediaPlayer player, int what, int extra) {
        if (what == MediaPlayer.MEDIA_ERROR_UNKNOWN) {
            /*
             * Restart play back in case we did not start anything yet. This may
             * be the case when we tried to tune in in very first secs of the
             * broadcast when there is no data yet.
             */
            if (liveBroadcast && mediaPlayer != null && !mediaPlayer.isPlaying() && !startedPlayback) {
                if (checkCount-- > 0) {
                    mediaPlayer.reset();
                    checkBroadcast();
                } else {
                    Log.w(TAG, "Broadcast finished");
                }
            } else {
                Log.w(TAG, "No media in stream");
            }
        } else if (what == MediaPlayer.MEDIA_ERROR_SERVER_DIED) {
            Log.w(TAG, "Media service died unexpectedly");
        } else {
            Log.w(TAG, "Unknown media error");
        }
        return true;
    }

   @Override
    public boolean onInfo(MediaPlayer mp, int what, int extra) {
        switch (what) {
        case MediaPlayer.MEDIA_INFO_VIDEO_TRACK_LAGGING:
            Log.w(TAG, "Media is too complex to decode it fast enough.");
            startedPlayback = true;
            break;
        case MEDIA_INFO_NETWORK_BANDWIDTH:
            Log.w(TAG, "Bandwith in recent past.");
            break;
        case MediaPlayer.MEDIA_INFO_BUFFERING_START:
            Log.w(TAG, "Start of media bufferring.");
            startedPlayback = true;
            break;
        case MediaPlayer.MEDIA_INFO_BUFFERING_END:
            Log.w(TAG, "End of media bufferring.");
            startedPlayback = true;
            break;
        case MediaPlayer.MEDIA_INFO_BAD_INTERLEAVING:
            Log.w(TAG, "Media is not properly interleaved.");
            break;
        case MediaPlayer.MEDIA_INFO_NOT_SEEKABLE:
            Log.w(TAG, "Stream is not seekable.");
            break;
        case MediaPlayer.MEDIA_INFO_METADATA_UPDATE:
            Log.w(TAG, "New set of metadata is available.");
            break;
        case MediaPlayer.MEDIA_INFO_UNKNOWN:
        default:
            Log.w(TAG, "Unknown playback info (" + what + ":" + extra + ").");
            break;
        }
        return true;
    }

    private void startPlayback() {
        if (mediaPlayer != null) {
            onLoaded(mediaPlayer);
            mediaPlayer.start();
        }
    }

    private void pausePlayback() {
        if (mediaPlayer != null && mediaPlayer.isPlaying())
            mediaPlayer.pause();
    }

    private void resumePlayback() {
        if (mediaPlayer != null && mediaPlayer.isPlaying())
            mediaPlayer.start();
    }

    private void onLoaded(MediaPlayer mp) {
    }

    public void onPrepared(MediaPlayer mp) {
        playerReady = true;
        startPlayback();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        setContentView(R.layout.playback);
        videoView = (TextureView) findViewById(R.id.playback_video);
        videoView.setOnClickListener(videoViewClickHandler);
        videoView.setSurfaceTextureListener(this);
        createMediaPlayer();
    }

   @Override
    protected void onDestroy() {
        releaseMediaPlayer();
        if (surface != null) {
            surface.release();
            surface = null;
        }
        super.onDestroy();
    }

    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
        this.surface = new Surface(surface);
        loadMedia(someurl);
    }

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
        if (this.surface != null) {
            releaseMediaPlayer();
            this.surface.release();
            this.surface = null;
        }
        return true;
    }

    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
    }

    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture surface) {
    }

    @Override
    public void onVideoSizeChanged(MediaPlayer mp, int w, int h) {
        if (w > 0 && h > 0 && !videoSizeSetupDone) {
            Log.w(TAG, "Video size changed: " + w + "x" + h);
            changeVideoSize(w, h);
        }
    }

    private boolean videoSizeSetupDone = false;

    private void changeVideoSize(int width, int height) {
        DisplayMetrics metrics = new DisplayMetrics();
        RelativeLayout.LayoutParams params;

        Utils.getScreenMetrics(this, metrics);
        VideoOrientation orientation = someVideoSource.getVideoOrientation();
        if (orientation == LANDSCAPE) {
            params = new RelativeLayout.LayoutParams(metrics.widthPixels, metrics.heightPixels);
        } else {
            float rotation = orientation == BroadcastVideoOrientation.BroadcastVideoFrontCamera ? -90.0f : 90.0f;
            params = new RelativeLayout.LayoutParams(metrics.heightPixels, metrics.widthPixels);
            float scale = (width * 1.0f) / (height * 1.0f);
            videoView.setRotation(rotation);
            videoView.setScaleX(scale);
        }
        params.addRule(RelativeLayout.CENTER_IN_PARENT, -1);
        videoView.setLayoutParams(params);
        videoSizeSetupDone = true;
    }

    private void loadMedia(String url) {
        if (surface == null)
            return;
        Log.d(App.TAG, "Loading url: " + url);

        startedPlayback = false;
        try {
            mediaPlayer.reset();
            mediaPlayer.setSurface(surface);
            mediaPlayer.setDataSource(url);
            mediaPlayer.setOnPreparedListener(this);
            mediaPlayer.setOnCompletionListener(this);
            mediaPlayer.setOnErrorListener(this);
            mediaPlayer.setOnVideoSizeChangedListener(this);
            mediaPlayer.setScreenOnWhilePlaying(true);
            mediaPlayer.setOnBufferingUpdateListener(this);
            mediaPlayer.setOnInfoListener(this);
            mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mediaPlayer.prepareAsync();
        } catch (Exception e) {
            Log.w(TAG, "Media load failed");
            Utils.alert(this, "Playback Error", e.getMessage(), finishHandler);
        }
    }
}

希望这有助于。我一直在寻找这种解决方案相当长的时间。尝试一切,这似乎是唯一的出路。

Hope this helps. I was looking for this solution for quite long time. Tried everything and this seems to be the only way.

这篇关于旋转的机器人VideoView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆