Play mp4 video from url - android

I am using VideoView to play video from url. When I run app it gives error Can't play this video with below msg in logcat
Couldn't open file on client side; trying server side:java.io.FileNotFoundException: No content provider:aklearningsolutions.com/video/Nursery%20Rhymes.mp4
Unable to open content: aklearningsolutions.com/video/Nursery%20Rhymes.mp4
java.io.IOException: setDataSource failed.
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1095)
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1069)
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1023)
below is my activity file
package com.dp.videostore.Activity;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.TextureView;
import android.widget.MediaController;
import android.widget.VideoView;
import com.dp.videostore.R;
public class PlayerActivity extends AppCompatActivity {
VideoView videoView;
String url = "aklearningsolutions.com/video/Nursery Rhymes.mp4";
MediaController mc;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player);
videoView = (VideoView)findViewById(R.id.video_view);
try {
mc = new MediaController(this);
mc.setAnchorView(videoView);
mc.setMediaPlayer(videoView);
videoView.setMediaController(mc);
Uri link = Uri.parse(url.replace(" ","%20"));
videoView.setVideoURI(link);
videoView.requestFocus();
videoView.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Below is my xml file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.dp.videostore.Activity.PlayerActivity">
<VideoView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/video_view"/>
</RelativeLayout>
When i hit video link in web browser, video play very well in browser

Try putting the protocol into your URL. As it is, Android thinks you are using a ContentProvider
String url = "http://aklearningsolutions.com/video/Nursery Rhymes.mp4";

Related

Play multiple videos at the same time using TextureView in Android programmatically

I would like to crop and play multiple videos in one activity using texture view in android programmatically. The code below throws an exception " Caused by: java.lang.IllegalArgumentException: surfaceTexture must not be null".
Mainactivity.java class
import androidx.appcompat.app.AppCompatActivity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Surface;
import android.view.TextureView;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
private TextureView videoView1;
private TextureView videoView2;
private MediaPlayer mediaPlayer1;
private MediaPlayer mediaPlayer2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
videoView1 = findViewById(R.id.video_view_1);
videoView2 = findViewById(R.id.video_view_2);
String path = "android.resource://" + getPackageName() + "/" + R.raw.patila;
mediaPlayer1 = new MediaPlayer();
try {
mediaPlayer1.setDataSource(path);
mediaPlayer1.setSurface(new Surface(videoView1.getSurfaceTexture()));
mediaPlayer1.setOnPreparedListener(mp -> mp.start());
mediaPlayer1.prepareAsync();
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer2 = new MediaPlayer();
try {
mediaPlayer2.setDataSource(path);
mediaPlayer2.setSurface(new Surface(videoView2.getSurfaceTexture()));
mediaPlayer2.setOnPreparedListener(mp -> mp.start());
mediaPlayer2.prepareAsync();
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
protected void onDestroy() {
super.onDestroy();
mediaPlayer1.release();
mediaPlayer2.release();
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<TextureView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="#+id/video_view_1"/>
<TextureView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="#+id/video_view_2"/>
</LinearLayout>
I have done multiple research about the same and how can achieve multiple video cropping and playing on one activity.
I'm really confused on the best methodology to achieve the same because I'm new to TextureView. Kindly assist me on how to go about it.

VideoView android is not working on android version 4+

Anyone help me. I am preparing application to integrate video into it. Kept video in res/raw folder. Video format .mp4. Working api-level-21. It is working on android devices for android version 2.2 but not on 4+.
Here is my code:
java:
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.MediaController;
import android.widget.VideoView;
public class Gift extends ActionBarActivity {
MediaController mediaController;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.gift);
VideoView myVideoView = (VideoView) findViewById(R.id.videoView);
Uri uri= Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.sidbday);
mediaController = new MediaController(this);
myVideoView.setMediaController(mediaController);
myVideoView.setVideoURI(uri);
myVideoView.start();
}
}
xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<VideoView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/videoView"/>
</RelativeLayout>
This message is shown when framework is not ready to play video yet. You can start your video after onPrepared method is called:
instead of myVideoView.start(); use
video.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
myVideoView.start();
}
});
EDIT
I didn't notice that it's an old topic but maybe it hepls someone.

RTSP Url of video not working in VideoView

I am creating video playing app from Youtube. I have extracted Video Data using gdata API and got 3gp url for format 1,6. This i got extracted from Media:Group --> Media:content element.
My Device Info Android 4.0.3 and Model Micromax P350.
These video is working in VLC Player but not in VideoView enter code here in my app. Here is my code:
<LinearLayout
android:id="#+id/LinearLayout01"
android:layout_height="fill_parent"
android:paddingLeft="2px"
android:paddingRight="2px"
xmlns:android="http://schemas.android.com/apk/res/android"
android:paddi`enter code here`ngTop="2px"
android:paddingBottom="2px"
android:layout_width="fill_parent"
android:orientation="vertical">
<VideoView
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:id="#+id/VideoView" />
</LinearLayout>
MainActivity.java
package com.example.firstapp;
import android.app.Activity;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
VideoView videoView = (VideoView)findViewById(R.id.VideoView);
//MediaController mediaController = new MediaController(this);
// mediaController.setAnchorView(videoView);
//videoView.setMediaController(mediaController);
videoView.setVideoPath("rtsp://v1.cache8.c.youtube.com/CiILENy73wIaGQneb1Sj_PGnoRMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp");
videoView.start();
}
}
Must be your video is not in one of the supported formats:
http://developer.android.com/guide/appendix/media-formats.html
//play rtsp stream
private void PlayRtspStream(String rtspUrl){
videoView.setVideoURI(Uri.parse(rtspUrl));
videoView.requestFocus();
videoView.start();
}

How to play video which is at some URL location in android's video view?

I am trying to play youtube video within video view but getting error(can't play this video) all the time(on device as well as on emulator too). Please help me with this problem.
Thanks in advance.
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.widget.VideoView;
public class AndroidVideoView extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
VideoView myVideoView = (VideoView) findViewById(R.id.videoview);
String viewSource = "rtsp://v2.cache8.c.youtube.com/CjYLENy73wIaLQndUnGVjs340xMYDSANFEIJbXYtZ29vZ2xlSARSBXdhdGNoYJP8mrqSz625UAw=/0/0/0/video.3gp";
myVideoView.setVideoURI(Uri.parse(viewSource));
myVideoView.requestFocus();
myVideoView.start();
}
}
1) Videos will not buffer on emulator because it is virtual device. In-order stream video you should use real device
2) you must provide this permission because with out internet connection we will not able to connect with YouTube server
<uses-permission android:name="android.permission.INTERNET" />
3)Please validate your RTSP link. Given below is my rtsp link for my YouTube video
"rtsp://v1.cache6.c.youtube.com/CiILENy73wIaGQnCikhfRzTDsBMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp";
To generate rtsp link Use this
http://gdata.youtube.com/feeds/mobile/videos/"+Video Id+"
In my case Video Id is sMM0R19IisI
My Code
public class MainActivity extends Activity {
String SrcPath = "rtsp://v1.cache6.c.youtube.com/CiILENy73wIaGQnCikhfRzTDsBMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp";//"rtsp://v5.cache1.c.youtube.com/CjYLENy73wIaLQnhycnrJQ8qmRMYESARFEIJbXYtZ29vZ2xlSARSBXdhdGNoYPj_hYjnq6uUTQw=/0/0/0/video.3gp";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
VideoView myVideoView = (VideoView)findViewById(R.id.myvideoview);
myVideoView.setVideoURI(Uri.parse(SrcPath));
myVideoView.setMediaController(new MediaController(this));
myVideoView.requestFocus();
myVideoView.start();
}
}
LAYOUT XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<VideoView
android:id="#+id/myvideoview"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>

how to write a simple video player on android?

I have code for simple audio player on android. But it is not working for videos. Please guide me to write a simple video player.
The code of audio player is
package com.example.helloplayer;
public class HelloPlayer extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MediaPlayer mp = MediaPlayer.create(this, R.raw.file);
mp.start();
}
}
For making a video player you will have to make use of video view. a sample is shown below
Layout file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<VideoView android:id="#+id/videoView"
android:layout_height="fill_parent"
android:layout_width="fill_parent"/>
</LinearLayout>
Here i am playing a video stored in "resource/raw" folder , "one" is the name of video file,you can replace it with name of your video file .also make sure that you are going to play an android supported video format
VideoplayerActivitvity
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;
public class VideoplayerActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
VideoView videoView =(VideoView)findViewById(R.id.videoView);
MediaController mediaController= new MediaController(this);
mediaController.setAnchorView(videoView);
Uri uri=Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.one);
videoView.setMediaController(mediaController);
videoView.setVideoURI(uri);
videoView.requestFocus();
videoView.start();
}
}

Categories

Resources