[Java] Android Videoview仅适用于本地文件

时间:2012-11-21 17:02:44

标签: java android android-videoview

我尝试从网址播放视频,但只有本地视频在我的代码中有效。如果我尝试从网址打开视频,我的nexus 7会显示 无法播放此视频。这是播放本地文件的代码,它工作正常(两者都是相同的视频)

package com.sample.prog;

import java.io.File;

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

public class MainActivity extends Activity {

    static private final String pathToFile = "bigbuck.mp4";
    private VideoView videoPlayer;

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

        File root = Environment.getExternalStorageDirectory(); 

        videoPlayer = (VideoView) findViewById(R.id.videoPlayer);   
        videoPlayer.setKeepScreenOn(true);    
        videoPlayer.setVideoPath(root + "/" + pathToFile);
        videoPlayer.start();
    }
}

以下是从网址播放视频的代码,但不起作用

package com.sample.prog;

import java.io.File;

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

public class MainActivity extends Activity {

    static private final String pathToFile = "http://www.myanimesource.de/bigbuck.mp4";
    private VideoView videoPlayer;

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

        //File root = Environment.getExternalStorageDirectory(); 

        videoPlayer = (VideoView) findViewById(R.id.videoPlayer);   
        videoPlayer.setKeepScreenOn(true);    
        videoPlayer.setVideoPath(URI.parse(pathToFile));
        videoPlayer.start();
    }
}
希望你能帮助我解决我的问题,

问候 基督教

1 个答案:

答案 0 :(得分:5)

确保您拥有正确的互联网访问权限;将以下行放在AndroidManifest.xml中:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />