3

我尝试从 url 播放视频,但只有本地视频在我的代码中有效。如果我尝试从 url 打开视频,我的 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();
    }
}

这是从 url 播放视频的代码,但它不起作用

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

希望你能帮我解决我的问题,

问候基督徒

4

1 回答 1

5

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

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
于 2012-11-21T17:09:25.147 回答