Playing videos one by one issue : Android SDK
I have an requirement in which I want to play the second video immediately once first video gets completed finishing. I have impl开发者_如何转开发emented this functionality, but the issue I am seeing is appearance of black screen between video transition. I am trying this on Galaxy S device (1 GHZ processor, 512 MB RAM).
I have used VideoView, SurfaceView, but still issue is not resolved. It looks like video preparation is taking time. I have used recommended videos format (H264 encoded videos with around 500 bit rate). Considering device Galaxy S 1 GHZ processor, this issue should not occur.
Any related pointer will be appreciated.
Thanks! Nilesh
Here is the code:
Here is the code. I am having two videos : video1 and video2. After playing first, I am loading Video2. Here I am using 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;
}
}
Is there any reason you can't just splice the two videos together into one video in an editor? Do they have to be separate files?
Also, have you tried using multiple VideoViews? Right now, when video1 finishes, you're telling your VideoView to load up video2 from the resources which might be taking some time. If you had two VideoViews in the Activity (one for video1 and one for video2) and loaded both of them up during onCreate, you could hide the one that wasn't currently playing. In 'onCompletion', instead of loading video2 you would just hide the VideoView for video1 and make the VideoView for video2 visible and start playing it.
Something like this (not tested):
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;
}
}
}
精彩评论