[Java]Android Videoview works only with local files

对着背影说爱祢 提交于 2019-12-23 19:25:47

问题


I tried to play a video from a url, but only local videos works in my code. If I try to open a video from a url my nexus 7 displays Can not play this video. Here is the code for the playback of a local file, it works fine (both are the same videos)

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

and here is the code for the playback of video from a url, but it don't work

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

hope you can help me to solve my problem,

regards christian


回答1:


Make sure you have the correct permissions for internet access; put the following lines in your AndroidManifest.xml:

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


来源:https://stackoverflow.com/questions/13498315/javaandroid-videoview-works-only-with-local-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!