How to play video in android 4.0 and above - android

Bs"d
Hi everybody
I need to play a video
I have created an VideoView that takes uri parameter
and plays it
In Galaxy s2 and Galaxy s1 it works good but in Galaxy s3
it doesn't play
This is my source code
video=new VideoView(this);
MediaController mc = new MediaController(this);
video.setMediaController(mc);
Uri uri = Uri.parse(url);
video.setVideoURI(uri);
video.requestFocus();
setContentView(video);
video.start();

Related

How to play videos with phone's standard video player app

I want to play videos i have stored in the external storage with the phone's standard video Player app. I've tried using FileProvider, but I can't manage to pass the video to the player.
private void passVideo(String videoname){
File videoPath = new File(Environment.getExternalStorageDirectory(), "video_folder");
File newFile = new File(videoPath, videoname);
Uri path = FileProvider.getUriForFile(this, "com.example.provider", newFile);
Intent shareIntent = ShareCompat.IntentBuilder.from(this)
.setType(getContentResolver().getType(path))
.setStream(path)
.getIntent();
shareIntent.setData(path);
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(shareIntent, "Open Video..."));
}
With this code I manage to get the Chooser for gmail, whatsapp and other social media platforms, but that's not what I want and they all say they can't handle the file format anyway. It also gives the option to play the video with VLC, but it instantly crashes.
I have tried every possible file format and none of them works.
Sorry if I'm missing something obvious, I am still a beginner.
ShareCompat.IntentBuilder is for ACTION_SEND, which is not the typical Intent action for playing a video. ACTION_VIEW would be more typical. So, try:
private void passVideo(String videoname){
File videoPath = new File(Environment.getExternalStorageDirectory(), "video_folder");
File newFile = new File(videoPath, videoname);
Uri uri = FileProvider.getUriForFile(this, "com.example.provider", newFile);
Intent viewIntent = new Intent(Intent.ACTION_VIEW, uri);
viewIntent.setType(getContentResolver().getType(uri));
viewIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(viewIntent);
}

video share by intent only play in my own app

I am working on video player when i share video and open that link its play on my default player not in my app. how to open that video on my app.
Here is my code(in fragment class):
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT,
"Hey check out my app at: "+share_video);
sendIntent.setType("text/plain");
startActivity(sendIntent);
Try this:- You may use the createChooser for your intent, which will displays all the app those are able to handle your intent.
For Example:-
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT,
"Hey check out my app at: "+share_video);
sendIntent.setType("text/plain");
Intent chooser = Intent.createChooser(sendIntent);
// Verify the intent will resolve to at least one activity
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(chooser);
}
Use video view:
for streaming a link u can do something like:
String LINK = "type_here_the_link";
VideoView mVideoView = (VideoView) findViewById(R.id.videoview);
MediaController mc = new MediaController(this);
mc.setAnchorView(videoView);
mc.setMediaPlayer(videoView);
Uri video = Uri.parse(LINK);
mVideoView.setMediaController(mc);
mVideoView.setVideoURI(video);
mVideoView.start();
>or for local video u can do something like:
VideoView videoView =(VideoView)findViewById(R.id.videoView1);
//Creating MediaController
MediaController mediaController= new MediaController(this);
mediaController.setAnchorView(videoView);
//specify the location of media file
Uri uri=Uri.parse(Environment.getExternalStorageDirectory().getPath()+"/media/1.mp4");
//Setting MediaController and URI, then starting the videoView
videoView.setMediaController(mediaController);
videoView.setVideoURI(uri);
videoView.requestFocus();
videoView.start();
> U can visit the link for more understanding:
> https://www.javatpoint.com/playing-video-in-android-example

"Cant play video" error in VideoView after reading the uri from database

In my app, I am selecting videos using Intent as follows:
Intent intent = new Intent();
intent.setType("video/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, SELECT_VIDEO);
Then the uri of the video is saved in the database in String format
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK && requestCode == SELECT_VIDEO) {
Uri selectedVideoUri = data.getData();
if (selectedVideoUri != null) {
videoPathList.add(selectedVideoUri.toString());
I read the string from the database, convert it to uri using Uri.parse() method and use it to play the video. However I get "Cant play video" error.
VideoView videoView =(VideoView)findViewById(R.id.videoView);
//Creating MediaController
MediaController mediaController= new MediaController(this);
mediaController.setAnchorView(videoView);
Uri uri= Uri.parse(getIntent().getStringExtra("video_path"));
//Setting MediaController and URI, then starting the videoView
videoView.setMediaController(mediaController);
videoView.setVideoURI(uri);
videoView.requestFocus();
videoView.start();
The video plays fine if I play using the uri obtained in onActivityResult(). The problem occurs only after saving the uri to the database in string format. How do I solve this?
Then the uri of the video is saved in the database in String format
The Uri may not be usable later on.
How do I solve this?
On Android 4.4+, switch to ACTION_OPEN_DOCUMENT instead of ACTION_GET_CONTENT. Then, when you get your Uri, call takePersistableUriPermissions() on a ContentResolver. This should give you durable access to the content identified by the Uri, and you should be able to play back the video later... assuming that the video is still there, of course.
See this blog post for more on this subject.

Video Play through intent from assets folder in android

i WAnna play a video that is in my assets folder. that can be played in any player installed in android.
Help OuT
Take a video view and media player in layout and in your activity onCreate you can do this
private VideoView video;
private MediaController ctlr;
uri=Uri.parse("android.resource://packagename/" + R.raw.famous);
video=(VideoView)findViewById(R.id.video);
video.setVideoURI(uri);
ctlr=new MediaController(this);
ctlr.setMediaPlayer(video);
video.setMediaController(ctlr);
video.requestFocus();
video.start();
Here the video named famous is in raw folder in res. or you can keep in assets and change the file name based on that.
Code is tested and it works fine.
check this out :
Intent viewMediaIntent = new Intent();
viewMediaIntent.setAction(android.content.Intent.ACTION_VIEW);
Uri audio = Uri.parse("android.resource://com.audio.test/raw/"+R.raw.audio1);
Log.d(TAG,"uri:"+audio.toString());
viewMediaIntent.setDataAndType(audio, "video/*");
viewMediaIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
Log.d(TAG,"Starting");
Intent i = Intent.createChooser(viewMediaIntent, "Play Music");
mGap.startActivity(i);
Log.d(TAG,"Started");

how to play video from raw folder in android?

Hi All I want to play video in android from raw folder in native player .I able to play video from sd card and server url. But if my mp4 is in raw folder its fire exception. Can somebody help me.
My code is here.
Uri uri = Uri.parse("android.resource://" + getPackageName() + R.raw.sun);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);intent.setDataAndType(uri, "video/*");
startActivity(intent);
its working if i play using Video View but i don't want play in Video View.
Uri uri = Uri.parse("android.resource://" + getPackageName() + "/"+R.raw.sun);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);intent.setDataAndType(uri, "video/*");
startActivity(intent);
what you missed is simply the "/"
Hope this helps :)

Categories

Resources