Is there a way to have a media file (audio/video) running in a cardview layout? I want to have a video preview of the file inside a card layout, when the user clicks on the card the video/audio is taken fullscreen and played, just like Facebook's feed.
In order to get that kind of functionality you have to solve a few problems:
You cannot use VideoView in a list because it is extending SurfaceView, and if playback is on and user starts scrolling, the visual effect will look like video is trying to catch the scroll. So you need a special VideoView that is based on something else, for example TextureView.
Managing video player state:
in order to start/stop playback we need to call a few methods of MediaPlayer.class:
setDataSource()
prepare()
start()
stop()
reset()
release()
These methods are direct calls to hardware and it may take some time before hardware will respond. So we cannot call it in UI thread. It will block it for more than 16 milliseconds, so user will see a lagging scrolled list. We have to call it from a background thread.
You need to track in run time which view on the screen is active and this view should be playing.
I've created an opensource project called VideoPlayerManager.
It is thoroughly documented and some basic flows are handled in the demo application. It is not ready for production but if you need some reference of how to get this kind of functionality, you can find it there.
P.S. Regarding CardView: it is a regular view. You can put a video player into it and get video playback in CardView.
Related
Phone only with Android 4.0+ and no other third party libraries.
Application uses Navigation Tab of the ActionBar. One of the tab contains the stock VideoView control from ICS streaming live video from a source. I want to have the VideoView keep playing when user switch tabs to view information (call it InformationView).
On a tablet, I don't have this issue because the screen size is bigger and I make VideoView always on screen.
On a phone, screen is smaller therefore tab must be used.
I understand that VideoView probably depends on a visible surface in order to draw the video frame. When user switch tabs, the surface isn't available to VideoView therefore it automatically stops.
I can probably work around this by using a big scrollview that contains both InformationView and VideoView.
Is there any way to force it keep playing (maybe by drawing to an invisible surface) or is there a third party library that mimic the 'tab' behavior by using a giant scrollview so I don't have to write more code?
Note this isn't the same as multiple VideoViews problem which described here: One videoview blocked by another videoview
OK the solution is to hide the fragment that contains the VideoView when switching tabs, hence there is always a 'surface' video view can draw to.
You can custom VideoView same default but that's different you should control holder surface, oncreatSurface, onchangeSurface().
A better implementation of this concept would be not to use the stock VideoView widget, which is really just a marrying together of a SurfaceView and a MediaPlayer, but to divorce these two and use them individually. If your application controls the MediaPlayer directly, is can continue to play the audio track even after the surface disappears.
Making your Activity or Fragment one of the SurfaceHolder.Callback instances attached to the SurfaceView will allow you to know when the surface is or is not available for drawing. The MediaPlayer methods setDisplay() and setSurface() would be then used in your application to attach and detach the video surface when it becomes available in the view hierarchy per that callback.
You may also find the source of VideoView helpful (link) in coming up with your own implementation that accomplishes a similar goal.
I use a SurfaceView for displaying a video in my app, and it is released when I change the content view to somewhere else. My question is quite easy (I guess):
When I want to switch back to the view with the video, I have to provide a new SurfaceView to the player, since an error message tells me that the surface has been released.
Can I somehow simply re-initiate the SurfaceView I defined in my layout.xml to reuse it?
Or does someone have a better idea to pause a video in a MediaPlayer, display an image, and switch back to the video without destroying the SurfaceView?
Cheers!
I have to create media player like youtube player.Actually if i use VideoView for play video and MediaController for controll video(MediaController onlyu provides play,pause,next,pre and seek options).But in my player i have to add one more option i.e bandwidth(I would have three bandwidth and on clicking on bandwidth video will play).So please suggest me.
As far I know, MediaController doesn't give many opportunities to be customized. So, you have two options depending on your UI requirements and the effort you are willing to do.
The hard way: You can just get rid of Android MediaController and
implement your own media controller, since you are not bound to use
it. MediaPlayer (through VideoView) provides you the needed
functions to build it: pause(), play(), resume() methods and event
listeners like onPrepared, onStopped, etc. To display the progress
bar you can use SeekBar, but you will have to code the interaction
with MediaPlayer (this is, the progress bar will not move alone).
The easy way: you can find another place to put your bandwith
button, for instance, on the top of the screen. You might even place
your button over MediaController.
You just have to create a layout properly combining the GUI elements you need. VideoView can be used with other GUI elements like buttons and can be nested within layouts like RelativeLayout and LinearLayout.
So, if you want your bandwidth button adjacent to VideoView, you may use LinearLayout, if you want your bandwith button over the video surface, like MediaController, you may use RelativeLayout, managing properly the layers.
have a look at SurfaceView, that will give you an idea on not bounding your logic with player.
I'm trying to make a video player with VideoView that supports subtitles using a TextView on top of it.
I've already parsed the subtitles but now i need an event that runs every time a frame changes or so to update the subtitles with the current playing position.
I tried making a thread but I am not able to change my TextView in that separate thread. Is there an event on VideoView/MediaPlayer that runs every frame change that I can override to call my updateSubtitle method?
Starting with Android 4.4 , you can put subtitles from within the VideoView itself:
https://www.youtube.com/watch?v=hCRGc2PcmB8#t=130
Coming to your first question, there is no event that happens each video frame. Having said that there are ways of getting current time and synchronize subtitles to the current time. Below links might be of help
A white paper on how to support subtitles in Android
I have a webview that's playing a video, and I want it to appear behind some other views, but I can't seem to make it happen. I've tried simply just sending the foreground views to the front with bringChildToFront(View v), but no cigar. Any thoughts?
Edit: Some more details: The webview plays a video using flash, and the webview is the size of the entire screen. I have some stuff I want to pop up in front of the video, but they only stay behind the video. They're definitely there, as they receive touch events and stuff, but just hidden away from view by the video.
Since the Flash player will essentially pre-empt any other views in the window, you'll just have to do one better over Flash: use another window on top of the main window.
Android allows you to do this by using the PopupWindow class.