0
package z.x;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.VideoView;

public class AsdqweActivity extends Activity {
    /** Called when the activity is first created. */
    VideoView myVideoView;
    TextView translation;
    String SrcPath = "/sdcard/bunny.MP4",txtdisplay="";
    Thread Thread1,Thread2;
    Button tag;
    int count=0,tstart=-1,tend=0;
    AudioManager am;
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        myVideoView = (VideoView) findViewById(R.id.newVideoView);
        translation= (TextView) findViewById(R.id.translation);
        MediaController mediaController = new MediaController(this);
        mediaController.setAnchorView(myVideoView);
        myVideoView.setMediaController(mediaController);
        myVideoView.setKeepScreenOn(true);
        Runnable runnable = new VideoPlayer();
        Thread2= new Thread(runnable);   
        Thread2.start();

        runnable = new CountDownRunner();
        Thread1= new Thread(runnable);   
        Thread1.start();

        runnable = new AudioPlayer();
        Thread1= new Thread(runnable);   
        Thread1.start();

        am = 

        tag = (Button) findViewById(R.id.start);
        tag.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                //how to change the text on the button????????
                if(count%2==0)
                {
                    tstart=myVideoView.getCurrentPosition();
                    System.out.println("tag starts here: "+ tstart);
                    myVideoView.pause();
                    tend=tstart+1000;
                    count++;
                    if((tstart+1000)>myVideoView.getDuration())
                    {
                        tend=myVideoView.getDuration();
                        count++;
                    }
                }
                else
                {
                    tend = myVideoView.getCurrentPosition();
                    if(tend>=(tstart+1000)){
                        System.out.println("tag ends here: "+ tend);
                        count++;
                        myVideoView.pause();    
                    }
                    else
                    {
                        System.out.println("invalid tagging");
                    }
                    tstart=-1;
                }
            }
        });
        myVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() 
        {
            public void onCompletion(MediaPlayer mp) 
            {
            Log.v("log_tag", "On Completion");
            try {
                Thread1.join();
                Thread2.join();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            //r.stopThread();
            finish();
            }
        });
    }

    class VideoPlayer implements Runnable{

        public void run()
        {
            myVideoView.requestFocus();
            myVideoView.setVideoPath(SrcPath);
            myVideoView.start();
        }

    }

    class AudioPlayer implements Runnable{

        public AudioPlayer() {
            // TODO Auto-generated constructor stub

        }
        public void run()
        {
            /*myVideoView.setsetvolume(0,0);
            audio.requestFocus();
            audio.setVideoPath(SrcPathaudio);
            audio.start();*/
        }

    }

    class CountDownRunner implements Runnable
    {   
        File file = new File("/sdcard/harsh.srt");
        long r=0;
        RandomAccessFile rand;
        public CountDownRunner() {
            // TODO Auto-generated constructor stub
            if(!file.exists())
            {
              System.out.println("File does not exist.");
              System.exit(0);
            }
            try {
                rand = new RandomAccessFile(file,"r");
            } catch (FileNotFoundException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            translation = (TextView)findViewById(R.id.translation);

        }
        public void run() 
        {   
            // Get the object of DataInputStream
            while(!Thread.currentThread().isInterrupted())
            {
                try 
                {
                    doWork();
                    Thread.sleep(100);
                }catch (InterruptedException e) 
                {
                    Thread.currentThread().interrupt();
                    e.printStackTrace();
                }catch(Exception e)
                {
                    e.printStackTrace();
                }
            }
            closeRandFile();

        }
        public void closeRandFile(){
            try {
                rand.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        public void doWork(){
            runOnUiThread(new Runnable() {
                public void run() {
                    try{
                        rand.seek(r);
                        //If it returns  milliseconds, divide by 1000
                        int playTime = myVideoView.getCurrentPosition();
                        long t1=0,t2=0;
                        int i=0,j=0;
                       /* String textValue = "i havent entered it yet";
                        System.out.println(playTime);
                        if(playTime<15000){
                        textValue = "its still not 15000";
                        System.out.println(textValue);
                        }else if(playTime>3000)
                        {textValue = "its more than 15000";}*/
                        ////////////////////////////////////
                        try{
                          // Open the file that is the first 
                          // command line parameter

                          String strLine;
                          //Read File Line By Line
                          while(((strLine = rand.readLine()) != null)&&((strLine=="")))
                          {
                              System.out.println(strLine+"  line 184");
                          }
                          if(strLine != null)
                          {
                              i = Integer.parseInt(strLine);
                              System.out.println(strLine+"  line 189");
                              if(i>j){
                                  if((strLine = rand.readLine()) != null)
                                  {
                                      //02:03:24,100 --> 02:03:25,500
                                      //String substring(int startIndex, int endIndex)
                                      int h =Integer.parseInt(strLine.substring(0,2));
                                      int m =Integer.parseInt(strLine.substring(3,5));
                                      int s =Integer.parseInt(strLine.substring(6,8));
                                      int ms=Integer.parseInt(strLine.substring(9,12));
                                      t1 = (((((h*60)+m)*60)+s)*1000)+ms ;
                                      System.out.println("start of this text time "+t1);
                                      int l = strLine.indexOf("-->")+4;
                                      h =Integer.parseInt(strLine.substring(l+0,l+2));
                                      m =Integer.parseInt(strLine.substring(l+3,l+5));
                                      s =Integer.parseInt(strLine.substring(l+6,l+8));
                                      ms=Integer.parseInt(strLine.substring(l+9,l+12));
                                      t2 = (((((h*60)+m)*60)+s)*1000)+ms ;
                                      System.out.println("end of this text time "+t2);
                                  }
                                  if((playTime<=t2)&&(playTime>=t1)){
                                        while (((strLine = rand.readLine()) != null)&&(strLine!="")){
                                              // Print the content on the console
                                                  txtdisplay=txtdisplay+strLine;    
                                          }
                                          System.out.println(txtdisplay);
                                          r=rand.getFilePointer(); 
                                          translation.setText(txtdisplay);
                                  }
                                  txtdisplay="";
                          }
                          }
                          //Close the input stream

                            }catch (Exception e){//Catch exception if any
                          System.err.println("Error: " + e.getMessage());
                          }
                        //////////////////////////////////////  
                    }catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });

        }
    }
}

I am new here. I was asked by my professor to play other audio while the same video is being played(for example a person doesn't understand English and wants the the app to play french ). I have used video view. One possible method was to mute the present audio and play the french audio stored in my sdcard. but video view supports no muting( i couldn't find one). I read that i have to go for media player instead of video view. Please help...!! what should i do... Thanks in advance.

nsp
  • 378
  • 1
  • 4
  • 19
  • A VideoView is just a wrapper around the MediaPlayer object / SurfaceView, but after a little searching I can see it doesn't support the setVolume control that the MediaPlayer object has... Which is strange, because you'd think something like that would be implemented. Maybe you can write some kind of CustomVideoView and just implement the setVolume method in there – Cruceo Jun 13 '12 at 19:04
  • what do you say about audioManager?? – nsp Jun 13 '12 at 19:09
  • Never used it myself, but it appears someone else has done something similar: http://stackoverflow.com/a/3197282/1426565 – Cruceo Jun 13 '12 at 19:12

1 Answers1

3

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();
            }    
     }
 };
Leonardo Arango Baena
  • 870
  • 1
  • 10
  • 15