How Can we Set Video Length for Some time Span like 60 seconds if its length is more than 5 minute

StackOverflow https://stackoverflow.com/questions/21695436

  •  09-10-2022
  •  | 
  •  

Question

**In Android Application Please Suggest how can we setDuration of video... My video length is currently more than 2 minutes. *1.videoview doesnot giving setDuration ... 2. how to setDuration in videoview***

    public class MainActivity extends Activity {
    VideoView video;
    MediaPlayer mp;
    Button play;
    int playtime=10000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    video=(VideoView)findViewById(R.id.videoView1);
        video.setLongClickable(true);

        play=(Button)findViewById(R.id.btn1);


        Uri uri=Uri.parse("android.resource://" + getPackageName() +"/"+R.drawable.b);
        video.setVideoURI(uri);
        video.setMediaController(new MediaController(this));
        //video.setDuration(playtime); 
        MediaController mp=new MediaController(this);





        play.setOnLongClickListener(new OnLongClickListener() {

             @Override
            public boolean onLongClick(View v) 
             {
                 if(video.getDuration()<=playtime)
                 {
                 video.setVisibility(v.VISIBLE);
                 video.requestFocus();
                 video.start();
                 play.setEnabled(false);
                }
                 else
                 {
                     video.stopPlayback();
                 }
                return true;
            }
        });
        video.setOnCompletionListener(new OnCompletionListener() {

            @Override
            public void onCompletion(MediaPlayer mp) {
                play.setEnabled(true);
                video.setVisibility(View.GONE);
                play.setFocusableInTouchMode(true);
                play.requestFocus();

            }
        }); 



    }

    2. Xml and here is the XML Snippet which is ok..

    <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"
   tools:context=".MainActivity" >

 <VideoView
     android:id="@+id/videoView1"
     android:layout_width="wrap_content"
     android:layout_height="300dp"
     android:visibility="gone"
     android:layout_centerHorizontal="true" />

   <Button 
       android:id="@+id/btn1"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignParentBottom="true"
       android:layout_centerHorizontal="true" 
       android:layout_marginTop="20dp"
       android:text="play"/> 


    </RelativeLayout>
Was it helpful?

Solution

You need to implement MediaPlayerControl to MediaController Like this.

mp.setMediaPlayer(new MediaPlayerControl() {

            @Override
            public void start() {

            }
            @Override
            public void seekTo(int pos) {

            }
            @Override
            public void pause() {

            }
            @Override
            public boolean isPlaying() {
                return false;
            }
            @Override
            public int getDuration() {
                return 0; // Pass your duration here
            }
            @Override
            public int getCurrentPosition() {
                return 0;
            }
            @Override
            public int getBufferPercentage() {
                return 0;
            }
            @Override
            public boolean canSeekForward() {
                return false;
            }
            @Override
            public boolean canSeekBackward() {
                return false;
            }
            @Override
            public boolean canPause() {
                return false;
            }
        });

Replace this lines

 video.setMediaController(new MediaController(this));
        //video.setDuration(playtime); 
        MediaController mp=new MediaController(this);

With

    MediaController mp=new MediaController(this);
 video.setMediaController(mp);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top