How Android uses Videoview to play local video and network video _android

Source: Internet
Author: User

1, the effect shows

2. layout file

<?xml version= "1.0" encoding= "Utf-8"?> <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"
  android:paddingbottom= "@dimen/activity_vertical_margin"
  android: paddingleft= "@dimen/activity_horizontal_margin"
  android:paddingright= "@dimen/activity_horizontal_margin"
  android:paddingtop= "@dimen/activity_vertical_margin"
  tools:context= " App.com.myapplication.LocalVideoActivity ">

  <relativelayout
    android:layout_width=" Match_parent "
    android:layout_height= "200DP" >

    <videoview
      android:id= "@+id/videoview"
      android: Layout_width= "Match_parent"
      android:layout_height= "match_parent"
      />

  </RelativeLayout>


</RelativeLayout>

3, Code section

Package app.com.myapplication;
Import Android.media.MediaPlayer;
Import Android.net.Uri;
Import Android.os.Bundle;
Import android.os.Environment;
Import android.support.v7.app.AppCompatActivity;
Import Android.widget.MediaController;
Import Android.widget.Toast;

Import Android.widget.VideoView;

  public class Localvideoactivity extends Appcompatactivity {private Videoview videoview;
    @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);

    Setcontentview (R.layout.activity_local_video); Local video needs to add a fl1234.mp4 video String videoUrl1 = Environment.getexternalstoragedirectory () to the phone's SD card root directory. GetPath () + "/fl1234

    . mp4 ";

    Network video String videoUrl2 = Utils.videourl;

    Uri uri = Uri.parse (VIDEOURL2);

    Videoview = (videoview) This.findviewbyid (R.id.videoview);

    Set the video controller Videoview.setmediacontroller (new Mediacontroller (this)); Playback Complete callback Videoview.setoncompletionlistener (new MyplayeroncompletionlisteneR ());

    Sets the video path Videoview.setvideouri (URI);
  Start playing video Videoview.start (); Class Myplayeroncompletionlistener implements Mediaplayer.oncompletionlistener {@Override public void oncom
    Pletion (MediaPlayer MP) {Toast.maketext (Localvideoactivity.this, "Play completed", Toast.length_short). Show (); }
  }
}
Package app.com.myapplication;

/**
 * Created by ${zyj} on 2016/4/19.
 * * Public
class Utils {public

  static final String Videourl = "Http://clips.vorwaerts-gmbh.de/big_buck_ Bunny.mp4 ";

}

4, summary

Android's way of using Videoview to play local video and web video is almost over, and hopefully it will help to develop Android. If you have questions, you can leave a message for discussion.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.