在Android中播放视频时出现黑屏

时间:2011-03-14 08:28:33

标签: android animation android-layout media-player layer

我正在使用Android媒体播放器播放视频还使用另一个图层在视频顶部显示动画。 (视频文件出现在SD卡上。)

动画基本上是一个与视频同步的裁剪图像( TranslateAnimation )。

每当我点击按钮播放视频时,我会立即在中心处看到裁剪后的图像,然后是视频&动画都开始了。我希望以某种方式通过首先加载视频来避免这种情况。然后将图像强加在顶部。

我怎样才能做到这一点?还是有更好的方法吗?

以下是相关的代码段:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.videolayer);
        this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
        try{
            Intent myint = this.getIntent();
            photoPath = myint.getStringExtra("photo_path");
            large_bitmap_path = myint.getStringExtra("large_bitmap_path");
            bitmap_path = myint.getStringExtra("bitmap_path");

            imv1 = (ImageView)findViewById(R.id.imv1);            
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inPreferredConfig = Bitmap.Config.ARGB_8888;
            bitmap = BitmapFactory.decodeFile(photoPath, options);
            imv1.setImageBitmap(bitmap);
            getWindowManager().getDefaultDisplay().getMetrics(dm);
            videoView = (VideoView) findViewById(R.id.VideoView);

            File f=new File(Environment.getExternalStorageDirectory(), "videos/"+Videoid[GalleryVideo.galleryVideoCounter]+".m4v");
            Uri video = Uri.parse(f.getAbsolutePath());            

            videoView.setVideoURI(video);
            videoView.setOnPreparedListener(this);
            videoView.setOnCompletionListener(video_listener);
            videoView.start();            
            lower = (View)findViewById(R.id.lower);
            middle = (View)findViewById(R.id.middle);
            upper = (View)findViewById(R.id.upper);
            upper.setVisibility(4);

            .......
            RelativeLayout ll = (RelativeLayout) findViewById(R.id.LinearLayout01);
            ll.setOnTouchListener(video_tap);
            startTime = System.currentTimeMillis();


        } catch(Exception e){
            e.printStackTrace();
        }
    }

.................

@Override
    public void onPrepared(MediaPlayer mp) {
        // TODO Auto-generated method stub
        videoHeight = videoView.getMeasuredHeight();
        videoWidth = videoView.getMeasuredWidth();
        displayWidth = getApplicationContext().getResources().getDisplayMetrics().widthPixels;
        displayHeight = getApplicationContext().getResources().getDisplayMetrics().heightPixels;
        //video is ready for play, now start animation
        anim_start = 1;
        launchAnimation();            
    }

    private Runnable resumeAnim =new Runnable() {
        public void run() {
            launchAnimation();
        }
    };

1 个答案:

答案 0 :(得分:0)

您应该使用线程来播放视频并使用处理程序显示图像,或者您可以使用AsyncTask来解决黑屏问题。我遇到了同样的问题,并使用线程和处理程序摆脱了。