I want to mute a playing Video by VideoView in my Android Application.
I couldn't find any method to do so in VideoView Class.
Any idea how to do this?
I have found a method "setVolume" in MediaPlayer Class, But I am unable to find any working code to play video by MediaPlayer class.
So I believe I can set volume 0 by this method.
Therefore I am looking for any working code to play video using MediaPlayer Class or how to control volume using VideoView Class.
Below is the code for playing video using VideoView , which I am using.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video);
VideoView videoView = (VideoView)this.findViewById(R.id.VVSimpleVideo);
MediaController mc = new MediaController(this);
mc.setAnchorView(videoView);
mc.setMediaPlayer(videoView);
videoView.setMediaController(mc);
String _path = "/mnt/sdcard/Movies/video5.mp4";
videoView.setVideoPath(_path);
videoView.requestFocus();
videoView.start();
}
If you want to get access to the MediaPlayer of a VideoView you have to call MediaPlayer.OnPreparedListener and MediaPlayer.OnCompletionListener, then you can call MediaPlayer.setVolume(0f, 0f); function to set the volume to 0.
Do this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video);
VideoView videoView = (VideoView)this.findViewById(R.id.VVSimpleVideo);
MediaController mc = new MediaController(this);
mc.setAnchorView(videoView);
mc.setMediaPlayer(videoView);
videoView.setMediaController(mc);
String _path = "/mnt/sdcard/Movies/video5.mp4";
videoView.setVideoPath(_path);
videoView.setOnPreparedListener(PreparedListener);
videoView.requestFocus();
//Dont start your video here
//videoView.start();
}
MediaPlayer.OnPreparedListener PreparedListener = new MediaPlayer.OnPreparedListener(){
#Override
public void onPrepared(MediaPlayer m) {
try {
if (m.isPlaying()) {
m.stop();
m.release();
m = new MediaPlayer();
}
m.setVolume(0f, 0f);
m.setLooping(false);
m.start();
} catch (Exception e) {
e.printStackTrace();
}
}
};
videoview.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.setVolume(0, 0);
}
});
I have done this using MediaPlayer class.
I have used setVolume function of MediaPlayer class to set the volume to 0.
also I have realised that dont use AudioManager class, because using AudioManager if a set volume to 0, then it set volume to 0 for all the instance of MediaPlayer and VideoView. But if you will use setVolume() method of MediaPlayer then it will just Mute the volume of that instance only.
Also set volume to 0 is bot easy using VideoView because VideoView is a wrapper over MediaPlayer class and just allow to access few function of MediaPlayer.
Also I have read on some blog that though you can reference MediaPlayer instance using VideoView instances, but its very complex and its not recommended to do so.
Hope this would be helpful to other new readers how try to do similar things.
Related
Am recording a video in Android device using JavaCV and playing it using a videoview. Now want to show a text on video while playing, which is entered after recording video. This text must be seen in all video players while playing the video.
I have gone thro the link How to add text on video recording? too which is expected here too
This is the method called after the video is recorded.
private void playRecordedVideo(Uri videoUri, boolean playVideoInLoop)
{
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
videoView.setLayoutParams(layoutParams);
videoView.setVisibility(View.VISIBLE);
videoView.setVideoURI(videoUri);
if(playVideoInLoop)
{
MediaController mediaController = new MediaController(MainActivity.this);
mediaController.setAnchorView(videoView);
videoView.setMediaController(mediaController);
videoView.setOnPreparedListener (new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.setLooping(true);
}
});
}
else
{
videoView.setMediaController(null);
}
videoView.start();
btnStart.setText(getString(R.string.txt_finish));
}
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.
Hi I'm new to android development and working on a game. I currently have an opening cutsceen which is a videoview, but I want to mute the the sound, if the user selects mute music from the preference menu. The problem is I don't know how to mute the music in a videoview without actually turning off the video entirely!
The AudioManager is not a good option always, because you mute all the system...
if you want to get access to the MediaPlayer of a VideoView you have to call MediaPlayer.OnPreparedListener and MediaPlayer.OnCompletionListener, then you can call setVolume(0f, 0f); function to set the volume to 0.
Do this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video);
VideoView videoView = (VideoView)this.findViewById(R.id.VVSimpleVideo);
MediaController mc = new MediaController(this);
mc.setAnchorView(videoView);
mc.setMediaPlayer(videoView);
videoView.setMediaController(mc);
String _path = "/mnt/sdcard/Movies/video5.mp4";
videoView.setVideoPath(_path);
videoView.setOnPreparedListener(PreparedListener);
videoView.requestFocus();
//Dont start your video here
//videoView.start();
}
MediaPlayer.OnPreparedListener PreparedListener = new MediaPlayer.OnPreparedListener(){
#Override
public void onPrepared(MediaPlayer m) {
try {
if (m.isPlaying()) {
m.stop();
m.release();
m = new MediaPlayer();
}
m.setVolume(0f, 0f);
m.setLooping(false);
m.start();
} catch (Exception e) {
e.printStackTrace();
}
}
};
The best you can do is use AudioManager to mute the music stream at the beginning of the cut scene and unmute it once it is done: VideoView does not provide independent mute controls.
Using:
private void play() {
VideoView v = (VideoView) findViewById(R.id.videoView);
MediaPlayer mp = MediaPlayer.create(this, R.raw.video);
mp.setDisplay(v.getHolder());
mp.start();
}
My video plays just about 5 first seconds, and stay like paused.. why does it happen? Is it something related to buffer? (I'm playing a local resource)
--
I've tried another 3gp video, and the same problem happens.
Combining a VideoView and a MediaPlayer may not be the right approach. VideoView uses its own MediaPlayer. Either use VideoView by itself, or use a combination of MediaPlayer and SurfaceView.
Working code (maybe was some problem with resource overuse):
private void play() throws Exception {
v = (VideoView) findViewById(R.id.videoView);
if (!firstPlay) {
mp.release();
}
mp = MediaPlayer.create(this, R.raw.video);
mp.setDisplay(v.getHolder());
mp.start();
firstPlay = false;
}