[Java] Android Videoview can only handle local files… here is a solution to the problem.
[Java] Android Videoview can only handle local files
I tried playing a video from a url, but only the local video works in my code. If I try to open a video from the URL displayed by my Nexus 7
This video cannot be played. This is the code to play the local file, and it works fine (all the same video).
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();
}
}
This is the code to play the video from the URL, but it doesn’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 solve my problem,
Greeting.
Christians
Solution
Ensure that you have the correct internet access; Put the following line into your AndroidManifest.xml:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />