21
  • On my button click I wrote the following code for playing video from my SDCARD (mp4).

    video_view = (VideoView)findViewById(R.id.video_view);
    video_view.setVideoPath("/sdcard/myvideo.mp4");
    video_view.setMediaController(new MediaController(this));
    video_view.start(); 
    
  • I can play recorded video from SDCARD in player.

  • But when I run it on my application in videoview I can hear only sound.
  • Problem : I am not able to see the video.

---- I tried following SO link but still no luck ----

  • link 1
  • link 2

  • Is anybody ever faced this kind of problem ? If yes then how can it solve it ?

Payal Sorathiya
  • 756
  • 1
  • 8
  • 23
Dhruvil Patel
  • 2,910
  • 2
  • 22
  • 39

13 Answers13

68

I've tried every method above, but none of them could work.

Finally, I tried to call this function, then the video appeared.

video_view.setZOrderOnTop(true);

Hope this also works for you.

SeaStar
  • 1,217
  • 1
  • 10
  • 15
  • 1
    I was just having with problem with API 19 Kitkat only, it worked everywhere else. Setting this fixed it. Thanks. – Steven Trigg Feb 24 '14 at 04:04
  • Tried everything under the Sun and this finally worked -- thank ya. – Maurizio Aug 13 '14 at 04:08
  • I think this should be the accepted answer! Thanks a lot! – julianwyz Feb 22 '16 at 22:23
  • 3
    The only issues is that now you can't put anything on top of the video, for example subtitles :( – Miro Mar 30 '16 at 15:27
  • 1
    Any fix on this to put something above the video (subtitle, mute control...)? This is more of a hack than the correct solution! – Ivan May 25 '16 at 18:47
  • @SeaStar video_view.setZOrderOnTop(true); method not found for ScalableVideoView and ScalableVideoView is extends TextureView, Thanks in advance. – Bhavin Patel Sep 13 '16 at 08:39
  • 1
    If you did set a background-color for the VideoView, try removing that backround-color instead of setting the z-order-on-top. – Lensflare May 04 '18 at 10:06
  • I have an overlay I use, so I hide the videoview and then on click event make the videoview visible, but it was not playing. But once I set the zOrderOnTop(true) it was visible. – angryITguy Jan 24 '19 at 06:22
16

SOLUTION 1:

videoView.setZOrderOnTop(true);

this will set the videoview to the top layer; In other words: block everything under it.

SOLUTION 2:

videoView.setBackgroundColor(Color.TRANSPARENT);

SOLUTION 3:

sometimes this is related about your apptheme; In my case, I changed the apptheme from @style/AppTheme to @android:style/Theme.NoTitleBar.Fullscreen fix my problem.

Muhammed Refaat
  • 8,914
  • 14
  • 83
  • 118
liu zhiyuan
  • 171
  • 1
  • 3
2

try this :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <VideoView 
        android:id="@+id/view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"/>

</RelativeLayout>

it may be help you...

Harshit Rathi
  • 1,862
  • 2
  • 18
  • 25
2

Can you give this a try?

video_view = (VideoView)findViewById(R.id.video_view);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(video_view);
mediaController.setMediaPlayer(video_view); //you probably don't need this
video_view.setVideoPath("/sdcard/myvideo.mp4");
video_view.setMediaController(mediaController);
video_view.start(); 
Pawan Kumar
  • 273
  • 1
  • 10
2

I faced the same issue, my video view resized to the expected video resolution but there is nothing on the screen. I tried to use above mentioned solutions but nothing worked.

I tried to set the visibility and that worked, not sure why. This might help someone who come across this post and is facing an issue like me

videoView = (VideoView) findViewById(R.id.viewLiveStream);
videoView.setVisibility(View.VISIBLE);

Here is my XML, not sure why visibility was an issue.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/activity_bg1"
    android:orientation="vertical">

    <Button
        android:layout_marginTop="10dp"
        android:layout_gravity="center"
        android:id="@+id/btnStreaming"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/start_streaming" />
    <TextView
        android:layout_marginTop="10dp"
        android:layout_gravity="center"
        android:id="@+id/txtStreamingUrl"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="textMultiLine"
        android:lines="2" />
    <VideoView
        android:id="@+id/viewLiveStream"
        android:layout_width="640dp"
        android:layout_height="480dp"/>
    <Button
        android:id="@+id/btnPlay"
        android:layout_marginTop="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/play"
        android:layout_gravity="center_vertical|center_horizontal"/>
</LinearLayout>
~                                                                                           
pragnesh
  • 1,240
  • 7
  • 18
1

I had this exact same issue except that I was adding my video view programmatically. I noticed while inspecting the view hierarchy that the parent ViewGroup had it's layer type set to software.

Since the codebase I work on is shared and I didn't notice that the layer type was being setting to software in a subclass of the parent ViewGroup.

So try setting the parent's views layer type to None or Hardware

// Remove this if your parent ViewGroup has this set
//setLayerType(View.LAYER_TYPE_SOFTWARE, null);

// Or set this layer type if the above is not present
setLayerType(View.LAYER_TYPE_NONE, null);
dsmflyer
  • 81
  • 1
  • 6
1

You can do this

video_view = (VideoView)findViewById(R.id.video_view);
video_view.setZOrderMediaOverlay(true);
video_view.setVideoPath("/sdcard/myvideo.mp4");
video_view.setMediaController(new MediaController(this));
video_view.start();
video_view.videoView.setZOrderOnTop(true);

put the setZorderOnTop(true) method after you started the videoView, not before.

Leslie Morejon
  • 117
  • 1
  • 7
0

My solution was placing the VideoView control at top of the layout and no matter if you set the visibility property.

This work for me on a S6 with Android 6.0

adrian
  • 9
  • 2
0
<VideoView android:layout_height="fill_parent"
 android:layout_width="fill_parent"
 android:layout_centerInParent="true" 
android:id="@+id/myVideo"/>


public class Videoplay extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_videoplay);
         VideoView vidView = (VideoView)findViewById(R.id.myVideo);

        MediaController vidControl = new MediaController(this);

        String vidAddress = "https://archive.org/download/ksnn_compilation_master_the_internet/ksnn_compilation_master_the_internet_512kb.mp4";
        Uri vidUri = Uri.parse(vidAddress);

        Toast.makeText(Videoplay.this, "Loading Teaser",Toast.LENGTH_LONG).show();
        vidView.setVideoURI(vidUri);
          vidView.start();   
        vidControl.setAnchorView(vidView);

        vidView.setMediaController(vidControl);


    }

Here I am playing the video from one of the links.. It works perfectly for me.

Hope this helps..

vithika
  • 213
  • 1
  • 5
  • 16
0

The black screen and audio playing happened with me when I did not release the MediaPlayer object. When you play a video in Activity with media player you have to release it when activity is destroyed. And when you open it again a new instance of media player will be created and use the already released resources.

MediaPlayer.release();

Mohammad Sayed
  • 146
  • 1
  • 8
0

I tried all above answers but did not get success after observing some code, i request focus for my video view. And It is now working on all devices, Hope this will work for some Devs!!

private var mediaController: MediaController? = null
private fun prepareVideo() {
    val uri = Uri.fromFile(File(recordedVideoPath))
    mediaController = MediaController(this)
    mediaController?.setAnchorView(videoView)
    videoView.setMediaController(mediaController)
    videoView.requestFocus()
    videoView.setVideoURI(uri)
    videoView.seekTo(2)
    videoView.setZOrderOnTop(true)

    videoView.setOnPreparedListener {
        try {
            mediaController?.show(0)
        } catch (e: Exception) {
            System.out.println("Error in Video")
        }
    }
    videoView.setOnErrorListener { _, _, _ ->
        true
    }
}
amit bansode
  • 339
  • 3
  • 14
0

I have same problem but I find out that It was caused by the media file. My video file has 1080x1920 resolution. My device doesn't support height >= 1080 so VideoView has sound only. I use mmpeg to convert supported resolution This is command i use, you can convert it to any code with library:

ffmpeg -i input.mp4 -vf "scale='-1:min(1080,ih)'" output.mp4
thaovd1712
  • 120
  • 1
  • 1
  • 9
-7

I think you use bad mp4 format. Uses Android specific mp4 format!!!