Play video in VideoView on Android

I cannot understand why I cannot play the video in my VideoView. All I get for the message is:

Unable to play video: Sorry, this video cannot be played.

I created an SD card for my emulator. Do I need to put the SD card in a specific folder in the SDK? Please comment.

Here's the layout:

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

Here is the code:

 package com.examples.videoviewdemo; import android.app.Activity; import android.os.Bundle; import android.widget.MediaController; import android.widget.VideoView; public class VideoViewDemo 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); //videoView.setMediaController(mediaController); videoView.setVideoPath("/sdcard/blonde_secretary.3gp"); videoView.start(); } } 

Waiting for an answer...

+50
android
Jul 16 2018-10-16T00:
source share
13 answers

I assume your video is not compatible with Android. Try this with another video. This one definitely works , used to work with Android (but for some reason does not work on newer devices). If this video works, but yours does not, then your video is not compatible with Android.

As others have shown, please check this on the device. Playing video on an emulator requires too much power.

+28
Aug 18 '10 at 21:08
source share

Project example

Finally, I got a draft evidence-based concept for the job, so I'll talk about it here.

Layout Customization

The layout is set up in such a way where the light gray area is VideoView .

enter image description here

activity_main.xml

 <?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:id="@+id/activity_main" 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="com.example.videotest.MainActivity"> <VideoView android:id="@+id/videoview" android:layout_width="300dp" android:layout_height="200dp"/> <Button android:text="Play" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/videoview" android:onClick="onButtonClick" android:id="@+id/button"/> </RelativeLayout> 

Prepare a video clip

According to the documentation here . It uses the powerful ffmpeg command line tool to convert to what should play on all (hopefully?) Android devices. Read the answer I linked to get more explanation. I used a slightly modified version because I was getting errors with the original version.

 ffmpeg -y -i input_file.avi -s 432x320 -b:v 384k -vcodec libx264 -flags +loop+mv4 -cmp 256 -partitions +parti4x4+parti8x8+partp4x4+partp8x8 -subq 6 -trellis 0 -refs 5 -bf 0 -coder 0 -me_range 16 -g 250 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -qmin 10 -qmax 51 -qdiff 4 -c:a aac -ac 1 -ar 16000 -r 13 -ab 32000 -aspect 3:2 -strict -2 output_file.mp4 

I would definitely read a lot more about each of these options to find out which ones need to be adjusted, how much the video and sound quality goes.

Then rename output_file.mp4 to test.mp4 and put it in the Android /res/raw folder. Create a folder if it does not already exist.

The code

There is not much in the code. The video is played when you click the Play button. Thanks this answer for help.

MainActivity.java

 public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void onButtonClick(View v) { VideoView videoview = (VideoView) findViewById(R.id.videoview); Uri uri = Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.test); videoview.setVideoURI(uri); videoview.start(); } } 

Passed

It's all. Now you can play your video clip on a simulator or a real device.

+15
Dec 09 '16 at 13:45
source share

Make the videoView member variable of your activity class instead of storing it as local to the onCreate function:

 VideoView videoView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); videoView = (VideoView)findViewById(R.id.VideoView); videoView.setVideoPath("/sdcard/blonde_secretary.3gp"); videoView.start(); } 
+6
Jun 22 '12 at 11:56
source share

Add android.permission.READ_EXTERNAL_STORAGE to the manifest, worked for me

+5
Feb 17 '15 at 6:52
source share

The code seems flawless! Simple and straightforward.

Therefore, he must work by telephone. The emulator has difficulty playing the video, it happened to me too.

Try to upgrade the API to the last, it can help!

Right-click on an open project, select Properties> Android> check the latest version on the right ...

Igor

+2
Jul 19 '10 at 14:16
source share

You can access your SD card using DDMS

+2
Jul 19 '10 at 14:19
source share

VideoView can only play Stream 3gp videos. I recommend this code to broadcast your video or try a higher version of Android. Try streaming video online.

 public void onCreate(Bundle savedInstanceState) { setContentView(R.layout.main); String videourl = "http://something.com/blah.mp4"; Uri uri = Uri.parse(videourl); Intent intent = new Intent(Intent.ACTION_VIEW, uri); intent.setDataAndType(uri, "video/mp4"); startActivity(intent); } 

Or Click here to view the Android streaming tutorial .

+2
Jun 05 '13 at 13:53 on
source share

To confirm that the video is in the correct format (resolution, bit rate, codec, etc.), check the official documentation box - excerpt below:

Standard definition (low quality)
Video Codec - H.264
Video Resolution - 176 x 144 px
Video frame rate - 12 fps
Video Bitrate - 56 Kbps
Audio Codec - AAC-LC
Audio channels - (mono)
Audio Bitrate - 24 Kbps

Standard Definition (High Quality)
Video Codec - H.264
Video Resolution - 480 x 360 px
Video frame rate - 30 fps
Video Bitrate - 500 Kbps
Audio Codec - AAC-LC
Audio channels - 2 (stereo)
Audio Bitrate - 128 Kbps

720p high resolution (N / A on all devices)
Video Codec - H.264
Video Resolution - 1280 x 720 px
Video frame rate - 30 fps
Video Bitrate - 2 Mbps
Audio Codec - AAC-LC
Audio channels - 2 (stereo)
Audio Bitrate - 192 Kbps

+2
Apr 29 '14 at 8:29
source share
 VideoView videoView =(VideoView) findViewById(R.id.videoViewId); Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getAbsolutePath()+"/yourvideo"); videoView.setVideoURI(uri); videoView.start(); 

Instead of using setVideoPath, use setVideoUri. you can get the path to your video stored in external storage using (Environment.getExternalStorageDirectory (). getAbsolutePath () + "/ yourvideo") and parse it in Uri. If your video is stored in sdcard / MyVideo / video.mp4, replace "/ yourvideo" in the code with "/MyVideo/video.mp4"

This works great for me :) `

+2
Feb 12 '16 at 7:22
source share

Good! if you are using video compatibility for Android, the only reason for this warning is that you must use videos larger than 300 MB. Android does not support large video (> 300 MB). We can get it through NDK optimization.

0
Apr 16 2018-12-12T00:
source share

The problem may be with the movie format. If it is encoded by H264, make sure it is in the base profile.

0
Mar 19 '13 at 5:34
source share

I have almost the same problem with VideoView. I'm trying to play a video (1080 * 1080) with a Nexus 5 and it works well, but the same video on Galaxy ace 2 gives me the message Can not Play Video.

But I notice that with a lower definition video (120x120) it works fine.

So maybe itโ€™s all about the โ€œSizeโ€, (and especially with the blonde_secretary.3gp video ....)

0
Feb 25 '15 at 19:20
source share
 //just copy this code to your main activity. if ( ContextCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED ){ if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, android.Manifest.permission.READ_EXTERNAL_STORAGE)){ }else { ActivityCompat.requestPermissions(MainActivity.this,new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE},1); } }else { } 
0
Apr 21 '17 at 13:12
source share



All Articles