2

I'm a newbie on android java programming with Eclipse (I have a .NET background) and fail to get a video played in an emulator (nor on an android htc desire)

My setup:

  • Eclipse IDE for Java Developers, Version: Helios Service Release 2
  • Android SDK and ADT Plugin for Eclipse

The video I'm using is a 3gp video I recorded with my htc desire.

I have tried the 2 approaches with

  1. VideoView
  2. SurfaceView with a MediaPlayer which I took out of the book from Reto Meier "Professional Android 2 Application Development".

I'm targetting Android 2.2 (both in the emulator, as well on my htc desire)

Details

1. VideoView approach

getDuration always returns -1 and even after calling videoView.start() isPlaying is false. Here's the code I'm using:

public class MyActivity extends Activity 
{
    private static final String FileName = "VIDEO0015.3gp";
    private static final String MyTag = "MyActivity";

    /** 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.videoView1);
        videoView.setKeepScreenOn(true);

        File clip=new File(Environment.getExternalStorageDirectory(), FileName);
        if (clip.exists())
        {
            videoView.setVideoPath(clip.getAbsolutePath());
            int duration = videoView.getDuration();

            if (videoView.canSeekForward())
            {
                videoView.seekTo(duration/2);
            }

            videoView.start();
        }
   }
}

The 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"
    >
<VideoView android:id="@+id/videoView1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content">
</VideoView>
</LinearLayout>

2. SurfaceView with MediaPlayer With this approach, I get the correct duration (about 15 sec) and isPlaying is true. Both nothing is to be seen and also video playing never is completed.

I will add the code if somebody wants to check it.


Thanks for any help, I'm getting desperate.

Frank

FrankE
  • 313
  • 1
  • 4
  • 14
  • Seems odd, could you post your main.xml (don't forget to format it as code - just mark it and press ctrl+k), and have you tried playing another video (not recorded on the phone)? – MByD May 08 '11 at 11:04
  • @MByD: Thanks for your help. I have added the contents of the main.xml file. I have played around a bit with the layout settings, but no change. I'm really a newbie, so maybe Im doing something fundamentally wrong. About other videos, I tried one other, which failed with a different error. Because the second attempt (with the surfaceView and mediaPlayer) returns the correct duration and says isPlaying = true I assumed that the video is ok. I will check another video. Is there any video that is guaranteed to work? – FrankE May 09 '11 at 17:05
  • I tried your code and got the same result (with valid video), I don't know why, maybe I'll have time to check it later. – MByD May 09 '11 at 17:25

3 Answers3

0

In case you haven't found a solution, VideoView.getDuration() will return -1 if the video is not in playback state. The video is not in playback state until it has been prepared. So calling VideoView.getDuration() directly after setting the URI does not guarantee that the video has been prepared.

I found this by looking at the source of VideoView:

@Override
public int getDuration() {
    if (isInPlaybackState()) {
        return mMediaPlayer.getDuration();
    }

    return -1;
}

The solution is to set an OnPreparedListener to your VideoView, and obtain/use the duration once the video is prepared. You can then use VideoView.getDuration() or MediaPlayer.getDuration(), which are nearly identical.

Solution:

public class MyActivity extends Activity {
private static final String FileName = "VIDEO0015.3gp";
private static final String MyTag = "MyActivity";

/**
 * 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.videoView1);
    videoView.setKeepScreenOn(true);

    File clip = new File(Environment.getExternalStorageDirectory(), FileName);
    if (clip.exists()) {
        videoView.setVideoPath(clip.getAbsolutePath());
        videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                int duration = videoView.getDuration();
                if (videoView.canSeekForward()) {
                    videoView.seekTo(duration / 2);
                    videoView.start();
                }
            }
        });
    }
}

}

vman
  • 1,264
  • 11
  • 20
0

I faced a similar problem before. Did you try 'hinting' the 3gp video? You can have a look at here. Hope it will help.

Community
  • 1
  • 1
Muhammad Razib
  • 1,297
  • 9
  • 13
0

Hi I have write some testing video view program before. And I think maybe is your video file path wrong? Here is my testing code,

public class VideoActivity  extends Activity {
    /** Called when the activity is first created. */
    @Override
 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.video_view_layout);

        VideoView videoView = (VideoView) findViewById(R.id.VideoView);
        MediaController mediaController = new MediaController(this);
        mediaController.setAnchorView(videoView);

        Uri video = Uri.parse("/sdcard/test_video.mp4");
        videoView.setMediaController(mediaController);
        videoView.setVideoURI(video);
        videoView.start();
        }
}

And the XML is,

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

AndroidManifest.xml,

        <activity android:name=".VideoActivity"
                  android:label="@string/app_name"
                  android:screenOrientation="landscape"
                  android:theme="@android:style/Theme.NoTitleBar"
                  android:noHistory="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

Hope can help you.

Rebecca
  • 85
  • 1
  • 8
  • In this case, the video file is put into the /sdcard root level. – Rebecca May 09 '11 at 07:43
  • thanks for your help. I tried your code, but I get _No Launcher activity found! The launch will only sync the application package on the device!_ I'm a newbie so I do not know the cause of the problem. I think that the path to my video file is OK, because I check it in the code with `File clip=new File(Environment.getExternalStorageDirectory(), FileName); if (clip.exists())` – FrankE May 09 '11 at 17:22