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)
If the file is being opened exclusively by some other process, opening it for either reading or writing will cause java.io.FileNotFoundException (Access is denied) exception. Fix: Make sure that the file is not opened by any other program or processes.
Try to create a file in a subfolder, for example, C:/somedir/somefile.txt instead of the root. If the file is being opened exclusively by some other process, opening it for either reading or writing will cause java.io.FileNotFoundException (Access is denied) exception. Make sure that the file is not opened by any other program or process.
The following constructors throw a FileNotFoundException when the specified filename does not exist: FileInputStream, FileOutputStream, and RandomAccessFile. These classes aim to obtain input bytes from a file in a file system, while the former class supports both reading and writing to a random access file.
If the file does not exist, the application creates it. However, if the file cannot be created, is a directory, or the file already exists but its permissions are sufficient for changing its content, a FileNotFoundException is thrown. FileNotFoundExceptionExample_v2.java
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With