1

I am trying to play a video from a url in android, for this I am using videoview control in the fragment class, but while playing there is no video output only audio output is there, the video is in .mp4 format. I have done it the following way-

  VideoView vv;
  vv=(VideoView)view.findViewById(R.id.videoView1);
  String src="video path";
  Uri uri=Uri.parse(src);   
  getActivity().getWindow().setFormat(PixelFormat.TRANSLUCENT);     
  MediaController mc=new MediaController(getActivity());
  mc.setMediaPlayer(vv);
  vv.setMediaController(mc);
  vv.setVideoURI(uri);
  vv.requestFocus();
  vv.start();

This is my xml code

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="#303040" >

    <RelativeLayout
        android:layout_width="230dp"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/headLine"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="100dp"
            android:layout_marginTop="5dp"
            android:textColor="#AFDEFE"
            android:textSize="14sp"
            android:textStyle="italic"
            android:typeface="serif" />

        <com.facebook.widget.ProfilePictureView
            android:id="@+id/fbPic"
            android:layout_width="72dp"
            android:layout_height="72dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginTop="5dp"
            android:layout_marginLeft="20dp" >
        </com.facebook.widget.ProfilePictureView>
    </RelativeLayout>

    <com.facebook.widget.LoginButton
            android:id="@+id/loginButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="30dp" />

    <Button
        android:id="@+id/settingsButton"
        android:layout_width="111dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="32dp"
        android:text="Play" />

    <VideoView
        android:id="@+id/videoView1"
        android:layout_width="200dp"
        android:layout_height="140dp"
        android:layout_marginLeft="60dp"
        android:layout_marginTop="10dp"
        android:keepScreenOn="true" />

</LinearLayout>
Y0gesh Gupta
  • 2,184
  • 5
  • 40
  • 56

2 Answers2

2

add only one line

vv.setZOrderOnTop(true);
Yogesh Rathi
  • 6,331
  • 4
  • 51
  • 81
1

Here try my code I suggest you for the following Code wherein I am running my application successfully

The Code is as Follows:

XML file:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#f0f0f0" >

    <Button
        android:id="@+id/btnVideoGallery"
        android:layout_width="75dp"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="15dp"
        android:text="@string/gallery" />

    <Button
        android:id="@+id/btnCancel"
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btnVideoGallery"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="22dp"
        android:text="@string/cancel" />

    <TextView
        android:id="@+id/lblDisplayImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btnCancel"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:text="@string/below_this_text_video_will_be_displayed"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000000"
        android:textSize="13dp" />

    <VideoView
        android:id="@+id/vvDisplayVideo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/lblDisplayImage"
        android:layout_marginTop="15dp" />

</RelativeLayout>

Java File:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.VideoView;

public class VideoActivity extends Activity {

    private Button btnVideoGallery,btnCancel;
    private VideoView vvDisplayVideo;
    /** The Constant PICK_VIDEO. */
    private static final int PICK_VIDEO=1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_options);

        btnVideoGallery=(Button)findViewById(R.id.btnVideoGallery);
        vvDisplayVideo=(VideoView)findViewById(R.id.vvDisplayVideo);
        btnCancel=(Button)findViewById(R.id.btnCancel);
        vvDisplayVideo.setVisibility(View.GONE);

        btnVideoGallery.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                Intent video=new Intent();
                video.setAction(Intent.ACTION_PICK);
                video.setType("video/*");
                startActivityForResult(video, PICK_VIDEO);

            }
        });

        btnCancel.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                Intent goStartUp=new Intent(VideoActivity.this, StartUpActivity.class);
                goStartUp.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(goStartUp);
                finish();
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        if (resultCode==Activity.RESULT_OK && requestCode == PICK_VIDEO) {

            vvDisplayVideo.setVisibility(View.VISIBLE);
            vvDisplayVideo.setVideoURI(data.getData());
            vvDisplayVideo.setFocusable(true);
            MediaController mc=new MediaController(this);
            vvDisplayVideo.setMediaController(mc);
            Log.i("True", "Executed");
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub

        Intent goStartUp=new Intent(VideoActivity.this, StartUpActivity.class);
        goStartUp.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(goStartUp);
        finish();
        return super.onKeyDown(keyCode, event);
    }
}

Also you can modify the Manifest File as per your use:

<manifest ...>
    <uses-sdk .../>
    <uses-permission android:name="android.permission.CAMERA" />
        <uses-permission android:name="android.permission.RECORD_VIDEO" />
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

        <uses-feature
            android:name="android.hardware.camera"
            android:required="false" />

    <application ...>
    </application>

</manifest>
rekire
  • 47,260
  • 30
  • 167
  • 264
Hardik Vora
  • 414
  • 2
  • 9
  • Hi, thanks for the solution, but again no video is being showed up, only audio is there. – Y0gesh Gupta Dec 13 '12 at 17:12
  • I am doing it on a fragment, I hope it wont make any difference? – Y0gesh Gupta Dec 13 '12 at 17:58
  • i also think that it should not make any difference – Hardik Vora Dec 14 '12 at 10:01
  • hi @slAcker did it worked for you?I am asking you because if you are getting the solution of your problem please accept it as others will also know about this link as a useful link & if you are still not getting the solution try my code by using Activity.It will definitely help you.I really hope this will work for you. – Hardik Vora Dec 20 '12 at 16:41
  • Hi hardik, no it didn't work for me I am thinking of creating a dedicated activity to play video instead of adding it on an already congested activity. – Y0gesh Gupta Dec 21 '12 at 19:10
  • @HardikVora you should really learn how to format an answer propper. – rekire Dec 28 '12 at 13:51