Question

I am trying to play a video using VideoView. I get a force close message when I run my app. Somebody please help me. The following is my code.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<VideoView
    android:id="@+id/videoView1"
    android:layout_width="match_parent"
    android:layout_height="328dp" />

<Button
    android:id="@+id/button1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/play" />


<Button
    android:id="@+id/button2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/pause" />

<Button
    android:id="@+id/button3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/stop" />

</LinearLayout>


package ram.videoplayer;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.VideoView;


public class VideoplayerActivity extends Activity {
/** Called when the activity is first created. */

VideoView vv;
View v;
int id;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    vv = (VideoView)findViewById(R.id.videoView1);
    Uri u = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.v);
    vv.setVideoURI(u);

    id = v.getId();

    switch (id) {

    case R.id.button1:
        vv.start();
        break;

    case R.id.button2:
        vv.pause();
        break;

    case R.id.button3:
        vv.stopPlayback();
        break;

       } 
    }
}

Initially this was my code. It wasn't working. Then I tried to reduce my code and I deleted all the three buttons I was using. I was using vv.start(); straight away without any button clicks. I had bid of success when I did so. I was able to hear my video running. But unfortunately I could see no video on my screen. Then I modified my code. I used vv.setVideoPath("/sdcard/v.mp4"); instead of the Uri statements. It resulted in a "can't play video" message. I searched for answers. I have found no satisfying answers so far. I am using android 2.2 and when I was on my hunt I found some statements like " vv.setVideoPath("/sdcard/v.mp4"); won't work in android 2.2" So I have 3 questions now.

  1. Will setVideoPath() really won't work in android 2.2 ?
  2. Why couldn't I see my video when I was using Uri ?
  3. What went wrong when I added buttons to my code ?

Please help me.

Thanks in advance.

Was it helpful?

Solution

In your xml file, for each button add this line android:onClick="buttonClick"
in your activity class change your code to this:

 VideoView vv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    vv = (VideoView) findViewById(R.id.videoView1);
    Uri u = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.v);
    vv.setVideoURI(u);
}

public void buttonClick(View v){
    switch (v.getId()) {

        case R.id.button1:
            vv.start();
            break;

        case R.id.button2:
            vv.pause();
            break;

        case R.id.button3:
            vv.stopPlayback();
            break;

    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top