getting error while playing a video file with VideoView and MediaController - android

I am trying to implement a video player with the help of VideoView and MediaController. I have tried reading a mp4 file that I have recorded with my camera which is stored on my sdcard and also a file from a remote server. But everytime I am getting a dialog stating that "Can't play this video". These are the source codes.
MainActivity.java
#SuppressLint("ValidFragment")
public class VideoPlayFragment extends Fragment
{
private VideoView mVideoView;
private ImageView mPlayButtonImageView,mThumbnailImageView;
private MediaController mMediaController;
Context mCntx;
String mUrl;
public VideoPlayFragment(Context context, String mPhotoURLString)
{
// TODO Auto-generated constructor stub
this.mCntx=context;
this.mUrl=mPhotoURLString;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view=inflater.inflate(R.layout.video_play_layout, null);
initUI(view);
return view;
}
private void initUI(View view)
{
// TODO Auto-generated method stub
mVideoView=(VideoView) view.findViewById(R.id.video_play_layout_video_view);
mPlayButtonImageView=(ImageView) view.findViewById(R.id.video_play_layout_play_button);
mThumbnailImageView=(ImageView) view.findViewById(R.id.video_play_layout_thumnail);
Bitmap bmThumbnail;
bmThumbnail = ThumbnailUtils.createVideoThumbnail(mUrl, Thumbnails.FULL_SCREEN_KIND);
mThumbnailImageView.setImageBitmap(bmThumbnail);
mPlayButtonImageView.setOnClickListener(new OnClickListener()
{
#Override public void onClick(View v)
{
// TODO Auto-generated method stub
mPlayButtonImageView.setVisibility(View.GONE);
mThumbnailImageView.setVisibility(View.GONE);
mVideoView.setVisibility(View.VISIBLE);
try {
mMediaController = new MediaController(mCntx);
mMediaController.setAnchorView(mVideoView);
Uri video = Uri.parse(mUrl);
mVideoView.setMediaController(mMediaController);
mVideoView.setVideoURI(video);
mVideoView.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
mVideoView.start();
}
});
}catch(Exception e){
System.out.println("Video Play Error :"+e.getMessage());
}
}
});
}
}
Layout code--
<?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"
android:orientation="vertical"
>
<VideoView
android:id="#+id/video_play_layout_video_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:layout_marginBottom="25dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:visibility="gone"/>
<ImageView
android:id="#+id/video_play_layout_thumnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:contentDescription="#string/app_name"
android:layout_marginBottom="25dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
<ImageView
android:id="#+id/video_play_layout_play_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:contentDescription="#string/app_name"
android:src="#drawable/playbttn" />
</RelativeLayout>
Log--
10-11 16:16:24.108: E/MediaPlayer(7429): Error (1,-2147483648)
10-11 16:16:24.108: D/VideoView(7429): Error: 1,-2147483648
Please can some one suggest any other way of doing this as I am finding the same examples similar to code above in most of the tutorials.

You are going to so complicated to play a video. Please see the below link. It will be easy for you to play a video
How to play video file using VideoView in Android
Also check the below link also
Android video player example

Related

VideoPlayer widget keeps on giving NullPointerException of invoking a virtual method on a null object reference

Right now I am trying to play the video in the start of my App as welcome greetings I followed this Video player link before adding the controller. I wrote the following code and it ran successfully in a separate Activity.
public class MainActivity extends AppCompatActivity {
private static final String VIDEO_SAMPLE = "tacoma_narrows";
private VideoView mVideoView;
#Override
protected void onStart() {
super.onStart();
initializePlayer();
}
#Override
protected void onStop() {
super.onStop();
releasePlayer();
}
#Override
protected void onPause() {
super.onPause();
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
mVideoView.pause();
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mVideoView = findViewById(R.id.startingvideo);
}
private Uri getMedia(String mediaName) {
return Uri.parse("android.resource://" + getPackageName() +
"/raw/" + mediaName);
}
private void initializePlayer() {
Uri videoUri = getMedia(VIDEO_SAMPLE);
mVideoView.setVideoURI(videoUri);
mVideoView.start();
}
private void releasePlayer() {
mVideoView.stopPlayback();
}}
but when I tried to add the same code in my welcome screen then I got the following message
java.lang.RuntimeException: Unable to start activity
ComponentInfo{sterlingtechsolutionpk.buttonbutton/sterlingtechsolutionpk.buttonbutton.Welcome_Screen}:
java.lang.NullPointerException: Attempt to invoke virtual method 'void
android.widget.VideoView.setVideoURI(android.net.Uri)' on a null
object reference
The .xml file is
<android.support.constraint.ConstraintLayout 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:id="#+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/loading_screen"
android:minHeight="177dp"
tools:context="sterlingtechsolutionpk.buttonbutton.Welcome_Screen">
<!-- The primary full-screen view. This can be replaced with whatever view
is needed to present your content, e.g. VideoView, SurfaceView,
TextureView, etc. -->
<!-- This FrameLayout insets its children based on system windows using
android:fitsSystemWindows. -->
<ProgressBar
android:id="#+id/start_progres"
style="#android:style/Widget.ProgressBar.Horizontal"
android:layout_width="240dp"
android:layout_height="30dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="362dp"
android:indeterminate="false"
android:progressBackgroundTint="#fff200"
android:progressTint="#005cef"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.455" />
<VideoView
android:id="#+id/startingvideo"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_margin="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="4:3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
Any Help regarding this is appreciated.
Maybe you are using the wrong id?
Code:
mVideoView = findViewById(R.id.videoview);
Layout:
android:id="#+id/startingvideo"
You should change one of these lines so that they are matching.
You are using wrong ids for your mVideoView. It returns a NullPointerException because mVideoView is null, like you give wrong id.
change it to :
mVideoView = (VideoView) findViewById(R.id.startingvideo);
try calling initializePlayer() from onCreate() instead of onStart();
check if videoUri is not null. Your getMedia method is maybe returning a null value.

Playing new videos with Youtube Api on Android

I'm developing an app with Android Studio for which I need the Youtube API. The problem is that I want the video to be played
every time the user clicks on "Reproducir"("play"). So I have researched how to load a video and everybody says to use the function
initializates that will call onInitializationSuccess and playes there. But if I want to load new videos, parsing the url and playing the new videos I can't. I tried calling the initialization everytime the user clicks on Reproducir but in that case It ig.
public class AddVideoActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener{
private YouTubePlayerView youTubeView;
private EditText urlTxtView;
private Button playButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_video);
youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_player_add);
urlTxtView = (EditText) findViewById(R.id.urlYoutube);
playButton = (Button) findViewById(R.id.buttonPlay);
playButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
youTubeView.initialize(Config.YOUTUBE_API_KEY, (YouTubePlayer.OnInitializedListener) view.getContext());
}
});
}
#Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
String url = urlTxtView.getText().toString();
Log.d("1",url );
youTubePlayer.loadVideo("a4NT5iBFuZs");
}
#Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult result) {
Toast.makeText(this, "ERROR:"+result.toString(), Toast.LENGTH_LONG).show();
}
XML Layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/action_add_video"
android:textStyle="normal|bold"
android:textSize="30sp"
android:gravity="center_horizontal"
android:layout_marginBottom="20dp" />
<com.google.android.youtube.player.YouTubePlayerView
android:id="#+id/youtube_player_add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"/>
<EditText
android:id="#+id/tituloVideo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Titulo"
android:inputType="textPersonName" />
<EditText
android:id="#+id/urlYoutube"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="URL o buscar con #youtube"
android:inputType="textPersonName" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/buttonCancelar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Cancelar" />
<Button
android:id="#+id/buttonPlay"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Reproducir" />
<Button
android:id="#+id/buttonGuardar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Guardar" />
</LinearLayout>
</LinearLayout>
Log:
W/YouTubeAndroidPlayerAPI: YouTube video playback stopped due to unauthorized overlay on top of player. The YouTubePlayerView is obscured by android.view.View{a21cb6f V.ED.... ........ 0,0-768,50 #102002f android:id/statusBarBackground}. Bottom edge 36 px below YouTubePlayerView's top edge. .
Here there is an image of the idea:
Picture of Android emulator, the user should introduce an url, clicks on Reproducir and plays the video from the url
If you are reinitilize the youtube video video view if already its been initialize thn first you need to release exising youtube view.
Please check below code
Specifically i've changed initialize part on Play button click
playButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (youTubePlayer != null) {
youTubePlayer.release();
}
youTubeView.initialize("AIzaSyDo9p_Qjy8XRBdFnVOaIx586MzHHLOUopw", (YouTubePlayer.OnInitializedListener) view.getContext());
}
});
Entire Activity code
public class AddVideoActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {
private YouTubePlayerView youTubeView;
private EditText urlTxtView;
private Button playButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_video);
youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_player_add);
urlTxtView = (EditText) findViewById(R.id.urlYoutube);
playButton = (Button) findViewById(R.id.buttonPlay);
playButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (youTubePlayer != null) {
youTubePlayer.release();
}
youTubeView.initialize(Config.YOUTUBE_API_KEY, (YouTubePlayer.OnInitializedListener) view.getContext());
}
});
}
private YouTubePlayer youTubePlayer;
#Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean wasRestored) {
this.youTubePlayer = youTubePlayer;
String url = urlTxtView.getText().toString();
Log.d("1", url);
if (!wasRestored) {/// here videoid you need to pass not entire video URL
youTubePlayer.loadVideo(url);
}
}
#Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult result) {
Toast.makeText(this, "ERROR:" + result.toString(), Toast.LENGTH_LONG).show();
}
}
to load video you need to pass video id not video url.So with assumption tht in edit text video id will be pass or if url thn you will get the video id from url and thn video will be loaded
this.youTubePlayer = youTubePlayer;
String url = urlTxtView.getText().toString();
Log.d("1", url);
if (!wasRestored) {/// here videoid you need to pass not entire video URL
youTubePlayer.loadVideo(url);
}
And for below log
Log: W/YouTubeAndroidPlayerAPI: YouTube video playback stopped due to
unauthorized overlay on top of player. The YouTubePlayerView is
obscured by android.view.View{a21cb6f V.ED.... ........ 0,0-768,50
102002f android:id/statusBarBackground}. Bottom edge 36 px below YouTubePlayerView's top edge.
you can refer answer https://stackoverflow.com/a/29676512/1140237
in your case for this log... issue is with Application or Activity theme. Give No ActionBarTheme to resolve that error

Android : Positionning the MediaController with setAnchorView

Since 24 hours I'm trying to set the Anchor view of my MediaControler without success.
My VideoView is embedded in a bigger application but even with a very simple testing app I don't understand the problem.
Here is the code of the onCreate of my testing activity :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final VideoView vv = (VideoView) findViewById(R.id.videoView1);
vv.setVideoURI(Uri.parse("http://192.168.30.188/test.mp4"));
final MediaController mediaController = new MediaController(ll.getContext());
vv.setMediaController(mediaController);
vv.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mediaController.setAnchorView(vv);
}
});
vv.requestFocus();
}
And here is my simple layout :
<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"
tools:context=".MainActivity" >
<FrameLayout
android:id="#+id/layout1"
android:layout_width="218dp"
android:layout_height="162dp"
android:background="#android:color/holo_purple">
<VideoView
android:id="#+id/videoView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
</RelativeLayout>
It seems that the media controller appears aligned with the bottom of my video view, but ever horitzontally centered !!!
I had a look at a lot of stack overflow "solutions" (more or less good) or other site samples and I can't understand ...
Can anybody help me please ?

Not displaying videoview in full screen

Everyone. I would like to videoview displays the video in full screen, I've tried using some commands, but they didn't work out. I need some help. I put below my code.
The video continues showing about 1/3 of screen! I'm a newbie!!!
Intro.JAVA
public class Intro extends Activity {
public void onCreate(Bundle b) {
super.onCreate(b);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.intro);
VideoView vid = (VideoView) findViewById(R.id.videoView1);
String uriPath = "android.resource://com.english/raw/videoenglish";
Uri uri = Uri.parse(uriPath);
vid.setVideoURI(uri);
vid.start();
vid.requestFocus();
vid.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
Intent main = new Intent(Intro.this, IntentsDemoActivity.class);
Intro.this.startActivity(main);
Intro.this.finish();
}
});
}
}
INTRO.XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<VideoView
android:id="#+id/videoView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_gravity="center" />
</LinearLayout>
Usually, the actual video will not fill the screen a lot of the time, because the aspect ratio of the video does not match the aspect ratio of the screen itself.
I suspect that the VideoView itself is filling up the whole screen. Try with a different device or the emulator.
Here are some other questions for reference either way:
Reference 1
Reference 2

Android: Cannot see video on HTC Hero 1.5

I am trying to play a video through my application.
public class VideoScreen extends Activity {
public MediaPlayer videoMediaPlayer = new MediaPlayer();
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.videoscreen);
SurfaceView demoVideo = (SurfaceView) findViewById(R.id.demoVideo);
SurfaceHolder videoHolder = demoVideo.getHolder();
videoMediaPlayer = MediaPlayer.create(this, R.raw.help);
videoMediaPlayer.setDisplay(videoHolder);
videoMediaPlayer.setLooping(false);
videoMediaPlayer.setScreenOnWhilePlaying(true);
videoMediaPlayer.start();
}
}
XML File:
<?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">
<SurfaceView android:id="#+id/demoVideo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></SurfaceView>
</FrameLayout>
I can hear only sound, but no video.
But, when I try to play this video separately through file manager. It is playing perfectly. Where I am wrong?
Please help.
I managed to play the video on my phone as well as simulator using VideoView
The changes done were:
Java File:
public class VideoScreen extends Activity {
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.videoscreen);
Uri raw_uri=Uri.parse("android.resource://com.binary/"+R.raw.myvideo);
VideoView videoView=(VideoView)findViewById(R.id.demoVideo);
videoView.setVideoURI(raw_uri);
videoView.setMediaController(new MediaController(this));
videoView.setOnCompletionListener(videoOverListener);
videoView.start();
videoView.requestFocus();
}
}
XML File:
<?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">
<VideoView android:id="#+id/demoVideo"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</VideoView>
</FrameLayout>
Hope this helps.

Categories

Resources