Android VideoView and MediaController example
The following code will play a video from a video url, using the VideoView for playing the video and MediaController for video playing controller.
1. Make sure the INTERNET permission is in the manifest file.
<uses-permission android:name="android.permission.INTERNET" />
2. Create a layout file with VideoView in it.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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="com.example.mediacontroller_videoview.MainActivity"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center"> <VideoView android:id="@+id/video_view" android:layout_width="match_parent" android:layout_height="230dp" /> </RelativeLayout> </LinearLayout>
3. The activity class for playing the video.
import android.media.MediaPlayer import android.os.Bundle import android.support.v7.app.AppCompatActivity import android.widget.MediaController import kotlinx.android.synthetic.main.activity_main.* class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // http://techslides.com/demos/sample-videos/small.mp4 // http://www.ebookfrenzy.com/android_book/movie.mp4 // val videoPath = "android.resource://" + packageName + "/" + R.raw.sample_video video_view.setVideoPath("http://techslides.com/demos/sample-videos/small.mp4") var mediaController = MediaController(this) mediaController.requestFocus() video_view.setOnPreparedListener(MediaPlayer.OnPreparedListener { mediaController.show(0) }) mediaController.setAnchorView(video_view) video_view.setMediaController(mediaController) video_view.start() } }
Search within Codexpedia
Custom Search
Search the entire web
Custom Search
Related Posts