Video player not working - Android Studio
Asked Answered
B

2

0

I am trying to add a video played on button click with android studio. However, when I click the button a "sorry, this video cannot be played" message box appears on the emulator screen.

Can you help me see where I'm going wrong. Below is the code I approached the goal with

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

<?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>
Baikal answered 22/6, 2011 at 10:38 Comment(6)
This is probably an invalid path, can you post the full path of the video file?Surreptitious
where is your video file . Is is it raw folder inside the app or it is in sdcard ?Dunlavy
also check the file is not corrupted and is a valid 3gpConveyor
now i have stored in rw folder and the file is not currput it is working properly I have played it just a min ago....Baikal
the full path of the file is C:/workspace/TrialVideo/raw/lic.3gpBaikal
and other one video also placed in the raw folder in .mp4 file formatBaikal
K
2

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>

place the video on the raw folder and run the code. Sometimes video will not be correctly shown on the emulator, try to check it also on the actual device.

Katelyn answered 22/6, 2011 at 12:43 Comment(4)
SorryBut, this one code also not wroking. I have used same code lines and creates a new project instead of using it in my previous project. And now whenever I'll run my project it will show's "The process android.com.TrialVideo has stopped unexpectedly please try again"Baikal
Is the problem relate with the version of Android. Or any version can have a capability to run the video files directly or on a buttn click..Baikal
@Baikal Did you check this on physical device?. please run that on a mobile because some videos doesn't run on Emulator but works on phone thats why. Please try this once.Katelyn
tHANKS IT WILL WORKED FOR ME. i HAVE FORGOT TO PUT THE RAW FOLDER INTO THE RES DIRECTOY THAT'S WHY THE APPLICATION DOES NOT SUPPORT MY VIDEO. NOW IT WILL WORKED CORRECTLY.Baikal
L
0

Android does not have a C: drive. You need to put the video file on the device (e.g., copy it to the device's external storage), then supply VideoView with an appropriate path to the file (e.g., using Environment.getExternalStorageDirectory()).

Larkin answered 22/6, 2011 at 12:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.