MediaController Play On Video Click - android

I have a simple app where a video loads from a url and when you click on the video, the MediaController pops up.
This works fine, but I'm wondering if there is a way I can just skip that step and have it so that clicking the video automatically triggers the 'play' function of the MediaController without having to ever see it.
These videos are short and I don't need to be able to control them other than just pressing play.
Here is my code:
private static final String MOVIE_URL ="http://mywebsite.com/videos/testvideo.mkv";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final VideoView vid = (VideoView) findViewById(R.id.videoView);
Uri video = Uri.parse(MOVIE_URL);
vid.setMediaController(new MediaController(this));
vid.setVideoURI(video);
vid.seekTo(1);
vid.setOnClickListener(
new View.OnClickListener()
{
#Override
public void onClick(View view)
{
vid.start();
vid.requestFocus();
}
}
);
}
EDIT
I needed to use onTouchListener instead of onClickListener because it's a videoview. This works perfectly!
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final VideoView vid = (VideoView) findViewById(R.id.videoView);
Uri video = Uri.parse(MOVIE_URL);
// vid.setMediaController(new MediaController(this));
vid.setVideoURI(video);
vid.seekTo(1);
vid.setOnTouchListener(
new View.OnTouchListener()
{
#Override
public boolean onTouch(View v, MotionEvent event) {
vid.start();
vid.requestFocus();
return false;
}
}
);
}

Add a PreparedListener to listen for video if prepared . When the VideoView is prepared to start the play
vid.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
vid.start();
vid.requestFocus();
}
});
vid.setVideoURI(video);
When setVideoURI, the instance of MediaPlayerwill be created and register for MediaPlayer setOnPreparedListener . So that when MediaPlayer is prepared the VideoView onPrepared listener will get called.

Related

Pause/resume video based on focus

I have a videoview that plays as a background. How do I pause it when my app looses focus and resume the video when the app has focus again? Here is my code.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launcher);
VideoView videoView = (VideoView) findViewById(R.id.launcherVideo);
Uri src = Uri.parse("android.resource://com.package/raw/video");
videoView.setVideoURI(src);
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.setVolume(0, 0);
mp.setLooping(true);
}
});
//videoView.setMediaController(new MediaController(this));
videoView.start();
}
override onPause() method and write code for pause on that method and override onResume() method and write code for resume on that method

Video not playing but audio plays fine on Live Streaming in android

I have a VideoView which is used to play live stream. I am not able to find where the problem occurs.
In the beginning, I used RTMP to play the live stream using Vitamio which resulted in playing the audio leaving the screen blank. I checked out with the RTMP link, it works fine with a website. I surfed a lot about this, but I did not find any solution.
So now, I switched to HTTP to play the live stream which also results in the same problem (i.e audio playing fine but video is blank).
I am expecting a solution for either RTMP or HTTP.
Any Suggestions???
UPDATE 1: I have found a problem with the link. I used VLC Media Player to check whether my RTMP and HTTP link is working fine or not. The RTMP link works fine whereas the problem was with the HTTP link. The HTTP link plays only the audio.
On the other hand, having the RTMP link working fine, it doesn't solve the problem when using Vitamio. Any Suggestions for RTMP??
Here is my code:
public class ITVLiveStreamActivity extends Activity {
private VideoView liveVideoView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_itvlive_stream);
liveVideoView = (VideoView) findViewById(R.id.liveVideoView);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(liveVideoView);
Uri uri = Uri.parse(getIntent().getStringExtra("rtmp://61.16.143.170:1935/live/7khh-8fhu-vxd3-8fuw"));
liveVideoView.setVideoURI(uri);
liveVideoView.setMediaController(mediaController);
liveVideoView.requestFocus();
liveVideoView.start();
}
}
UPDATE 2:
Here is my code using Vitamio:
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import io.vov.vitamio.LibsChecker;
import io.vov.vitamio.MediaPlayer;
import io.vov.vitamio.widget.MediaController;
import io.vov.vitamio.widget.VideoView;
public class ITVLiveStreamActivity extends Activity {
private String pathToFileOrUrl="rtmp://61.16.143.170:1935/live/7khh-8fhu-vxd3-8fuw";
private VideoView mVideoView;
private ProgressDialog progDailog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(!LibsChecker.checkVitamioLibs(this))
return;
setContentView(R.layout.activity_itvlive_stream);
mVideoView = (VideoView)findViewById(R.id.vitamio_videoView);
progDailog = new ProgressDialog(this);
progDailog.setCancelable(false);
progDailog.setMessage(getResources().getString(R.string.loading));
progDailog.show();
mVideoView.setVideoPath(pathToFileOrUrl);
mVideoView.setMediaController(new MediaController(this));
mVideoView.requestFocus();
mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
progDailog.dismiss();
mediaPlayer.setPlaybackSpeed(1.0f);
}
});
}
}
I am getting the same result (i.e) Audio is playing but video is blank.
Here I have added the screenshot
You can create an activity like this to open default Video View
public class VideoViewActivity extends Activity {
// Declare variables
ProgressDialog pDialog;
VideoView videoview;
// Insert your Video URL
// String VideoURL; /*= "http://www.androidbegin.com/tutorial/AndroidCommercial.3gp";*/
String VideoURL;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the layout from video_main.xml
setContentView(R.layout.video_view);
// Find your VideoView in your video_main.xml layout
videoview = (VideoView) findViewById(R.id.VideoView);
// Execute StreamVideo AsyncTask
VideoURL = getIntent().getExtras().getString(Constants.LINK);
// Log.v("video", VideoURL);
// Create a progressbar
pDialog = new ProgressDialog(VideoViewActivity.this);
// Set progressbar title
//pDialog.setTitle("Android Video Streaming Tutorial");
// Set progressbar message
pDialog.setMessage("Buffering...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
// Show progressbar
pDialog.show();
try {
// Start the MediaController
MediaController mediacontroller = new MediaController(
VideoViewActivity.this);
mediacontroller.setAnchorView(videoview);
// Get the URL from String VideoURL
Uri video = Uri.parse(VideoURL);
videoview.setMediaController(mediacontroller);
videoview.setVideoURI(video);
} catch (Exception e) {
// Log.e("Error", e.getMessage());
pDialog.dismiss();
e.printStackTrace();
} finally {
videoview.setOnErrorListener(new OnErrorListener() {
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
CommonUtilities.showToast(VideoViewActivity.this, "Video Format not supported by device.");
VideoViewActivity.this.finish();
return true;
}
});
}
videoview.requestFocus();
videoview.setOnPreparedListener(new OnPreparedListener() {
// Close the progress bar and play the video
public void onPrepared(MediaPlayer mp) {
pDialog.dismiss();
videoview.start();
}
});
}
}
hi i have done this and its working fine here is code
public class PlayingLiveStream extends Activity {
VideoView vvmyliveplaying;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_playinglivestream);
vvmyliveplaying = (VideoView) findViewById(R.id.vvmyliveplaying);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(vvmyliveplaying);
Uri uri = Uri.parse(getIntent().getStringExtra("url_play"));
vvmyliveplaying.setVideoURI(uri);
vvmyliveplaying.setMediaController(mediaController);
vvmyliveplaying.requestFocus();
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
try {
vvmyliveplaying.pause();
finish();
} catch (Exception e) {
}
}
return false;
}
}
<VideoView
android:id="#+id/vvmyliveplaying"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Edited Answer
I have tested streaming on vitamio library demo sample and its working fine.
public class VideoViewDemo extends Activity {
private VideoView mVideoView;
private String pathToFileOrUrl ="rtmp://61.16.143.170:1935/live/7khh-8fhu-vxd3-8fuw";
private ProgressDialog progDailog;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
if (!LibsChecker.checkVitamioLibs(this))
return;
setContentView(R.layout.videoview);
mVideoView = (VideoView) findViewById(R.id.surface_view);
progDailog = new ProgressDialog(this);
progDailog.setCancelable(false);
progDailog.setMessage("Please wait");
progDailog.show();
mVideoView.setVideoPath(pathToFileOrUrl);
mVideoView.setMediaController(new MediaController(this));
mVideoView.requestFocus();
mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
progDailog.dismiss();
mediaPlayer.setPlaybackSpeed(1.0f);
}
});
}
}
and xml layout,
<io.vov.vitamio.widget.VideoView
android:id="#+id/surface_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Now i'm sure code work well.
Thanks hope this will help you .

VideoView - MediaController not showing up

I can't get it working... Whenever I touch my video, nothing is happing... Although, I think, the MediaController should be visible...
I currently use following function to play my video:
private void playCachedVideo(final VideoView videoView, final Context c, File f)
{
//videoView.setZOrderOnTop(true);
videoView.setOnPreparedListener(new OnPreparedListener()
{
#Override
public void onPrepared(MediaPlayer mp)
{
mp.setOnVideoSizeChangedListener(new OnVideoSizeChangedListener()
{
#Override
public void onVideoSizeChanged(MediaPlayer mp, int width, int height)
{
MediaController mediaController = new MediaController(c);
mediaController.setMediaPlayer(videoView);
mediaController.setAnchorView(videoView);
videoView.setMediaController(mediaController);
}
});
}
});
videoView.setVideoURI(Uri.fromFile(f));
videoView.start();
}
The video plays fine, but I can't get any MediaController to show up...
PS: I'm showing the player in a DialogFragment
Try this:
mMediaController = new MediaController(getActivity());
replace c by getActivity().
You need to attach the controller to the VideoView and then call mediaController.show(timeInMillisec) in onPrepared() callback.
This show the controller on touch of videoview and will hide the controller after specified timeInMillisec.
public class VideoViewActivity extends Activity implements MediaPlayer.OnPreparedListener{
private VideoView mVideoView = null;
MediaController mediaController = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.videoview_layout);
mVideoView = (VideoView) findViewById(R.id.VideoView);
mVideoView.setOnPreparedListener(this);
mediaController = new MediaController(mVideoView.getContext());
mediaController.setMediaPlayer(mVideoView);
mediaController.setAnchorView(mVideoView);
mVideoView.setMediaController(mediaController);
mVideoView.setVideoPath("URL");
}
#Override
public void onPrepared(MediaPlayer mp) {
mVideoView.start();
mediaController.show(2000);
}
}

Android: VideoView can't play video

im trying to get a basic video player working for an android application im working on but whenever i try to play the video i get an error saying "cant play video". Im certain the video is in supported formats and ive tried converting it between several of the supported formats on top of that so i assume its an issue with my code:
public class VideoActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.video_layout);
VideoView videoView = (VideoView)findViewById(R.id.VideoView1);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
videoView.setMediaController(mediaController);
videoView.setVideoPath("TravelAppTopic4/res/raw/canadavid.mp4");
videoView.start();
}
thanks in advance for any help
edit:im testing on both a virtual device and an S3 to the same result.
public class VideoScreen extends Activity{
private MediaController controller;
Context _ctx = VideoScreen.this;
private Thread thread;
ProgressBar progressBar = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.phone_video_screen);
progressBar = (ProgressBar) findViewById(R.id.progressbar);
VideoView video = (VideoView) findViewById(R.id.videoView1);
controller = new MediaController(VideoScreen.this);
video.setMediaController(controller);
video.setVideoPath(_url);
video.requestFocus();
video.start();
progressBar.setVisibility(View.VISIBLE);
video.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.start();
progressBar.setVisibility(View.GONE);
mp.setOnVideoSizeChangedListener(new OnVideoSizeChangedListener() {
#Override
public void onVideoSizeChanged(MediaPlayer mp, int arg1,
int arg2) {
progressBar.setVisibility(View.GONE);
mp.start();
}
});
}
});
}
}
try
videoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw. canadavid));

video doesnot play but audio listening

public class video extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
VideoView videoView = (VideoView) findViewById(R.id.surface);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
// Set video link (mp4 format )
Uri video = Uri.parse("android.resource://" + getPackageName() +"/"+R.raw.wildlife);
Intent tostart = new Intent(Intent.ACTION_VIEW);
startActivity(tostart);
videoView.setMediaController(mediaController);
videoView.setVideoURI(video);
videoView.requestFocus();
videoView.start();
}
}
here is my code to play the video. But the video stays in my screen and it does not play. Is there any solution ?
video_player_view.xml
<?xml version="1.0" encoding="utf-8"?>
<VideoView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/VideoPlayerView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
Java code:
public class VideoPlayerActivity extends Activity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.video_player_view);
String video_file_path = ?;//give the path of your video
VideoView video_view = (VideoView) this.findViewById(R.id.VideoPlayerView);
MediaController mc = new MediaController(this);
video_view.setMediaController(mc);
video_view.setVideoPath(video_file_path);
video_view.requestFocus();
// start video
video_view.start();
video_view.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
endActivity();
}
});
}
public void endActivity() {
this.finish();
}
}
Why are you using this code?
Intent tostart = new Intent(Intent.ACTION_VIEW);
startActivity(tostart);
If it is not necessary then remove it video will be played with sound. Otherwise please write what you exactly want to do with video playback.

Categories

Resources