How to write a simple video player on android?

I have a code for a simple audio player on Android. But it does not work for video. Please guide me to write a simple video player. Music player code

package com.example.helloplayer; public class HelloPlayer extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); MediaPlayer mp = MediaPlayer.create(this, R.raw.file); mp.start(); } } 
+6
source share
1 answer

To create a video player, you will need to use video viewing. sample is shown below

Layout file

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <VideoView android:id="@+id/videoView" android:layout_height="fill_parent" android:layout_width="fill_parent"/> </LinearLayout> 

Here I play the video stored in the "resource / raw" folder, "one" is the name of the video file, you can replace it with the name of your video file. Also make sure that you are going to play Android supported video format

VideoplayerActivitvity

 import android.app.Activity; import android.net.Uri; import android.os.Bundle; import android.widget.MediaController; import android.widget.Toast; import android.widget.VideoView; public class VideoplayerActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); VideoView videoView =(VideoView)findViewById(R.id.videoView); MediaController mediaController= new MediaController(this); mediaController.setAnchorView(videoView); Uri uri=Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.one); videoView.setMediaController(mediaController); videoView.setVideoURI(uri); videoView.requestFocus(); videoView.start(); } } 
+16
source

Source: https://habr.com/ru/post/891935/


All Articles