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.
12 回答
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.
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.
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();
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>
~
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...
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);
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.
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
<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..
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();
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
}
}
I think you use bad mp4 format. Uses Android specific mp4 format!!!