Video player does not work!

here is all my code to play the video when the button is pressed, but whenever I press the button, a message box appears on the emulator screen and the message “sorry, this video cannot be played”. Do not tell me where I am wrong.

File Trialvideo.java

package android.com.trialvideo; import android.app.Activity; import android.graphics.PixelFormat; import android.media.MediaPlayer; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.MediaController; import android.widget.VideoView; public class TrialVideoActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); /** // Video view: to view our video VideoView video = (VideoView) findViewById(R.id.surface_view); //set video path to our video(in this case man-cheetah-gazalle.3gp) video.setVideoPath("/raw/jeewan.mp4"); video.start(); **/ final Button play =(Button)findViewById(R.id.play); play.setOnClickListener(new OnClickListener(){ public void onClick(View V){ videoPlayer(); } });} public void videoPlayer(){ getWindow().setFormat(PixelFormat.TRANSLUCENT); VideoView videoHolder = (VideoView)findViewById(R.id.surface_view); videoHolder.setMediaController(new MediaController(this)); videoHolder.setVideoPath("/TrialVideo/raw/lic.3gp"); videoHolder.requestFocus(); videoHolder.start(); } } 

main.xml 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" > <Button android:layout_height="50dip" android:text="play" android:id="@+id/play" android:layout_width="50dip" > </Button> <VideoView android:id="@+id/surface_view" android:layout_width="475px" android:layout_height="440px" /> </LinearLayout> <?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" > <Button android:layout_height="50dip" android:text="play" android:id="@+id/play" android:layout_width="50dip" > </Button> <VideoView android:id="@+id/surface_view" android:layout_width="475px" android:layout_height="440px" /> </LinearLayout> 
0
source share
2 answers

Hi, try the following code:

VideoPlaying.java

 public class VideoPlaying extends Activity { private MediaController mc; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); VideoView vd = (VideoView) findViewById(R.id.VideoView); Uri uri = Uri.parse("android.resource://" + getPackageName() + "/"+R.raw.VideoName); mc = new MediaController(this); vd.setMediaController(mc); vd.requestFocus(); vd.setVideoURI(uri); vd.start(); } } 

main.xml

 <?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" android:gravity="center"> <VideoView android:layout_height="fill_parent" android:layout_width="fill_parent" android:id="@+id/VideoView"></VideoView> </LinearLayout> 

put the video in a raw folder and run the code. Sometimes the video does not display correctly on the emulator, try also checking it on the device itself.

+2
source

Android does not have a C: drive. You need to place the video file on the device (for example, copy it to the external storage of the device), then provide the VideoView with the appropriate path to the file (for example, using Environment.getExternalStorageDirectory() ).

0
source

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


All Articles