Reputation: 292
I am able to play youtube video through webview but not able to play video which are situated at my own server
Upvotes: 0
Views: 136
Reputation: 3679
It's actually better to play the video directly from the server and give an option to download the view if the user prefers.
Play the video
String SrcPath = "link.fileformat";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
VideoView myVideoView = (VideoView)findViewById(R.id.myvideoview);
myVideoView.setVideoURI(Uri.parse(SrcPath));
myVideoView.setMediaController(new MediaController(this));
myVideoView.requestFocus();
myVideoView.start();
}
Download Video to SdCard:
try {
URL url = new URL(provide any URL);
HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
String PATH = Environment.getExternalStorageDirectory()
+ "/download/";
Log.v(LOG_TAG, "PATH: " + PATH);
File file = new File(PATH);
file.mkdirs();
String fileName = "Test.mp3";
File outputFile = new File(file, fileName);
FileOutputStream fos = new FileOutputStream(outputFile);
InputStream is = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = is.read(buffer)) != -1) {
fos.write(buffer, 0, len1);
}
fos.close();
is.close();
// }
} catch (IOException e) {
Log.d(LOG_TAG, "Error: " + e);
Toast.makeText(myApp, "error " + e.toString(), Toast.LENGTH_LONG)
.show();
}
and do add
the required permissions
Upvotes: 1