java.io.FileNotFoundException: No content provider: - java

i have problem to reproduce a video with videoView.setVideoPath();
in fact video doesn't reproduce..i don't know why. Video's path is correct.
This is my code:
public class MainActivity extends AppCompatActivity {
private VideoView videoView;
private int position = 0;
private MediaController mediaController;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
videoView = (VideoView) findViewById(R.id.videoView);
// Set the media controller buttons
if (mediaController == null) {
mediaController = new MediaController(MainActivity.this);
// Set the videoView that acts as the anchor for the MediaController.
mediaController.setAnchorView(videoView);
// Set MediaController for VideoView
videoView.setMediaController(mediaController);
}
try {
// ID of video file.
String videoUrl="https://www.youtube.com/watch?v=JHdmkP-nfsA";
videoView.setVideoPath(videoUrl);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
videoView.requestFocus();
// When the video file ready for playback.
videoView.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mediaPlayer) {
videoView.seekTo(position);
if (position == 0) {
videoView.start();
}
// When video Screen change size.
mediaPlayer.setOnVideoSizeChangedListener(new MediaPlayer.OnVideoSizeChangedListener() {
#Override
public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
// Re-Set the videoView that acts as the anchor for the MediaController
mediaController.setAnchorView(videoView);
}
});
}
});
}
}
Who can help me?
Thanks in advance everybody!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
LOGCAT:
Couldn't open https://www.youtube.com/watch?v=JHdmkP-nfsA: java.io.FileNotFoundException: No content provider: https://www.youtube.com/watch?v=JHdmkP-nfsA
10-29 12:27:28.419 25932-25932/com.example.marco.ud D/MediaPlayer: setDataSource IOException | SecurityException happend :
java.io.FileNotFoundException: No content provider: https://www.youtube.com/watch?v=JHdmkP-nfsA
at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1137)
at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:988)
at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:911)
at android.media.MediaPlayer.attemptDataSource(MediaPlayer.java:1102)
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1093)
at android.widget.VideoView.openVideo(VideoView.java:356)
at android.widget.VideoView.-wrap0(VideoView.java)
at android.widget.VideoView$7.surfaceCreated(VideoView.java:632)
at android.view.SurfaceView.updateWindow(SurfaceView.java:656)
at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:172)
at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:1013)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2510)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1519)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7113)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:927)
at android.view.Choreographer.doCallbacks(Choreographer.java:702)
at android.view.Choreographer.doFrame(Choreographer.java:638)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:913)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6780)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)

You can not play a YouTube video directly to the VideoView or ExoPlayer, for that, first you have to download the video then set the locale path of that video to the method VideoView.setVideoPath("path")
If you want to play only YouTube videos then use YouTube Android Player API, and if you only want to play other remote videos (.mp4, .ogg, .3gp etc.) then use the method VideoView.setVideoURI("uri").
Example-
String videoUrl = "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4";
try {
// Start the MediaController
MediaController mediacontroller = new MediaController(this);
mediacontroller.setAnchorView(videoview);
// Get the URL from String videoUrl
Uri video = Uri.parse(videoUrl);
videoview.setMediaController(mediacontroller);
videoview.setVideoURI(video);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
videoview.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
videoview.start();
}
});

I recommend using ExoPlayer instead of VideoView:
app gradle:
implementation 'com.google.android.exoplayer:exoplayer:2.10.8'
layout xml:
<com.google.android.exoplayer2.ui.PlayerView
android:id="#+id/video_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true" />
java code:
PlayerView videoView = findViewById(R.id.video_view);
SimpleExoPlayer player = ExoPlayerFactory.newSimpleInstance(this);
videoView.setPlayer(player);
// Produces DataSource instances through which media data is loaded.
DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(this, Util.getUserAgent(this, "yourApplicationName"));
// This is the MediaSource representing the media to be played.
MediaSource videoSource = new ProgressiveMediaSource.Factory(dataSourceFactory).createMediaSource(Uri.parse(fileEntity.getPath()));
// Prepare the player with the source.
player.prepare(videoSource);
player.setPlayWhenReady(true);

VideoView.setVideoPath requires a local path on the device. You should try setVideoURI instead if you want to play a remote MP4 or something else. VideoView Documentation
If you want to embed YouTube videos in your app, consider using the YouTube Android Player API or using something like a WebView

I had same issue but it was because of corporate proxy server. I used same app over my mobile internet and it started working. Hope it save someone's time

You are using VideoView which
Displays a video file. The VideoView class can load images from
various sources (such as resources or content providers).
You might want to try setVideoURI
Or : ExoPlayer if min SDK > 16
Or use SurfaceView :
SurfaceView surfaceView = (SurfaceView)findViewById(R.id.surfaceView);
SurfaceHolder surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(new SurfaceHolder.Callback() {
#Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
mediaPlayer.setDataSource(STREAM_URL);
mediaPlayer.setDisplay(surfaceHolder);
mediaPlayer.start();
}
Ref : https://developer.android.com/reference/android/widget/VideoView.html

Related

How to play a video from raw folder?

I want to play a video from raw folder. I tried a java code but I give an error on my Logcat and the app is stops on the phone.
This is my code:
public class Help extends AppCompatActivity {
VideoView videoView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_help);
videoView = findViewById(R.id.helpvideo);
MediaController mediaController = new MediaController(this);
videoView.setMediaController(mediaController);
Uri uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.video);
videoView.setVideoURI(uri);
videoView.start();
}
}
And this is the error:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.sinaMoradi.iran.ps, PID: 25296
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sinaMoradi.iran.ps/com.sinaMoradi.iran.ps.Help}: java.lang.ClassCastException: androidx.constraintlayout.widget.ConstraintLayout cannot be cast to android.widget.VideoView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3827)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4003)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2317)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:246)
at android.app.ActivityThread.main(ActivityThread.java:8595)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
Caused by: java.lang.ClassCastException: androidx.constraintlayout.widget.ConstraintLayout cannot be cast to android.widget.VideoView
at com.sinaMoradi.iran.ps.Help.onCreate(Help.java:24)
at android.app.Activity.performCreate(Activity.java:8207)
at android.app.Activity.performCreate(Activity.java:8191)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3800)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4003) 
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2317) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:246) 
at android.app.ActivityThread.main(ActivityThread.java:8595) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130) 
I'm a novice developer and I don't what are this errors.
Thanks for your help.
In res/layout/activity_help.xml, you have a widget named helpvideo. This is a ConstraintLayout. It is not a VideoView (or, perhaps, you have two widgets both named helpvideo). Ensure that the only helpvideo in the layout is the VideoView.
You can create your xml file like this to open video view
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center" >
<VideoView
android:id="#+id/video_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"/>
</FrameLayout>
Paste video file to your res/Raw folder of the project.
Source code for main activity:
public class AndroidVideoViewExample extends Activity {
private VideoView myVideoView;
private int position = 0;
private ProgressDialog progressDialog;
private MediaController mediaControls;
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// set the main layout of the activity
setContentView(R.layout.activity_main);
//set the media controller buttons
if (mediaControls == null) {
mediaControls = new MediaController(AndroidVideoViewExample.this);
}
//initialize the VideoView
myVideoView = (VideoView) findViewById(R.id.video_view);
// create a progress bar while the video file is loading
progressDialog = new ProgressDialog(AndroidVideoViewExample.this);
// set a title for the progress bar
progressDialog.setTitle("JavaCodeGeeks Android Video View Example");
// set a message for the progress bar
progressDialog.setMessage("Loading...");
//set the progress bar not cancelable on users' touch
progressDialog.setCancelable(false);
// show the progress bar
progressDialog.show();
try {
//set the media controller in the VideoView
myVideoView.setMediaController(mediaControls);
//set the uri of the video to be played
myVideoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.kitkat));
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
myVideoView.requestFocus();
//we also set an setOnPreparedListener in order to know when the video file is ready for playback
myVideoView.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mediaPlayer) {
// close the progress bar and play the video
progressDialog.dismiss();
//if we have a position on savedInstanceState, the video playback should start from here
myVideoView.seekTo(position);
if (position == 0) {
myVideoView.start();
} else {
//if we come from a resumed activity, video playback will be paused
myVideoView.pause();
}
}
});
}
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
//we use onSaveInstanceState in order to store the video playback position for orientation change
savedInstanceState.putInt("Position", myVideoView.getCurrentPosition());
myVideoView.pause();
}
#Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
//we use onRestoreInstanceState in order to play the video playback from the stored position
position = savedInstanceState.getInt("Position");
myVideoView.seekTo(position);
}
}
the part where the URI of the video file from raw folder is below:
myVideoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.kitkat));
You can use this code to play video from your raw folder. #HappyCoding

how to play a video using video view and intent

I want to play a video on my DetaiActivity which is coming through Intent from another page I have an Image and video image is showing properly but I dont know how to play video
this is my myAdapter:
foodViewHolder.mCardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(mContext,DetailActivity.class);
intent.putExtra("Image",myFoodList.get(foodViewHolder.getAdapterPosition()).getItemImage());
intent.putExtra("Video",myFoodList.get(foodViewHolder.getAdapterPosition()).getItemVideo());
intent.putExtra("Name",myFoodList.get(foodViewHolder.getAdapterPosition()).getItemName());
intent.putExtra("Ingrediants",myFoodList.get(foodViewHolder.getAdapterPosition()).getItemIngrediants());//
intent.putExtra("Procedure",myFoodList.get(foodViewHolder.getAdapterPosition()).getItemProcedure());
mContext.startActivity(intent);
}
});
and this my DetailActivity where i want to show video
I have tried something but it is not working
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
foodName = (TextView)findViewById(R.id.txtName);
foodImage = (ImageView)findViewById(R.id.ivImage2);
foodVideo = (VideoView)findViewById(R.id.ivVideo2);
foodIngrediants = (TextView)findViewById(R.id.txt_Ingrediants);//
foodProcedure = (TextView)findViewById(R.id.txt_Procedure);
Bundle mBundle = getIntent().getExtras();
if(mBundle!=null){
foodName.setText(mBundle.getString("Name"));
foodIngrediants.setText(mBundle.getString("Ingrediants"));//
foodProcedure.setText(mBundle.getString("Procedure"));
foodImage.setImageResource(mBundle.getInt("Image"));
String videoURL = mBundle.getString("Video");
// Start the MediaController
MediaController mediacontroller = new MediaController(DetailActivity.this);
mediacontroller.setAnchorView(foodVideo);
// Get the URL from String VideoURL
Uri video = Uri.parse(videoURL);
foodVideo.setMediaController(mediacontroller);
foodVideo.setVideoURI(video);
foodVideo.start();
Glide.with(this)
.load(mBundle.getString("Image"))
.into(foodImage);
}
}
}
Try using MediaPlayer. Something like:
Player = MediaPlayer.create(activity, uri);
Player.seekTo(0);
Player.setVolume(0.5f,0.5f);
Player.start();

Play Video Using External Media Player in Android

Well, I've used VideoView to play my Video. It only support .mp4 format(Correct me if I'm wrong), I need to play .flv video. I've MX Player/VLC player installed on my Android phone. How do I load the list of available Media Players when I click Play Video Button in my Activity.
Below is the code I've written using Video View, if it helps
public void onClick(View v) {
File root = Environment.getExternalStorageDirectory();
String externalFilesDir = getExternalFilesDir(null).toString();
String videoResource = externalFilesDir +"/" + "VID_20160115_215637181.mp4";
mediaController.setAnchorView(videoView);
videoView.setMediaController(mediaController);
videoView.setVideoPath(videoResource);
videoView.requestFocus();
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
videoView.seekTo(0);
videoView.start();
}
});
}
If You want to play video in some other player, you can use ACTION_VIEW intent
Example in this question: Android intent for playing video?

Can't play this video. Android videoView mp4 recorded by android device

I've looked for existing potential solutions with other formats and those still responds with mentioned error.
Finally, recorded the video with the same device and used it as a resource for this app and it still doesn't work.
Devices: SGS2, lenovo a820
Video type: MPEG-4 video (video/mp4)
videoView = (VideoView)findViewById(R.id.videoView);
videoView.setVideoPath("android.resource://raw/sample.mp4");
videoView.start();
Please refer below code snippet...problem was with the path declaration..
String uriPath = "android.resource://"+getPackageName()+"/"+R.raw.aha_hands_only_cpr_english;
Uri uri = Uri.parse(uriPath);
mVideoView.setVideoURI(uri);
Thats it...
I tried everything mentioned before but it turns out that internet permission is needed to play a mp4 file.
<uses-permission android:name="android.permission.INTERNET" />
try following code..
videoView = (VideoView)this.findViewById(R.id.videoView);
String path = "android.resource://" + getPackageName() + "/" + R.raw.video_file;
MediaController mc = new MediaController(this);
videoView.setMediaController(mc);
videoView.start();
public class videoplayer extends Activity {
private static final String Videos_URL = "*your URI*";
private VideoView myVideoView;
private int position = 0;
private ProgressDialog progressDialog;
private MediaController mediaControls;
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the layout from video_main.xml
setContentView(R.layout.activity_main);
if (mediaControls == null) {
mediaControls = new MediaController(this);
}
// Find your VideoView in your video_main.xml layout
myVideoView = (VideoView) findViewById(R.id.videoView);
// Create a progressbar
progressDialog = new ProgressDialog(this);
// Set progressbar title
progressDialog.setTitle("ABCDEFGH");
// Set progressbar message
progressDialog.setMessage("Loading...");
progressDialog.setCancelable(false);
// Show progressbar
progressDialog.show();
try {
Uri video = Uri.parse(Videos_URL);
myVideoView.setVideoURI(video);
myVideoView.setMediaController(mediaControls);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
myVideoView.requestFocus();
myVideoView.setOnPreparedListener(new OnPreparedListener() {
// Close the progress bar and play the video
public void onPrepared(MediaPlayer mp) {
progressDialog.dismiss();
myVideoView.seekTo(position);
if (position == 0) {
myVideoView.start();
} else {
myVideoView.pause();
}
}
});
}
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putInt("Position", myVideoView.getCurrentPosition());
myVideoView.pause();
}
#Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
position = savedInstanceState.getInt("Position");
myVideoView.seekTo(position);
}
}
Be sure that the decoder (target sdk) supports the video format you are using. You can use VLC Player to convert video format to the desired one. In my case, I converted the MP4 to WebM file and load it in the VideoView.
Here is how you get the file path and play your video.
String path = "android.resource://" + getPackageName() + "/" + R.raw.sample;
VideoView videoView = (VideoView)findViewById(R.id.videoView);
videoView.setVideoURI(Uri.parse(path));
videoView.start()
Source:
Video format and codec support
https://developer.android.com/guide/topics/media/media-formats.html
For Lenovo a820 ,below is need :
- MP4/WMV/H.264/H.263 player
- MP3/WAV/WMA/eAAC+ player
make sure that ur video fits in above codec format.
Just Replace your code with this code and it will be working:
VideoView videoView = findViewById(R.id.videoView);
videoView.setVideoPath("android.resource://" + getPackageName() + "/" + R.raw.sample);
videoView.start();
I think the issue is with the device on which we try to play video, sometimes it doesn't support.
I tried running it on Linux emulatoe
Try this following code it works..........
VideoView videoView=(VideoView)findViewById(R.id.videoView);
videoView.setVideoPath("android.resource://"+getPackageName()+"/"+R.raw.videoname;
videoView.start();

Android video record error

Can anybody point out the error I am doing? I am trying to record a short video, but somehow it finds a way to crash.
Source Code:
public class Start_recording extends Activity implements SurfaceHolder.Callback
{
MediaRecorder recorder;
SurfaceHolder surfaceHolder;
SurfaceView myVideoView;
Camera camera;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
camera = Camera.open();
camera.unlock();
recorder = new MediaRecorder();
setContentView(R.layout.activity_main_rst);
myVideoView = (SurfaceView)findViewById(R.id.surface_camera);
surfaceHolder = myVideoView.getHolder();
surfaceHolder.addCallback(this);
initMediaRecorder();
boolean exists = (new File(android.os.Environment.getExternalStorageDirectory() + "/Record/")).exists();
if (!exists)
{
new File(android.os.Environment.getExternalStorageDirectory() + "/Record/").mkdirs();
}
try
{
recorder.prepare(); // This is the line of error.
recorder.start();
Thread.sleep(36000);
recorder.stop();
setupActionBar();
}
catch(Exception e)
{
Log.v("Error is there : ",e.toString());
e.printStackTrace();
}
}
public void initMediaRecorder()
{
try
{
recorder.setPreviewDisplay(surfaceHolder.getSurface());
recorder.setCamera(camera);
recorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(android.os.Environment.getExternalStorageDirectory()+"/Record/test.3gp");
recorder.setMaxDuration(300000);
}
catch(Exception f)
{
Log.v("Exception here : ",f.toString());
f.printStackTrace();
}
}
}
Error :
E/MediaRecorderJNI(1575): Application lost the surface
V/Error is there :(1575): java.io.IOException: invalid preview surface
W/System.err(1575): java.io.IOException: invalid preview surface
W/System.err(1575): at android.media.MediaRecorder._prepare(Native Method)
W/System.err(1575): at android.media.MediaRecorder.prepare(MediaRecorder.java:666)
W/System.err(1575): at com.example.project.Start_recording.onCreate(Start_recording.java:51)
I have added the permissions correctly in the Manifest file and also set the setPreviewDisplay(). But I get as invalid preview surface.Please correct me.
One has to use SurfaceHolder.Callback to start recording only after the surface really is created.

Categories

Resources