i have issues in android video playing. am geting videos from server and set to videoview but videos is playing in backgroung am not able to see that videos ,please refer my code what i have tried.
XML FILE
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<VideoView
android:id="#+id/eventDetailsPage_videosView"
android:layout_width="160dp"
android:layout_height="100dp"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:background="#drawable/ic_eventvideo_image" />
</LinearLayout>
Java Class
Uri uri=Uri.parse(mEventDetailsUtil1.getAttachmentsUrls().get(0).getAttach_url());
VideoView mVideosView = (VideoView) findViewById(R.id.eventDetailsPage_videosView);
MediaController mc = new MediaController(this);
mc.setAnchorView(mVideosView);
mc.setMediaPlayer(mVideosView);
mVideosView.setMediaController(mc);
mVideosView.setVideoURI(uri);
mVideosView.requestFocus();
mVideosView.start();
the video is playing when opening the activity , but i don't want like that i need when i click on VideoView i need to open video full screen.
Try this :
VideoView mVideoview;
mVideoview = (VideoView) findViewById(R.id.VideoView);
mPlayServer = (Button) findViewById(R.id.mServerbutton);
mPlayServer.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
playVideo(SERVER_VIDEO_URL);
}
});
private void playVideo(SERVER_VIDEO_URL)
try {
// Start the MediaController
MediaController mediacontroller = new MediaController(mContext);
mediacontroller.setAnchorView(mVideoview);
// Get the URL from String VideoURL
mVideo = Uri.parse(SERVER_VIDEO_URL);
mVideoview.setMediaController(mediacontroller);
mVideoview.setVideoURI(mVideo);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
mVideoview.requestFocus();
mVideoview.setOnPreparedListener(new OnPreparedListener() {
// Close the progress bar and play the video
public void onPrepared(MediaPlayer mp) {
mVideoview.start();
}
});
mVideoview.setOnCompletionListener(new OnCompletionListener() {
// Called when video is completed
public void onCompletion(MediaPlayer mp) {
}
});
}
To show in fullscreen use video view in new xml file with height and width as fill parent and start new activity with Intent.
Hope this helps.
Android does not support the resizing of videos, you may want to look into a Third Party library that you can utilize. By default android does not have this. I got the same problem while applying controls to video player.
Related
How to do an autoplay like facebook in a listview, only one video at a time, and pause it when it comes out of focus.
Below is the code for playing video after OnClick, on Video View in Another Activity, I want it like Facebook that user scrolls and Video plays Automatically.
Videos_URL = intent.getExtras().getString("VideoUrl");
// Find your VideoView in your video_main.xml layout
myVideoView = (VideoView) findViewById(R.id.videoview);
if (mediaControls == null) {
mediaControls = new MediaController(this);
}
// Create a progressbar
progressDialog = new ProgressDialog(this);
// Set progressbar title
progressDialog.setTitle("APPNAME Video ");
// Set progressbar message
progressDialog.setMessage("Loading...");
progressDialog.setCancelable(false);
// Show progressbar
// Show progressbar
progressDialog.show();
try {
Uri video = Uri.parse(Videos_URL);
myVideoView.setVideoURI(video);
myVideoView.setMediaController(mediaControls);
myVideoView.start();
Is it possible to get Auto Play in Video View in Android??
On getview method of adapter you do like this,
it will auto play video when it will come on view.
videoView.setVideoPath(videoPath);
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(final MediaPlayer mp) {
//seekbar.
mp.start();
} catch (Exception e) {
e.printStackTrace();
}
Actually i made one application for iphone and android device.. and in that video feature is available so video uploaded by iphone device,video (mp4) is cant played in android device so please any body help me
pDialog = new ProgressDialog(this);
// Set progressbar message
pDialog.setMessage("Buffering...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
// Show progressbar
pDialog.show();
try {
// Start the MediaController
MediaController mediacontroller = new MediaController(
PlayVideoViewFromURLActivity.this);
mediacontroller.setAnchorView(mVideoView);
// Get the URL from String VideoURL
Uri videoUri = Uri.parse(vidUrl); // vidUrl is url of video which on server
mVideoView.setMediaController(mediacontroller);
mVideoView.setVideoURI(videoUri);
} catch (Exception e) {
e.printStackTrace();
}
mVideoView.requestFocus();
mVideoView.setOnPreparedListener(new OnPreparedListener() {
// Close the progress bar and play the video
public void onPrepared(MediaPlayer mp) {
pDialog.dismiss();
mVideoView.start();
}
});
mVideoView.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
if (pDialog.isShowing()) {
pDialog.dismiss();
}
finish();
}
});
full Logcat error :
08-13 09:45:10.062: D/MediaPlayer(1929): Couldn't open file on client side, trying server side
08-13 09:45:11.770: E/MediaPlayer(1929): error (1, -2147483648)
08-13 09:45:11.790: E/MediaPlayer(1929): Error (1,-2147483648)
08-13 09:45:11.790: D/VideoView(1929): Error: 1,-2147483648
There is an alternative approach for streaming video using the MediaPlayer class,like we use it in creating a music player. You can stream media, including video, to a MediaPlayer object using a surface view. For example, you could use the following layout:
<RelativeLayout 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:background="#000000"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<SurfaceView
android:id="#+id/surfView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
MainActivity.java
public class TestAct extends Activity implements SurfaceHolder.Callback, OnPreparedListener
{
private MediaPlayer mediaPlayer;
private SurfaceHolder vidHolder;
private SurfaceView vidSurface;
String vidAddress = "https://archive.org/download/ksnn_compilation_master_the_internet/ksnn_compilation_master_the_internet_512kb.mp4";
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
vidSurface = (SurfaceView) findViewById(R.id.surfView);
vidHolder = vidSurface.getHolder();
vidHolder.addCallback(this);
}
#Override
public void surfaceCreated(SurfaceHolder arg0)
{
try
{
mediaPlayer = new MediaPlayer();
mediaPlayer.setDisplay(vidHolder);
mediaPlayer.setDataSource(vidAddress);
mediaPlayer.prepare();
mediaPlayer.setOnPreparedListener(this);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
}
catch (Exception e)
{
e.printStackTrace();
}
}
#Override
public void onPrepared(MediaPlayer mp)
{
mediaPlayer.start();
}
#Override
public void surfaceDestroyed(SurfaceHolder holder)
{
}
#Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3)
{
}
}
Hope this helps :)
Issue:
It is issue about conflict of encoding-decoding between the video and the device's platform support.
Solution:
I would insist to use Vitamio library to play the videos in android.
It is pretty simple to use and reliable also.
Example:
You can add it to your xml file like VideoView,
<io.vov.vitamio.widget.VideoView
android:id="#+id/surface_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
You can easily control the player,
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;
private EditText mEditText;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
if (!LibsChecker.checkVitamioLibs(this))
return;
setContentView(R.layout.videoview);
mEditText = (EditText) findViewById(R.id.url);
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();
return;
} else {
/*
* Alternatively,for streaming media you can use
* mVideoView.setVideoURI(Uri.parse(URLstring));
*/
mVideoView.setVideoPath(path);
mVideoView.setMediaController(new MediaController(this));
mVideoView.requestFocus();
mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
// optional need Vitamio 4.0
mediaPlayer.setPlaybackSpeed(1.0f);
}
});
}
}
public void startPlay(View view) {
String url = mEditText.getText().toString();
path = url;
if (!TextUtils.isEmpty(url)) {
mVideoView.setVideoPath(url);
}
}
public void openVideo(View View) {
mVideoView.setVideoPath(path);
}
}
Reference:
VideoViewDemo
I hope it will be helpful to you.
Can't Play video in android uploded from iphone
Why mp4 does not play in 4 steps:
download you vidView URL to PC with 'ffmpeg'
$ffmpeg -i $downloadFileName
inspect details of reported codecs
compare those reported codecs to what android media supports
Note: very likely that #2 will report codecs not listed at #4
Although the video is .mp4, the video is compressed on iOS in a way that the native android media player cannot play/decode.
I ran into this exact problem OP had when developing for cross platform. An easy alternative is using VLC for Android, which is open source and built upon FFMPEG and utilizes SurfaceViews.
To do this, include the below in your build.grade file:
compile "de.mrmaffen:vlc-android-sdk:1.0.3"
For example, you can then make a new activity for the Video, and in the onCreate, do the following:
mSurfaceView = (SurfaceView) findViewById(R.id.player_surface);
mSurfaceHolder = mSurfaceView.getHolder();
mSurfaceFrame = (FrameLayout) findViewById(R.id.player_surface_frame);
mMediaUrl = getIntent().getExtras().getString("videoUrl");
try {
mLibVLC = LibVLC.getInstance();
mLibVLC.setAout(mLibVLC.AOUT_AUDIOTRACK);
mLibVLC.setVout(mLibVLC.VOUT_ANDROID_SURFACE);
mLibVLC.setHardwareAcceleration(LibVLC.HW_ACCELERATION_FULL);
mLibVLC.eventVideoPlayerActivityCreated(Boolean.TRUE);
mLibVLC.init(getApplicationContext());
} catch (LibVlcException e){
Log.e(TAG, e.toString());
}
mSurfaceHolder.addCallback(mSurfaceCallback);
mSurface = mSurfaceHolder.getSurface();
mLibVLC.attachSurface(mSurface, VideoVLCActivity.this);
mLibVLC.playMRL(mMediaUrl);
Lots of info can be found here for troubleshooting:
VLC for Android VideoLAN Forums
There is a more detailed example here, although it uses a different version of libvlc.LibVLC and contains different methods, but it will give you a good idea of what steps are needed to add controls, set the proper size, aspect ratio, orientation etc.
My code below to streaming video:
VideoView vv = (VideoView)this.findViewById(R.id.screen_video);
Uri uri = Uri.parse(URL);
vv.setVideoURI(uri);
vv.start();
It works.
But if the URL video's format not support by android phone or pad.
It show a dialog, and not show the screen.
But it still streaming with black screen.
I want to get the error message, and access as an exception.
But I don't know how to get it?
Another problem is that the streaming may crash cause by low speed wifi.
How to check it to wait while low speed wifi?
try this code,it work,
public class PlayVideo extends Activity
{
private String videoPath ="url";
private static ProgressDialog progressDialog;
String videourl;
VideoView videoView ;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.play_video);
videoView = (VideoView) findViewById(R.id.videoView);
progressDialog = ProgressDialog.show(PlayVideo.this, "", "Buffering video...", true);
progressDialog.setCancelable(true);
PlayVideo();
}
private void PlayVideo()
{
try
{
getWindow().setFormat(PixelFormat.TRANSLUCENT);
MediaController mediaController = new MediaController(PlayVideo.this);
mediaController.setAnchorView(videoView);
Uri video = Uri.parse(videoPath );
videoView.setMediaController(mediaController);
videoView.setVideoURI(video);
videoView.requestFocus();
videoView.setOnPreparedListener(new OnPreparedListener()
{
public void onPrepared(MediaPlayer mp)
{
progressDialog.dismiss();
videoView.start();
}
});
}
catch(Exception e)
{
progressDialog.dismiss();
System.out.println("Video Play Error :"+e.toString());
finish();
}
}
}
It depends on which Android Version you are developing your application on. There are certain devices which do not support running .mp4 file. Go through Android Media support for more information. Check if you can play any .3gp files or not.
You shouldn't play the video instantly. Add OnPrepared listener to the video view and start the video playing after it. With MediaPlayer you could keep track of the buffering state and stop the video for a while when its not yet downloaded. Please have a look at this guide.
Try Intent to avoid that error. or put try catch in your block.
VideoView can only Stream 3gp videos but i recommend this code to stream your video
public void onCreate(Bundle savedInstanceState){
setContentView(R.layout.main);
String videourl = "http://something.com/blah.mp4";
Uri uri = Uri.parse(videourl);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
intent.setDataAndType(uri, "video/mp4");
startActivity(intent);
}
Or Click here to watch Android Video Streaming Tutorial.
I am trying to play a video that is passed from a WebView in a VideoView. It works, except VideoView does not want to read it. I keep getting the error:
"Sorry, this video cannot be played."
Here is the code for the VideoView:
public class VideoHandler extends Activity {
WebView myWebView;
VideoView myVideoView;
WebChromeClient chromeClient;
WebViewClient wvClient;
Intent in;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.video_player);
myVideoView = (VideoView) findViewById(R.id.videoview);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(myVideoView);
String video = (MNWVMainPage.myWebView.getUrl());
myVideoView.setMediaController(mediaController);
myVideoView.setVideoPath(video);
myVideoView.start();
myVideoView.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.stop();
mp.release();
setContentView(R.layout.mnwv_main);
}
});
}
}
Why wont this load the video?
From just reading from your code, I can see no error.
Can you check a few things:
Does the video align to the format list here?
Have you open the camera/ another VideoView? Even if you release them, the buffer seems to need some time to be actually released.
I wrote code for play video from URL. But I am getting UNABLE TO PLAY VIDEO message.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
VideoView videoView = (VideoView) findViewById(R.id.video);
MediaController mc = new MediaController(this);
mc.setAnchorView(videoView);
mc.setMediaPlayer(videoView);
Uri video = Uri.parse("http://www.youtube.com/watch?v=qvtCk1wZ7LM&feature=player_detailpage");
videoView.setMediaController(mc);
videoView.setVideoURI(video);
videoView.start();
}
<VideoView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/video"></VideoView>
You could simple fire an Intent to play the video by another app.
Here, Youtube app will detect a Youtube Video, and will prompt the dialog to play the video on your behalf.
String url = "http://www.youtube.com/watch?v=qvtCk1wZ7LM";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
But if you really want to integrate a video player inside your app, I strongly recommand you to take a look at the Youtube API.
Its simple just create video view then add a new media controller to it, set the video URL in the video view and start the video it will work.
Add the below Code into your MainActivity.java file.
#Override
protected void onCreate(Bundle savedInstanceState)
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
try {
setContentView(R.layout.videodisplay);
String link="http://s1133.photobucket.com/albums/m590/Anniebabycupcakez/?action=view& current=1376992942447_242.mp4";
VideoView videoView = (VideoView) findViewById(R.id.VideoView);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
Uri video = Uri.parse(link);
videoView.setMediaController(mediaController);
videoView.setVideoURI(video);
videoView.start();
} catch (Exception e) {
// TODO: handle exception
Toast.makeText(this, "Error connecting", Toast.LENGTH_SHORT).show();
}
}
You better try it on offline file to make sure that the video viewer is working fine (the video is compatible with the device) then play it from YouTube online.
Here is a simple and easy way to play the video.
xml file...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="#+id/LinearLayout01"
android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingLeft="2px" android:paddingRight="2px"
android:paddingTop="2px" android:paddingBottom="2px"
android:layout_width="fill_parent" android:orientation="vertical">
<VideoView android:layout_height="fill_parent"
android:layout_width="fill_parent" android:id="#+id/VideoView"></VideoView>
</LinearLayout>
java file.....
public class VideoPlayerController extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.video);
VideoView videoView = (VideoView) findViewById(R.id.VideoView);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
// Set video link (mp4 format )
Uri video = Uri.parse("mp4 video link");
videoView.setMediaController(mediaController);
videoView.setVideoURI(video);
videoView.start();
}
}
some file format like youtube video file format cant open in video view.
videoview only support system file format can opend it like mp4 . 3gp . other...
for open fly video format you must use surfeview and create class that can handl this video type
and for get video from link use inputstream (dont use string)
other easy way to show youtube video is just use Intent.View,url(your video uri)
https://developer.android.com/guide/appendix/media-formats.html
Use Exo-Player , it is better choice to play videos and having lot of functionallity --Follow Exo Player Docs