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.
Related
I am just trying to make an simple android application which will play a video from YouTube which I provide as a link. Here is the code
package com.example.videoplayer;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.VideoView;
import org.videolan.libvlc.IVLCVout;
import org.videolan.libvlc.LibVLC;
import org.videolan.libvlc.Media;
import org.videolan.libvlc.MediaPlayer;
import java.util.ArrayList;
import java.util.logging.StreamHandler;
public class MainActivity extends AppCompatActivity {
private static String TAG = "MainActivity";
private VideoView vv;
private Button bt;
private Context mContext = this;
private LibVLC mLibVLC = null;
private MediaPlayer mMediaPlayer = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vv = findViewById(R.id.videoView);
bt = findViewById(R.id.button);
}
public void setUpMediaStream(View view) {
final ArrayList<String> args = new ArrayList<>();
args.add("-vvv");
mLibVLC = new LibVLC(mContext, args);
Log.d(TAG, "setUpMediaStream() creating media player");
mMediaPlayer = new MediaPlayer(mLibVLC);
Log.d(TAG, "setUpMediaStream() setting video view");
String yt = "https://www.youtube.com/watch?v=tXHoqsnRD-U";
Media media = new Media(mLibVLC, Uri.parse(yt));
mMediaPlayer.setMedia(media);
media.release();
mMediaPlayer.setAspectRatio(null);
mMediaPlayer.setScale(0);
Log.d(TAG, "setUpMediaStream() playing media");
mMediaPlayer.play();
}}
The problem is I am facing the following exception when I press the play button
java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/com.example.videoplayer-JGI3RJow0fmaB6c-w2WuVQ==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.videoplayer-JGI3RJow0fmaB6c-w2WuVQ==/lib/arm64, /system/lib64]]] couldn't find "libc++_shared.so"
Any response is helpful.
Here is my XML :
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<VideoView
android:id="#+id/videoView"
android:layout_width="370dp"
android:layout_height="390dp"
android:layout_marginTop="104dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.487"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="72dp"
android:onClick="setUpMediaStream"
android:text="#string/play"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"`enter code here`
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/videoView"
app:layout_constraintVertical_bias="0.153" /> </androidx.constraintlayout.widget.ConstraintLayout>
I was unable to replicate your exception, but...
I don't think you can just pass in a url of a web page, vlc is not a WebView - it needs to be a playable file, such as https://jell.yfish.us/media/jellyfish-3-mbps-hd-h264.mkv - and be sure to add the internet permission in the manifest:
<uses-permission android:name="android.permission.INTERNET" />
I also think you need to replace the VideoView in your layout with org.videolan.libvlc.util.VLCVideoLayout and pass it into mediaPlayer.attachViews() after you instantiate the MediaPlayer.
I was able to play the video with these steps.
** EDIT **
LibVLC also have a sample project, where in their app-level build.gradle file they specify jni directories:
android {
...
sourceSets {
main {
jni.srcDirs = [] // Prevent gradle from building native code with ndk; we have our own Makefile for it.
jniLibs.srcDir 'jni/libs' // Where generated .so files are placed
assets.srcDirs = ['src/main/assets', '../assets/']
}
}
}
https://code.videolan.org/videolan/libvlc-android-samples/-/tree/master/java_sample
I want my video activity to go fullscreen when the video starts, but it's not happening. Here are some screenshots and the code that runs this activity.:
public class InfoLentes extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.infolentes);
}
public void sendMessage(View view) {
setContentView(R.layout.fullscreen);
VideoView VideoView = new VideoView(this);
getWindow().setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
VideoView.setMediaController(new MediaController(this));
Uri video = Uri.parse("android.resource://" + getPackageName() + "/"
+ R.raw.eyeprotect);
VideoView.setVideoURI(video);
setContentView(VideoView);
VideoView.start();
}
}
And this is my fullscreem.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<VideoView
android:id="#+id/VideoViewfull"
android:layout_width="match_parent"
android:layout_height="match_parent">
</VideoView>
</FrameLayout>
And Screenshots of whats happening.:
What am I doing wrong?
EDIT:
So Using #Sheetal Kumar Maurya answer, I created another activity and used the android:theme="#style/AppTheme.NoActionBar" on the manifest, the result is better, but still not really fullscreen.:
Create a VideoPlayer activity with NoActionbar theme.
<activity
android:name=".VideoPlayerActivity"
android:theme="#style/AppTheme.NoActionBar" />
In onCreate section add following:
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
Now use this activity as a player for full screen and enjoy.
So I finally, after a long time of searching, found the perfect solution for my problem.
Using the answer here on this post from #Hamiseixas, I did the following.:
Edited Gradle, and synced:
compile 'com.github.rtoshiro.fullscreenvideoview:fullscreenvideoview:1.1.2'
repositories {
mavenCentral()
}
Instead of a videoview, I used the new compiled package on my fullscreen.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_alignParentRight="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_height="fill_parent">
<com.github.rtoshiro.view.video.FullscreenVideoLayout
android:id="#+id/videoview"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</com.github.rtoshiro.view.video.FullscreenVideoLayout>
</RelativeLayout>
And when I press the intent button to take me to the video file I want to play, this is the activity that playes it.:
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import com.github.rtoshiro.view.video.FullscreenVideoLayout;
import java.io.IOException;
public class VideoTest extends AppCompatActivity {
FullscreenVideoLayout videoLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fullscreen);
videoLayout = (FullscreenVideoLayout) findViewById(R.id.videoview);
videoLayout.setActivity(this);
Uri video = Uri.parse("android.resource://" + getPackageName() + "/"
+ R.raw.eyeprotect);
try {
videoLayout.setVideoURI(video);
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
}
It was that simple =D
It's all better documented here, a huge thanks to that post, and to the creator of FullscreenVideoView, Toshiro
I want to show my animated gif image in full size on the splash screen, but it's not working. I also used match parent. Whats the problem here? My xml file is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_splashscreen"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.foodie.splash.splashscreen">
<com.felipecsl.gifimageview.library.GifImageView
android:id="#+id/gifImageView"
android:scaleType="fitCenter"
android:layout_width="match_parent"
android:layout_marginRight="0dp"
android:layout_marginLeft="0dp"
android:layout_height="match_parent" />
</RelativeLayout>
And this is how it looks:
Java code:
package com.example.foodie.splash;
import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ProgressBar;
import com.felipecsl.gifimageview.library.GifImageView;
import org.apache.commons.io.IOUtils;
import java.io.IOException;
import java.io.InputStream;
public class splashscreen extends AppCompatActivity {
private GifImageView gifImageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splashscreen);
gifImageView = (GifImageView)findViewById(R.id.gifImageView);
//Set GIFImageView resource
try{
InputStream inputStream = getAssets().open("foodie.gif");
byte[] bytes = IOUtils.toByteArray(inputStream);
gifImageView.setBytes(bytes);
gifImageView.startAnimation();
}
catch (IOException ex)
{
}
//Wait for 3 seconds and start Activity Main
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
splashscreen.this.startActivity(new Intent(splashscreen.this,MainActivity.class));
splashscreen.this.finish();
}
},5000); // 3000 = 3second
}
}
you choose a wrong scale type, sett the scaleType as FitXY instead of fit center
but of course it lead to change the scales, find out if it has something like centerCrop scale.
<com.felipecsl.gifimageview.library.GifImageView
android:id="#+id/gifImageView"
android:scaleType="fitXY"
android:layout_width="match_parent"
android:layout_height="match_parent" />
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";
I have few cards in a RecyclerView. Each card holds a custom video view. On click of a videoview, video should start. However in my case it just shows blue borders and the video doesnt run. The code is given below,
custom_card_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_margin="5dip"
card_view:cardCornerRadius="4dp">
<com.test.components.CustomVideoView
android:id="#+id/videoView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
/>
<TextView
android:id="#+id/info_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="2dip"
/>
</android.support.v7.widget.CardView>
CustomVideoView:
import android.content.Context;
import android.net.Uri;
import android.util.AttributeSet;
import android.widget.MediaController;
import android.widget.VideoView;
import com.test.src.R;
/**
* Created by Psych on 12/20/14.
*/
public class CustomVideoView extends VideoView {
MediaController mMediaController = null;
private String mVideoUrl = null;
public CustomVideoView(Context context) {
super(context);
}
public CustomVideoView(Context context, AttributeSet attrs) {
super(context, attrs);
}
/**
* Initialize all the basic elements here.
*/
private void initialize() {
mMediaController = new MediaController(getContext());
}
/**
* Starts up the video.
* Throws a null pointer exception if the URL is not set
*/
public void startVideo() {
// Return as is if there is no URL
if (mVideoUrl == null) {
throw new NullPointerException(getContext().getString(R.string.video_null_pointer_message));
}
mMediaController.setAnchorView(this);
setMediaController(mMediaController);
start();
}
public void setmVideoUrl(String mVideoUrl) {
this.mVideoUrl = mVideoUrl;
Uri vidUri = Uri.parse(mVideoUrl);
setVideoURI(vidUri);
}
public String getmVideoUrl() {
return mVideoUrl;
}
}
MainActivity:
public class MainActivity extends ActionBarActivity {
RecyclerView.LayoutManager mLayoutManager = null;
RecyclerView mCardsRecyclerView = null;
RecyclerView.Adapter mAdapter = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initComponents();
// use this setting to improve performance if you know that changes
// in content do not change the layout size of the RecyclerView
mCardsRecyclerView.setHasFixedSize(true);
mCardsRecyclerView.setLayoutManager(mLayoutManager);
mCardsRecyclerView.setAdapter(mAdapter);
}
/**
* Initialize all ui components/widgets/elements
*/
private void initComponents() {
mCardsRecyclerView = (RecyclerView) findViewById(R.id.cardsListView);
mLayoutManager = new LinearLayoutManager(this);
mAdapter = new NewsFeedAdapter();
}
}
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
style="#style/AppTheme"
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=".MainActivity">
<android.support.v7.widget.RecyclerView
android:id="#+id/cardsListView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/alertButton" />
</RelativeLayout>
I want each card to display just a thumbnail initially. On click of a video, the related card should start the video (not all of them). VideoView should take the whole screen when a video starts. How can I achieve this?
Thanks!
I had the same problem. This answer from another stackOverflow question, solved it for me. That is by giving explicit value for height (or width or both) in VideoView. For example
<VideoView
android:id="#+id/mVideoView"
android:layout_width="match_parent"
android:layout_height="300dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"/>