逐个播放视频问题:Android SDK

时间:2011-05-05 06:55:27

标签: android

我有一个要求,我想在第一个视频完成后立即播放第二个视频。我已经实现了这个功能,但我看到的问题是在视频转换之间出现黑屏。我在Galaxy S设备(1 GHZ处理器,512 MB RAM)上尝试这个。

我使用过VideoView,SurfaceView,但仍未解决问题。视频准备似乎需要时间。我使用了推荐的视频格式(H264编码的视频,大约500比特率)。考虑设备Galaxy S 1 GHZ处理器,不应该出现这个问题。

任何相关指针都将受到赞赏。

谢谢! Nilesh制作

以下是代码:

这是代码。我有两个视频:video1和video2。首先播放后,我正在加载Video2。我在这里使用videoview

package com.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.net.Uri;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.VideoView;

public class FirstActivity extends Activity implements OnCompletionListener
{
    /** Called when the activity is first created. */
    VideoView video;
     Toast toast;
    boolean first;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        overridePendingTransition(0, 0);
        setContentView(R.layout.main);
        video = (VideoView) findViewById(R.id.videoView1);
        Uri uri = Uri.parse("android.resource://" + getPackageName() + "/"
                + R.raw.video1);
        video.setOnCompletionListener(this);
        video.setVideoURI(uri);
        video.start();
        first=true;
    }

    public void onCompletion(MediaPlayer mp) {
        // TODO Auto-generated method stub
        if(first){
            Uri uri = Uri.parse("android.resource://" + getPackageName() + "/"
                    + R.raw.video1);
            video.setOnCompletionListener(this);
            video.setVideoURI(uri);
            video.start();
            first=false;
        }
        else{
            Uri uri = Uri.parse("android.resource://" + getPackageName() + "/"
                    + R.raw.video2);
            video.setOnCompletionListener(this);
            video.setVideoURI(uri);
            video.start();
            first=true;
        }
    }

    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
        ImageView image = new ImageView(this);
        image.setBackgroundColor(Color.WHITE);
        image.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.FILL_PARENT));
        toast = new Toast(getApplicationContext());
        toast.setView(image);
        toast.setDuration(Toast.LENGTH_LONG);
        toast.show();
        Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
        startActivity(intent);
        return true;
    }
}

1 个答案:

答案 0 :(得分:1)

你有什么理由不能将这两个视频拼接成一个编辑器中的一个视频吗?它们必须是单独的文件吗?

另外,您尝试使用多个VideoView吗?现在,当video1完成时,你告诉你的VideoView从可能需要一些时间的资源中加载video2。如果您在Activity中有两个VideoView(一个用于video1,一个用于video2)并在onCreate期间加载它们,则可以隐藏当前未播放的视频。在'onCompletion'中,您只需隐藏视频1的VideoView,并使VideoView for video2可见并开始播放,而不是加载视频2。

像这样(未经测试):

public class FirstActivity extends Activity implements OnCompletionListener
{
    /** Called when the activity is first created. */
    VideoView video1;
    VideoView video2;
    Toast toast;
    boolean first;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        overridePendingTransition(0, 0);
        setContentView(R.layout.main);

        video2 = (VideoView) findViewById(R.id.videoView1);
        Uri uri = Uri.parse("android.resource://" + getPackageName() + "/"
                + R.raw.video2);
        video2.setOnCompletionListener(this);
        video2.setVideoURI(uri);
        video2.setVisibility(HIDDEN);        

        video1 = (VideoView) findViewById(R.id.videoView1);
        Uri uri = Uri.parse("android.resource://" + getPackageName() + "/"
                + R.raw.video1);
        video1.setOnCompletionListener(this);
        video1.setVideoURI(uri);
        video1.start();

        first=true;
    }

    public void onCompletion(MediaPlayer mp) {
        // TODO Auto-generated method stub
        if(first){
            video2.setVisibility(HIDDEN);
            video1.setVisibility(VISIBLE);
            video1.start();
            first=false;
        }
        else{
            video1.setVisibility(HIDDEN);
            video2.setVisibility(VISIBLE);
            video2.start();
            first=true;
        }
    }
}