Android - 播放视频不起作用

时间:2011-05-08 10:53:48

标签: android video

我是使用Eclipse进行android java编程的新手(我有一个.NET背景)并且无法在模拟器中播放视频(也没有在android htc欲望中播放)

我的设置:

  • Eclipse IDE for Java Developers,版本:Helios Service Release 2
  • 适用于Eclipse的Android SDK和ADT插件

我正在使用的视频是我用htc欲望录制的3gp视频。

我用

尝试了两种方法
  1. VideoView
  2. 使用MediaPlayer的SurfaceView 我从Reto Meier“专业Android 2应用程序开发”中摘取了这本书。
  3. 我的目标是Android 2.2(在模拟器中,以及我的htc愿望)

    详细

    1。 VideoView方法

    getDuration总是返回-1,甚至在调用videoView.start()后,isPlaying为false。 这是我正在使用的代码:

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

    main.xml文件:

     <?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 通过这种方法,我得到正确的持续时间(大约15秒)并且isPlaying为真。 两者都没有被看到,视频播放也永远不会完成。

    如果有人想检查,我会添加代码。


    感谢您的帮助,我感到非常绝望。

3 个答案:

答案 0 :(得分:0)

我之前遇到过类似的问题。你试过'暗示'3gp视频吗?你可以看一下here。希望它会有所帮助。

答案 1 :(得分:0)

您好我以前写了一些测试视频观看程序。 我想也许你的视频文件路径错了? 这是我的测试代码,

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

XML就是,

<?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>
希望可以帮助你。

答案 2 :(得分:0)

如果您还没有找到解决方案,如果视频未处于播放状态,VideoView.getDuration()将返回-1。视频在准备好之前不处于播放状态。因此,在设置URI后直接调用VideoView.getDuration()并不能保证视频已经准备好。

我通过查看VideoView的来源找到了这个:

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

    return -1;
}

解决方案是将OnPreparedListener设置为VideoView,并在准备好视频后获取/使用持续时间。然后,您可以使用几乎相同的VideoView.getDuration()MediaPlayer.getDuration()

解决方案:

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

}

相关问题