开发者

Android - Play Video does not work

开发者 https://www.devze.com 2023-03-03 13:27 出处:网络
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)

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:

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


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


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.


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();
                }
            }
        });
    }
}

}

0

精彩评论

暂无评论...
验证码 换一张
取 消