5

想要在表面视图中调整视频大小

我正在使用 Surface 视图和视频支架在 Media Player 中播放视频...

// 但在某些设备上实现 MediaPlayerControl 时,例如 Micromax、iball、Akash 平板电脑,它给出了 ACRA 错误跟踪所跟踪的抽象方法不可用的错误。

现在我想解决提到的问题:

  • 如何使媒体播放器控件在所有设备中处于活动状态而不会由于某些设备中的媒体控制器的抽象方法而崩溃。

  • 如何调整视频的表面视图大小。

该代码在三星galaxy gt p1000 OS 2.2和一些更便宜的设备WorldTeck等上运行f9。

但在Creative ziio 7 操作系统版本为 2.2.1 (480*800) 分辨率密度为 160 dpi视频似乎被缩放并从边缘被切断。

代码如下所示:

/* Media Player Control is commented due to force close in some devices say *micromax,Creative ziio, akash etc */

    public class VideoViewerActivity extends Activity implements
            OnBufferingUpdateListener, OnCompletionListener, OnPreparedListener,
            OnVideoSizeChangedListener, SurfaceHolder.Callback {
        // implements  ERROR in some devices , MediaPlayerControl
        private static final String TAG = "MediaPlayerDemo";
        private int mVideoWidth;
        private int mVideoHeight;
        private MediaPlayer mMediaPlayer;
        private SurfaceView mPreview;
        private SurfaceHolder holder;
        private String path = "";
        private Bundle extras;
        private static final String MEDIA = "media";
        private static final int LOCAL_AUDIO = 1;
        private static final int STREAM_AUDIO = 2;
        private static final int RESOURCES_AUDIO = 3;
        private static final int LOCAL_VIDEO = 4;
        private static final int STREAM_VIDEO = 5;
        private boolean mIsVideoSizeKnown = false;
        private boolean mIsVideoReadyToBePlayed = false;
        /* ERROR In Abstract method implementation in the other devices */
        // private MediaController mediaController;
        private Context context;
        private final Handler handler = new Handler();

        /**
         * 
         * Called when the activity is first created.
         */
        @Override
        public void onCreate(Bundle icicle) {
            super.onCreate(icicle);
            setContentView(R.layout.video_layout);
            context = this;
            path = getIntent().getExtras().getString("media");

            Log.v("arpit", "path " + path);
            mPreview = (SurfaceView) findViewById(R.id.surfaceview1);
            holder = mPreview.getHolder();
            holder.addCallback(this);
            holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
            extras = getIntent().getExtras();

        }

        private void playVideo() {
            doCleanUp();
            try {

                // Create a new media player and set the listeners
                File f = new File(path);
                // FileInputStream in = new FileInputStream(f);

                RandomAccessFile randomAccessFile = new RandomAccessFile(f, "r");
                // randomAccessFile.skipBytes(1024);
                FileDescriptor fileDescriptor = randomAccessFile.getFD();

                mMediaPlayer = new MediaPlayer();
                mMediaPlayer.setDataSource(fileDescriptor, Constant.key1.length
                        + Data.deviceID.length(), f.length());

                mMediaPlayer.setDisplay(holder);
                mMediaPlayer.prepare();
                mMediaPlayer.setOnBufferingUpdateListener(this);
                mMediaPlayer.setOnCompletionListener(this);
                mMediaPlayer.setOnPreparedListener(this);
                mMediaPlayer.setOnVideoSizeChangedListener(this);
                mMediaPlayer.setScreenOnWhilePlaying(true);

                // mediaController = new MediaController(this);
                mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

            } catch (Exception e) {
                Log.e(TAG, "error: " + e.getMessage(), e);
                Toast.makeText(context, "supported video was not found",
                        Toast.LENGTH_SHORT).show();
                finish();
            }
        }

        public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
            Log.v(TAG, "onVideoSizeChanged called");
            if (width == 0 || height == 0) {
                Log.e(TAG, "invalid video width(" + width + ") or height(" + height
                        + ")");
                return;
            }
            mIsVideoSizeKnown = true;
            mVideoWidth = width;
            mVideoHeight = height;
            if (mIsVideoReadyToBePlayed && mIsVideoSizeKnown) {
                startVideoPlayback();
            }
        }


        public void surfaceDestroyed(SurfaceHolder surfaceholder) {
            Log.d(TAG, "surfaceDestroyed called");
        }

        public void surfaceCreated(SurfaceHolder holder) {
            Log.d(TAG, "surfaceCreated called");
            playVideo();

        }

        private void doCleanUp() {
            mVideoWidth = 0;
            mVideoHeight = 0;
            mIsVideoReadyToBePlayed = false;
            mIsVideoSizeKnown = false;
        }

        private void startVideoPlayback() {
            Log.v(TAG, "startVideoPlayback");
//also tried and used the below line but nothing good 


// holder.setFixedSize(480, 800);

holder.setFixedSize(mVideoWidth, mVideoHeight);

mMediaPlayer.start();

        }


    }
4

2 回答 2

2

I used TextureView instead of surface view (it allows to rotate video) and set its setScale{X|Y}() appropriately in:

@Override
public void onVideoSizeChanged(MediaPlayer mp, int w, int h) {
    if (w > 0 && h > 0) {
        RelativeLayout.LayoutParams l;
        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        l = new RelativeLayout.LayoutParams(metrics.heightPixels, metrics.widthPixels);
        l.addRule(RelativeLayout.CENTER_IN_PARENT, -1);
        float scale = (metrics.heightPixels * 1.0f) / (metrics.widthPixels * 1.0f);
        videoView.setScaleX(scale);
        videoView.setLayoutParams(l);
        Log.d(YourApp.TAG, "video size: " + mp.getVideoWidth() + "x" + mp.getVideoHeight() + " vertical scale: " + scale);
    }
}

Note that I have rotated my video, this is why I set scale for X.

于 2013-05-07T11:28:41.683 回答
1

嗨,arpit,我也在使用与您提到的播放视频文件相同的设备,我在将控件应用于视频播放器时遇到了同样的问题(抽象方法不可用)。

根据我对这个主题的研发,我得出的结论是,定制的 android 操作系统和这些设备的硬件彼此不兼容,因为它们与三星设备相比质量很差,尽管它们在 android 中有很多方法操作系统,但此类设备不支持。

所以我认为在这些设备中没有解决此类问题的方法。

请尝试在开发任何项目时专注于使用标准设备。这可能对您有很大帮助。

谢谢。:)

于 2012-06-18T10:03:16.237 回答