How to make the VideoView volume as mute? - android

I am working with the videoView, to play the .m3u8 videos steaming by using the custom controllers.
How can I make the VideoView audio as mute,Now I am going in one way, you can see my java code below. I tried that way but the Log files con't going inside on setOnInfoListener method. Is there any error what i am following.
Code I am using for getting Audio tracks:
Java
videoView.setOnInfoListener(new MediaPlayer.OnInfoListener() {
#Override
public boolean onInfo(MediaPlayer mp, int what, int extra) {
MediaPlayer.TrackInfo[] trackInfoArray = mp.getTrackInfo();
Log.e("track Length : ",String.valueOf(trackInfoArray.length));
for (int i = 0; i < trackInfoArray.length; i++) {
Log.e("track "+i+" : ",trackInfoArray[i].getLanguage().toString());
if (trackInfoArray[i].getTrackType() == MediaPlayer.TrackInfo.MEDIA_TRACK_TYPE_AUDIO
&& trackInfoArray[i].getLanguage().equals(Locale.getDefault().getISO3Language())) {
mp.selectTrack(i);
break;
}
}
return true;
}
});
Code I am using for mute the VideoView:
Java
videoView.setOnInfoListener(new MediaPlayer.OnInfoListener() {
#Override
public boolean onInfo(MediaPlayer m, int i, int i1) {
try {
Log.e("track : ","inside media player");
if (m.isPlaying()) {
m.stop();
m.release();
m = new MediaPlayer();
}
m.setVolume(0f, 0f);
m.setLooping(false);
m.start();
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
});
And remain everything video is running fine.Please give any other ways to make the videoView audio muting.
1.Is there any way to play the video with different audio tracks , I have list of .mp3 links.

Ref: Mute a playing video by VideoView in Android Application
Ans: https://stackoverflow.com/a/12549943/9909365
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.

Try this:
WebView wView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wView=(WebView)findViewById(R.id.webView1);
wView.setWebViewClient(new WebViewClient(){
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return super.shouldOverrideUrlLoading(view, url);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
});
wView.getSettings().setJavaScriptEnabled(true);
wView.getSettings().setBuiltInZoomControls(true);
wView.addJavascriptInterface(this, "myinterface");
}
public void displayMsg(String name,String pass){
Toast.makeText(getApplicationContext(), name+"\n"+pass, 2000).show();
}
public void test(View v){
switch (v.getId()) {
case R.id.imageView1:
wView.loadUrl("http://www.facebook.com");
break;
case R.id.imageView2:
wView.loadUrl("http://www.linkedin.com");
break;
case R.id.imageView3:
wView.loadUrl("http://www.twitter.com");
break;
case R.id.imageView4:
wView.loadUrl("http://www.youtube.com");
break;
case R.id.imageView5:
wView.loadUrl("file:///android_asset/login.html");
break;
}
}
}

Related

Pixelation in VideoView in Android ONLY with Wifi connection (3G/4G runs perfectly)

I have a problem that, after googling, I don't know why it is happenning.
I 've made and streaming video app with protocol RTSP. All is working good with a 3G/4G connection, but if i change to Wifi, much times video appears with a pixelation like a grey screen.
Testing in Android 5.1 (LG G3).
minSDKVersion = 15.
compiledSDKVersion = 22.
If i change to 3g, all goes well. It seems a problem of the initial buffer but its weird, because the problem is with Wifi connection (tested with two different connections, one xVDSL and one FTTH).
I'm not using any library, doing all the work directly with the android sdk and VideoView and mediaPlayer.
Here the code that init the configuration and how it handle the listeners:
videoView= (VideoView)this.findViewById(R.id.videoView);
videoView.setMediaController(null);
videoView.setOnErrorListener(this);
videoView.setOnCompletionListener(this);
videoView.setOnPreparedListener(this);
videoView.setVideoURI(Uri.parse(URL));
start/stop methods (the button above) and override listener methods:
public void startVideo(){
if(videoView !=null) {
initProgressDialog(getResources().getString(R.string.load_streaming));
startProgressDialog();
videoView.setVisibility(View.VISIBLE);
videoView.requestFocus();
//btnPlay.setVisibility(View.INVISIBLE);
}
}
public void stopVideo(){
if(videoView !=null) {
btnPlay.setText(getResources().getString(R.string.video_button_play));
stateVideo=2;
videoView.suspend();
}
}
public void resumeVideo(){
if(videoView !=null) {
initProgressDialog(getResources().getString(R.string.load_streaming));
startProgressDialog();
btnPlay.setText(getResources().getString(R.string.video_button_stop));
stateVideo=1;
videoView.setVisibility(View.VISIBLE);
videoView.resume();
}
}
private boolean manageErrorVideo(MediaPlayer mediaPlayer, int i, int i1){
return false;
}
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
videoView.setVisibility(View.VISIBLE);
stopProgressDialog();
}
#Override
public boolean onError(MediaPlayer mediaPlayer, int i, int i1) {
//A lot of control error code. If need, ask but never go this way when the problem appear.
return true;
}
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
btnPlay.setText(getResources().getString(R.string.video_button_stop));
videoView.start();
stopProgressDialog();
stateVideo = 1;
findViewById(R.id.mainView).setBackgroundColor(Color.BLACK);
Log.d("test", mediaPlayer.getVideoHeight() + "");
Log.d("test", mediaPlayer.getVideoWidth()+"");
}
#Override
public boolean onInfo(MediaPlayer mediaPlayer, int i, int i1) {
return false;
}
#Override
public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
}
Any information or help will be wellcome!
Thank you!!
EDIT: RTSP server is an IP camera.

Android setNextMediaPlayer() API for MediaPlayer is not playing next video

I have a simple activity which contains one instance of a VideoView and a reference to its' MediaPlayer. My goal was to use the setNextMediaPlayer() api from the MediaPlayer object in order to minimize the switching time between 2 videos.
In the below code, the 1st video plays well. When the 1st video completes, the 2nd video's audio begins to play in the background, but only the last frame of the 1st video is shown.
Do you know what the problem is? Why isn't the 2nd video's video displaying?
private VideoView player1;
private MediaPlayer mediaPlayer1;
public static final String URL_1 = "https://example.com/video1.mp4";
public static final String URL_2 = "https://example.com/video2.mp4";
public static final String TAG = "PrebufferingActivity";
public boolean FIRST_TIME = true;
public int count = 0;
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_prebuffering);
player1 = (VideoView) findViewById(R.id.videoPlayer1);
player1.setOnPreparedListener(new OnPreparedListener(){
#Override
public void onPrepared(MediaPlayer mp) {
mediaPlayer1 = mp;
if(FIRST_TIME == true)
{
mediaPlayer1.start();
player1.requestFocus();
FIRST_TIME = false;
}
}
});
player1.setOnInfoListener(new OnInfoListener(){
#Override
public boolean onInfo(MediaPlayer mp, int what, int extra)
{
if(what == MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START)
{
count++;
if(count % 2 != 0)
{
Log.d(TAG, "Odd count (" + count + ") Prebuffering URL_2");
MediaPlayer myMediaPlayer = MediaPlayer.create(PrebufferingActivity.this, Uri.parse(URL_2));
mp.setNextMediaPlayer(myMediaPlayer);
}
else
{
Log.d(TAG, "Even count (" + count + ") Prebuffering URL_1");
MediaPlayer myMediaPlayer = MediaPlayer.create(PrebufferingActivity.this, Uri.parse(URL_1));
mp.setNextMediaPlayer(myMediaPlayer);
}
}
return false;
}
});
player1.setOnCompletionListener(new OnCompletionListener(){
#Override
public void onCompletion(MediaPlayer mp) {
}
});
// Player 1
player1.setMediaController(new MediaController(this));
player1.setVideoURI(Uri.parse(URL_1));
}
In your example you create new MediaPlayer, but VideoView knows nothing about and can't control it. It wouldn't work properly.
If you need some specific functions probably you need to build your analog of VideoView, that would use MediaPlayer.
Regarding playing 2nd video by using setNextMediaPlayer() you can find some hints here:
https://stackoverflow.com/a/28465846/755313

How to free memory used by VideoView

I have been having this issue for a while now.
I have a simple application that plays a playlist of videos within a videoview with a pause of a few seconds between them.
private final Runnable playNormalVideo = new Runnable() {
public void run() {
try {
final String filepath = ...;
viewVideo.setOnErrorListener(new OnErrorListener() {
public boolean onError(MediaPlayer mp, int what, int extra) {
onEndVideo(false);
return true;
}
});
viewVideo.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
onEndVideo(false);
}
});
viewVideo.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
background.setVisibility(View.INVISIBLE);
viewVideo.start();
}
});
viewVideo.setVideoPath(filepath);
viewVideo.setVisibility(View.VISIBLE);
viewVideo.requestFocus();
} catch (Exception e) {
String error = Utils.getStackTrace(e);
Utils.log(true, error);
}
}
};
and in my onEndVideo() function I make the background visible and check what video to play next and with a Handler i request to play it after x seconds.
My issue is the following :
Within a long run (approx 1 day) the background becomes invisible within a lot of seconds before the video starts. I don't understand why. If someone could help me get rid of this issue.
I also thought I should free memory between video plays but i don't seem to find how to do that.
Note : All the videos are saved on the device.
Thanks for any help.

videoview oncompletionlistener not called in android?

i am displaying images and video in imageview and videoview but the issue is when video is
playing onpreparedlistener called but when video finish oncompletion listener not called
when videoview complete i increment the i for next video or images
also it gives me error in logcat like this but video is playing
10-29 20:12:47.770: E/MediaPlayer(3975): error (1, -2147483648)
private void nextVideo(String path){
mImageview.setVisibility(View.GONE);
if(mVideoview.getVisibility()==View.GONE){
mVideoview.setVisibility(View.VISIBLE);
}
controller = new MediaController(HomeActivityNewViewPager.this);
mVideoview.setVideoURI(Uri.parse(path));
mVideoview.setMediaController(null);
controller.setMediaPlayer(mVideoview);
mVideoview.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
mVideoview.start();
long duration = mVideoview.getDuration();
second=duration;
//handler.removeCallbacks(runnable);
//handler.postDelayed(runnable,second);
}
});
mVideoview.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
Log.v("video view completed","---"+i);
mp.reset();
if(automode){
if(i==myplaylistlocal.size() || i>myplaylistlocal.size())
{
String checkcount=spreferences.getString("roundcount", "");
Log.v("roundcount==Before Integer.parseInt","---->"+roundcount);
if(roundcount>=Integer.parseInt(checkcount))
{
roundcount=0;
Log.v("roundcount==After Integer.parseInt","---->"+roundcount);
updateplaylist();
}
i=0;
indexplus();
imagesautomode();
i++;
}
else if(i==myplaylistlocal.size()-1)
{
imagesautomode();
i++;
}
else{
imagesautomode();
}
}
else{
i++;
images();
}
}
});
mVideoview.setOnErrorListener(new OnErrorListener() {
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
Log.v("Error in video playing","----->"+i);
return true;
}
});
}
Either way, the error referenced above is MEDIA_ERROR_UNKNOWN. If this video was made for this app, I would make sure that it is properly encoded for Android. Also make sure that is clearly defines its endpoint.
http://developer.android.com/reference/android/media/MediaPlayer.html#MEDIA_ERROR_UNKNOWN
this is a work around but could possbly work in your situation:
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
if(what == MediaPlayer.MEDIA_ERROR_UNKNOWN)
//ERROR UNKNOWN - COULD BE IMPROPERLY FORMATTED VIDEO {
//MOVE ON TO NEXT VIDEO
//DO LOGGING
}
}

Android: Video cannot be played from non hard-coded URL

I'm trying to stream a video and play it using VideoView. I supply the view with the source URL of the video with setVideoURI() as shown below. With a hardcoded value like urlString = "www.myvideoserver.com/videos/bigbuckbunny.mp4", the video plays fine. However, when urlString is given the value from the intent (coming from the previous activity where the user chose the video), I get the message: "Sorry, Video cannot be played". I've read that one of the common causes is video format, like it's described here and here. I'm almost certain that it is not a format issue because I can play the video when the URL is fixed (and I know this because I can see from Log.d("PVurl", urlString); that the value of urlString is exactly the same as the one I fixed it to. That is, in LogCat, I copy paste the value into the line urlString = getIntent()... // "www.myvideoserver.com/videos/bigbuckbunny.mp4", and it works but not when urlString is set to the intent return value . LogCat Errror panel gives the following:
04-13 17:35:32.786: ERROR/MediaPlayer(2620): error (1, -1007)
04-13 17:35:32.786: ERROR/MediaPlayer(2620): Error (1,-1007)
I've searched around the internet but it doesn't seem that anyone has encountered such an error code before.
I'd very much appreciate if anyone has any idea what could be the problem. Thanks!
public void playvideo () { // obtain URL of the requested video from the intent in previous activity
try
{
urlString = getIntent().getStringExtra("mypackage.fulladdr");
if (urlStr != null)
{
Log.d("PVurl", urlString);
VideoView v = (VideoView) findViewById(R.id.videoView1);
// play video
v.setVideoURI(Uri.parse(urlString));
v.setMediaController(new MediaController(this));
v.start();
v.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
showRatingDialog();
}
});
}
}
catch (Exception e)
{
Log.d("PV_TAG", +e.getMessage());
e.printStackTrace();
}
}
You haven't added the scheme which the Uri needs to find out what it is referring to (http, in your case) .
urlString = "www.myvideoserver.com/videos/bigbuckbunny.mp4" change it to urlString = "http://www.myvideoserver.com/videos/bigbuckbunny.mp4"
(1, -1007) error means:
MEDIA_ERROR_UNKNOWN - "File or network related operation errors."
this may come from a corrupt file
see also javadoc of android.media.MediaPlayer.OnErrorListener#onError
http://developer.android.com/reference/android/media/MediaPlayer.OnErrorListener.html#onError
#param what the type of error that has occurred:
MEDIA_ERROR_UNKNOWN
MEDIA_ERROR_SERVER_DIED
#param extra an extra code, specific to the error. Typically implementation dependent.
MEDIA_ERROR_IO
MEDIA_ERROR_MALFORMED
MEDIA_ERROR_UNSUPPORTED
MEDIA_ERROR_TIMED_OUT
I tried the same thing and was able to get it to work. Perhaps something else is going on in your code. Here is what I have:
// Intent to start the activity that launches the video player
Intent i = new Intent(this, ExampleActivity.class);
i.putExtra("url", "http://www.yourvideo.com/yourvid.m4v");
startActivity(i);
And here is the code that deals with the player:
private MediaController mController;
private Uri videoUri;
private int percentBuffered= 0;
private VideoView videoView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
mController = new MediaController(this);
videoView = (VideoView) findViewById(R.id.videoView1);
// access String from the intent that launched this Activity
videoUri = Uri.parse(getIntent().getStringExtra("url"));
}
#Override
public void onResume() {
super.onResume();
videoView.setOnCompletionListener(this);
try {
videoView.setVideoURI(videoUri);
mController.setMediaPlayer(videoView);
videoView.setMediaController(mController);
videoView.requestFocus();
videoView.start();
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
protected void onPause() {
super.onPause();
finish();
}
#Override
public boolean onTouchEvent(MotionEvent event) {
mController.show();
return super.onTouchEvent(event);
}
#Override
public int getBufferPercentage() {
return percentBuffered;
}
#Override
public int getCurrentPosition() {
return videoView.getCurrentPosition();
}
#Override
public int getDuration() {
return videoView.getDuration();
}
#Override
public boolean isPlaying() {
return videoView.isPlaying();
}
#Override
public void pause() {
videoView.pause();
}
#Override
public void seekTo(int pos) {
videoView.seekTo(pos);
}
#Override
public void start() {
videoView.start();
}
#Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
percentBuffered = percent;
}
#Override
public void onCompletion(MediaPlayer mp) {
}
public boolean canPause() {
return true;
}
public boolean canSeekBackward() {
return true;
}
public boolean canSeekForward() {
return true;
}
Alright, I found the problem after many futile hours of digging through to find what MediaPlayer error(1, -1007) means.
In reality, it has to do with the rather unfortunate fact that the line
getIntent().getStringExtra("mypackage.fulladdr");
returns a string with an extra white space at the end. I think it's an implementation issue with getIntent().
Anyone knows where to report these kind of bugs, that'd be great.
Thanks to #Akhil and #javaMe for their attempts!

Categories

Resources