问题
this is my layout :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/frameLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center" >
<VideoView
android:id="@+id/geoloc_anim"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_gravity="top|center"
android:visibility="visible" />
<FrameLayout
android:id="@+id/placeholder"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</FrameLayout>
</FrameLayout>
</LinearLayout>
this is my activity code:
public class MainActivity extends ActionBarActivity implements OnPreparedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().hide();
VideoView mVideoView = (VideoView) findViewById(R.id.videoview);
Uri uri = Uri.parse("android.resource://" + getPackageName()+"/raw/lst2");
mVideoView.setVideoURI(uri);
mVideoView.requestFocus();
mVideoView.setZOrderOnTop(true);
mVideoView.start();
}
@Override
public void onPrepared(MediaPlayer mp) {
mp.setOnInfoListener(new MediaPlayer.OnInfoListener() {
@Override
public boolean onInfo(MediaPlayer mp, int what, int extra) {
View placeholder = (View) findViewById(R.id.placeholder);
if (what == MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START) {
// video started; hide the placeholder.
placeholder.setVisibility(View.GONE);
return true;
}
return false;
}
});
}
public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {
}
public void surfaceCreated(SurfaceHolder holder) {
}
public void surfaceDestroyed(SurfaceHolder holder) {
}
}
It works fine on android 4.2 but it doesn't work properly on android 2.3 . on android 2.3 , the first time it opens it works find but when close the app and open it again ,a black screen comes , something like this :

and after one minute or so , it goes from a black screen to a white screen but still nothing plays .
Could you help me to solve this problem ?
回答1:
Very late answer. But surely it helps anyone.
set videoView.setZOrderOnTop(true) before start().
https://developer.android.com/reference/android/view/SurfaceView.html#setZOrderOnTop(boolean)
videoView.setVideoURI(Uri.parse(uriString));
videoView.setZOrderOnTop(true);//this line solve the problem
videoView.start();
回答2:
I resolved this with toggling alpha
for VideoView
:
public class VideoPlayer extends VideoView {
....
public VideoPlayer(Context context) {
super(context);
init();
}
public void init() {
setAlpha(0); // hides view
setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.setOnInfoListener(new MediaPlayer.OnInfoListener() {
@Override
public boolean onInfo(MediaPlayer mp, int what, int extra) {
if (what == MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START) {
setAlpha(1); // shows view
return true;
}
return false;
}
});
}
});
I guess you can do the same if you have external VideoView
and have access to it.
回答3:
you need in onPrepared change the Window content! like show toask or other,e.g
@Override
public void onPrepared(final MediaPlayer mp) {
Toast.makeText(getApplicationContext(),"onPrepared",ToasLENGTH_SHORT).show();
mVideoView.setZOrderOnTop(false);
mVideoView.setBackgroundColor(Color.TRANSPARENT);
}
回答4:
remove the mVideoView.start() and change your onPrepared to start your video,
@Override
public void onPrepared(MediaPlayer mp) {
mp.start();
..............
I do not have a device with android less than 4.2 to test with, but this is how I start videos with my application
回答5:
Maybe to late, but I found a solution that worked for me. I combined answers from Sagar Limbani and faljbour
videoView.setVideoURI(Uri.parse(path));
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(final MediaPlayer mp) {
mp.start();
new Handler().post(new Runnable() {
@Override
public void run() {
if(mp.getCurrentPosition() != 0){
View placeholder = findViewById(R.id.placeholder);
placeholder.setVisibility(View.GONE);
} else {
new Handler().postDelayed(this, 50);
}
}
});
}
});
It worked for android 5.1, 5.0 and 4.0
回答6:
I had the same issue. I found that the main reason for that was the use of FrameLayout
as the parent layout. Use RelativeLayout
as the parent layout of the VideoView
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/frameLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center" >
<VideoView
android:id="@+id/geoloc_anim"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_gravity="top|center"
android:visibility="visible" />
<FrameLayout
android:id="@+id/placeholder"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</FrameLayout>
</RelativeLayout>
</LinearLayout>
回答7:
You can solve this by using custom VideoPlayer extends VideoView
public class VideoPlayer extends VideoView {
public VideoPlayer(Context context) {
super(context);
init();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
TyrooLog.i(TAG, "onMeasure");
int width = getDefaultSize(videoWidth, widthMeasureSpec);
int height = getDefaultSize(videoHeight, heightMeasureSpec);
if (videoWidth > 0 && videoHeight > 0) {
if (videoWidth * height > width * videoHeight) {
TyrooLog.i(TAG, "video too tall, correcting");
height = width * videoHeight / videoWidth;
} else if (videoWidth * height < width * videoHeight) {
TyrooLog.i(TAG, "video too wide, correcting");
width = height * videoWidth / videoHeight;
} else {
TyrooLog.i(TAG, "aspect ratio is correct: " + width+"/"+height+"="+mVideoWidth+"/"+mVideoHeight);
}
}
TyrooLog.i(TAG, "setting size: " + width + 'x' + height);
setMeasuredDimension(width, height);
}
}
回答8:
I faced a similar issue and found that adding mp.seekTo(1);
in the onPrepared(MediaPlayer mp)
method fixed it.
来源:https://stackoverflow.com/questions/28577704/android-black-screen-on-displaying-video-by-using-videoview