I want to play video inside my webview. I have examined different example but my HTML a bit different.
{\"mp4\":[{\"name\":\"240\",\"file\":\"http:\\\/\\\/xxxx.xxxx.net\\\/lt\\\/2016\\\/04\\\/24\\\/test_320x240.mp4\",\"type\":\"video\\\/mp4\"}
I can download HTML code and used function;
webview.loadData(myHTML,"text/html; charset=UTF-8", null);)
However, I can't display videos in my application. Thank you for helping.
You can try playing the video using VideoView rather than trying to load it in a webview.Below is the sample code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
videoView = (VideoView) findViewById(R.id.videoView);
File file = new File(Environment.getExternalStorageDirectory(), "rhyme.mp4");
videoView.setVideoPath(file.getAbsolutePath());
videoView.setMediaController(new MediaController(this));
videoView.start();
}
Also if you want to stream some video files from internet, you can go ahead with ExoPlayer. Here is the link
Kindly mark this reply as answer if it solves your problem.
Related
I am trying to display a mp4 video from a raw folder. The video is supposed to play in a videoview automatically when the app is open. I want the video to begin as soon as the activity is launched (This is the launcher activity). I also want the video to loop and have no sound. My xml is below.
<VideoView
android:id="#+id/launcherVideo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/linearLayout" />
This is the code I have.
String fileName = "android.resource://"+ getPackageName()+"/raw/launchervideo";
VideoView mvideo = (VideoView) findViewById(R.id.launcherVideo);
mvideo.setVideoPath(Launcher);
MediaController controller = new MediaController(this);
mvideo.setMediaController(controller);
mvideo.requestFocus();
mvideo.start();
}
This code currently does not do anything but display a blank screen when I run the app and I don't know why. Can anyone help out?
I don't see you use this variable, and I don't see how you define Launcher variable.
String fileName = "android.resource://"+ getPackageName()+"/raw/launchervideo";
to open video file from raw folder you can do this way:
Uri uri = Uri.parse(fileName);
mvideo.setVideoURI(uri);
mvideo.start()
I hope it can help solve your problem
Thanks for the help guys, I figured it out.
VideoView videoView = (VideoView) findViewById(R.id.launcherVideo);
Uri src = Uri.parse("android.resource://com.package/raw/video_name");
videoView.setVideoURI(src);
//videoView.setMediaController(new MediaController(this));
videoView.start();
This worked for me.
I try to use VLC to Streaming a video to my android phone
here is my code
public class VideoMain extends Activity {
VideoView myVideoView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_main);
VideoView myVideoView = (VideoView)findViewById(R.id.videoView1);
myVideoView.setVideoPath("http://140.118.208.220:8080");
myVideoView.setMediaController(new MediaController(this));
}
and the I use VLC streaming wizard->streaming to network->myVideo->HTTP->MPEG2->FINISH
But it seem to not work at all
please help my solve this:)
Android supports 3GPP (.3gp) and MPEG-4 (.mp4) no MPEG-2 in video for more detail see Android Developer Media Formats.
Am interested in creating own movie which should be started when the button "Start" is clicked. The movie should be played within the app, it should not use the media player or flash player. Is there any ways to play movie without player ?
Thanks in advance.
Try this code
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
getWindow().setFormat(PixelFormat.TRANSLUCENT);
videoHolder = new VideoView(this);
videoHolder.setMediaController(new MediaController(this));
setContentView(videoHolder);
// videoHolder.setVideoURI(Uri.parse("file:///sdcard/video.3gp"));
videoHolder.setVideoURI(Uri.parse("file:///sdcard/video.mp4"));
videoHolder.requestFocus();
videoHolder.start();
}
Have you taken a look at the VideoView Class? I believe that this is what you are looking for :)
Simple use videoview. save file in assets and use videoview in main.xml
I'm trying to stream a video from VLC to an HTC G1. After several "this should work" I found a sout-chain that allowed me to watch the stream via VLC. I am also able to hear the audio on the android.
The sout-chain I'm currently using:
vlc some_file.mp4 -I http --sout "#transcode{soverlay,ab=128,samplerate=44100,channels=2,acodec=mp4a,vcodec=h264,width=480,height=270,vfilter="canvas{width=480,height=270,aspect=16:9}",fps=25,vb=800,venc=x264{level=12,no-cabac,subme=20,threads=4,bframes=0,min-keyint=1,keyint=50}}:gather:rtp{mp4a-latm,sdp=rtsp://0.0.0.0:5554/stream.sdp}"
That's what I'm doing on the droid:
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
vv = (VideoView) findViewById(R.id.video_view);
vv.setVideoURI(Uri.parse("rtsp://<local_ip>:5554/stream.sdp"));
vv.start();
}
I tried to keep it as minimal as possible (this is actually an example I found in another thread here).
I also tried using MediaPlayer:
MediaPlayer mp = MediaPlayer.create(this, Uri.parse("rtsp://<local_ip>:5554/stream.sdp"));
mp.setDisplay(vv.getHolder());
mp.start();
I use setDisplay(SurfaceHolder) cause someone mentioned MediaPlayer otherwise wont know what display to use.
Any idea what I'm missing or doing wrong?
Edit: I hinted the the file with MP4Box
First, I think there's problem with your audio encoder, it shows "MPEG-1/2 Video" is not an audio encoder, would you please try "mpga"?
and another problem is you are trying to fit the frames into a specified size, width=480,height=270, could you delete this part?
my command works:
vlc /Users/chenyu/Sites/BBC.mp4 -I http --sout "#transcode{soverlay,ab=128,samplerate=44100,channels=2,acodec=mpga,vcodec=h264,fps=25,vb=800,venc=x264{level=1,no-cabac,subme=20,threads=4,bframes=0,min-keyint=1,keyint=50}}:gather:rtp{mp4a-latm,sdp=rtsp://10.0.1.2:5554/stream.sdp}"
also could you try the following code on android side?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final VideoView vidView = (VideoView)findViewById(R.id.myVideo);
MediaController vidControl = new MediaController(this);
vidControl.setAnchorView(vidView);
vidView.setMediaController(vidControl);
vidView.setVideoPath("rtsp://10.0.1.2:5554/stream.sdp");
vidView.start();
}
I want to play video content from HttpResponse object. The content is downloading from a streaming sever.
My requirement is to create a HTTP POST request to the server. Request contains the URL of video, username and password of the user for authentication purpose.
I wish to know how can we create a HTTP POST request and play/download the video.
Kindly provide some hints, steps/code to proceed.
Thanks,
I am not 100% sure, but I think that you can't stream most of the video due to the way the format stores the meta data of the video. That's why you tube has to convert your video files instead of just showing them in any format.
There are protocols that encapsulate this metadata and allows you to stream any video (that's what youtube mobile does). You should take a look at RTSP: http://en.wikipedia.org/wiki/Real_Time_Streaming_Protocol
If you use an rtsp link in a videoView it should stream the video flawlessly. The point is that your problem is server and not client related.
As an exercise you could grab an rtsp link from m.youtube.com and just pass to a videoView using setVideoPath and it should work.
If can't change the server implementation then you probably them I guess your solutions are:
1) Download and decode the video yourself, you would have to handle all the metadata and ensure that the video does work though. You could, theoretically, compile ffmpeg for android to help you with that, but I couldn't compile it to android with the network option on. That is a lot of work.
2) Write your own implementation of RTSP or other streaming protocol. Download the video in a thread and them create a local server in the device to stream that video file to a videoView instance. I actually have this working on an app. Android doesn't support the specific protocol that the client's servers use and I had to get it working. It took me a full month to do this. I can't post any example code because it's all the client's, but I could give you some further info on this if you are interested.
Either way, if you can't change the server/format of the video then you should prepare for a whole lot of work.
check the below code for showing a url http or rtsp in Video View.
Now before invoking your videoview .. just get
public class VideoViewDemo extends Activity {
/**
* TODO: Set the path variable to a streaming video URL or a local media
* file path.
*/
private String path = "";
private VideoView mVideoView;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.videoview);
mVideoView = (VideoView) findViewById(R.id.surface_view);
if (path == "") {
// Tell the user to provide a media file URL/path.
Toast.makeText(
VideoViewDemo.this,
"Please edit VideoViewDemo Activity, and set path"
+ " variable to your media file URL/path",
Toast.LENGTH_LONG).show();
} else {
/*
* Alternatively,for streaming media you can use
* mVideoView.setVideoURI(Uri.parse(URLstring));
*/
mVideoView.setVideoPath(path);
mVideoView.setMediaController(new MediaController(this));
mVideoView.requestFocus();
}
}
}
Now since your requirements are different first make http post request to your server ... get the desired input stream.. write that stream as mp4 to your local file system. then you can use below code to play content
// this can be path on file system
String path="/adf.mp4" ;
public void playVideo() {
MediaController mediaController = new MediaController(this);
mediaController.setMediaPlayer(videoView);
videoView.setVideoPath(path);
videoView.setMediaController(mediaController);
videoView.requestFocus();
videoView.start();
mediaController.show();
I tried this and it worked for me :D no rtsp no nothing, just a url with a mp4 video and it played!
public class VideoActivity extends Activity {
public static final String KEY = "video_id";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fotogaleria_video);
Intent i = getIntent();
if(i.hasExtra(KEY)){
String url = "https://sites.google.com/site/tempmp4download/bnm.mp4";
VideoView vv = (VideoView)findViewById(R.id.fotogaleria_video);
// vv.setVideoPath(url);
vv.setVideoURI(Uri.parse(url));
vv.setMediaController(new MediaController(this));
vv.requestFocus();
vv.start();
}
}
}
Look into following links
http://www.softwarepassion.com/android-series-get-post-and-multipart-post-requests/
http://w3mentor.com/learn/java/android-development/android-http-services/performa-a-http-post-request-with-the-httpclient-in-android/
For streaming, I think you have to download the full file, and then show it in the video player.