如何在android中播放.3GP视频文件

时间:2011-01-27 15:08:00

标签: android android-videoview

HI 我想在Android手机中播放.3GP视频文件。我尝试下面的代码,但它显示无法播放视频。所以请告诉我我会做什么

这是我的代码

 public class VideoPlay extends Activity {

private String path ;
private VideoView mVideoView;

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.videoplay);
    path="http://www.boodang.com/api/videobb/101009_Pure.3gp";
    mVideoView = (VideoView) findViewById(R.id.video);

    if (path == "") {
        // Tell the user to provide a media file URL/path.
        Toast.makeText(
                VideoPlay.this,
                "Please edit VideoViewDemo Activity, and set path"
                        + " variable to your media file URL/path",
                Toast.LENGTH_LONG).show();

    } else {

        /*
         * Alternatively,for streaming media you can use
         * mVideoView.setVideoURI(Uri.parse(URLstring));
         */
        mVideoView.setVideoPath(path);
        mVideoView.setMediaController(new MediaController(this));
        mVideoView.requestFocus();

    }
}
}

XML布局

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

3 个答案:

答案 0 :(得分:2)

检查Android SDK演示中的以下代码

package com.example.android.apis.media;

import com.example.android.apis.R;

import android.app.Activity;

import android.os.Bundle;

import android.widget.MediaController;

import android.widget.Toast;

import android.widget.VideoView;

public class VideoViewDemo extends Activity {

    /**
     * TODO: Set the path variable to a streaming video URL or a local media
     * file path.
     */
    private String path = "";
    private VideoView mVideoView;

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.videoview);
        mVideoView = (VideoView) findViewById(R.id.surface_view);

        if (path == "") {
            // Tell the user to provide a media file URL/path.
            Toast.makeText(
                    VideoViewDemo.this,
                    "Please edit VideoViewDemo Activity, and set path"
                            + " variable to your media file URL/path",
                    Toast.LENGTH_LONG).show();

        } else {

            /*
             * Alternatively,for streaming media you can use
             * mVideoView.setVideoURI(Uri.parse(URLstring));
             */
            mVideoView.setVideoPath(path);
            mVideoView.setMediaController(new MediaController(this));
            mVideoView.requestFocus();

        }
    }
}

videoview.xml

<VideoView 
    android:id="@+id/surface_view" 
    android:layout_width="320px"
    android:layout_height="240px"
/>

答案 1 :(得分:0)

This article提供与您的示例类似的代码,但存在一些差异,尤其是video.start和您的示例完全缺少MediaController.show

我建议稍微清理一下代码,然后尝试上述文章中的建议。在文章讨论中也有一些很好的反馈。

答案 2 :(得分:0)

正如@Peter Lillevold建议的那样,您应首先尝试视频播放器的参考实现。以下是一些链接:

尝试使用已知工作视频文件的这些播放器,this post中有一些链接。如果您实现播放器,并且这些参考视频有效,但您的.3gp视频没有,则问题可能是视频文件本身未按标准编码。