Open video from gallery and Intent VideoView to new activity - android

I want make video player app that launch from gallery and play the video to 'new activity'. the problem is, i already browse the video at the gallery. the video are playing at the 'main activity' not play at the 'new activity'. please help.
MainActivity :
public class MainActivity extends Activity {
Button button1;
VideoView videoView;
private static final int SELECT_VIDEO = 1;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.button1);
videoView=(VideoView)findViewById(R.id.videoView);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent();
intent.setType("video/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Video"), SELECT_VIDEO);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != RESULT_OK) return;
if (requestCode == SELECT_VIDEO) {
Uri mVideoURI = data.getData();
videoView.setVideoURI(mVideoURI);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
videoView.setMediaController(mediaController);
videoView.start();
}
}
}
what coding should i put in my 'new activity.java'?
and i also want to put 4 video in same activity. what should i do?

put the intent in NewActivity not in MainActivity

Related

Video View Not Working with URI

When intent is fired for recording video on button click, it is recording that but in onActivityResult() method it is not playing the video.
Thanks in advance.
public class Main2Activity extends AppCompatActivity {
private Button button;
private VideoView videoView;
static final int REQUEST_VIDEO_RECORD = 100;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
button = (Button) findViewById(R.id.record_video_button);
videoView = (VideoView) findViewById(R.id.display_video_view);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dispatchVideoIntent();
}
});
}
private void dispatchVideoIntent() {
Intent videoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
if(videoIntent.resolveActivity(getPackageManager()) != null){
startActivityForResult(videoIntent, REQUEST_VIDEO_RECORD);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == REQUEST_VIDEO_RECORD && resultCode == RESULT_OK){
Uri videoUri = data.getData();
videoView.setVideoURI(videoUri);
videoView.start();
}
}
}

Android:-Video player

I made a video player app that will pick a video at runtime to play. After picking a video it gives an error of Can't play video.
public class MainActivity extends AppCompatActivity {
VideoView videoView;
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button= (Button) findViewById(R.id.button);
videoView= (VideoView) findViewById(R.id.videoView);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent galleryIntent=new Intent();
galleryIntent.setType("video/*");
galleryIntent.setAction(galleryIntent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(galleryIntent,"Select
Video"),9);
}
});}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent
data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==9 && resultCode==RESULT_OK)
{
Uri videoUri=data.getData();
videoView.setVideoURI(videoUri);
videoView.start();}}}
`
1) Try to add OnPreparedListener via calling this setOnPreparedListener, and start playing from onPrepared callback.
2) Replace VideoView and MediaPlayer with ExoPlayer. It is more stable than default player, also based on default android components, so it is lightweight.

OpenCV, Android, Android-Studio: Detecting Object from video URI

I'm pretty new to android app development, and I'm currently working on a school project that requires my application to capture a video using my android phone and then detect an object in this case the object is a AA battery, using openCV, in the captured video. I'd like to know how I can have OpenCV detect an object in a pre-captured video using the video's uri, I am only capable of detecting an object using openCV's javaCameraView, which is in real-time.
If its not possible for OpenCV to do detection using only the video's URI are there other possible methods? Please note i must use openCV as this is a school project
Here's my java code for the video capturing:
public class MainActivity extends AppCompatActivity {
private Button mRecordView, mPlayView;
private VideoView mVideoView;
private Intent callVideoAppIntent;
private int ACTIVITY_START_CAMERA_APP = 0;
public Uri videoUri;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecordView = (Button) findViewById(R.id.btnRecord);
mPlayView = (Button) findViewById(R.id.btnPlay);
mVideoView = (VideoView) findViewById(R.id.videoview);
//Record Button OnClickListener
mRecordView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
callVideoAppIntent = new Intent();
callVideoAppIntent.setAction(MediaStore.ACTION_VIDEO_CAPTURE);
//startActivityForResult(Intent, int RequestCode);
//application capture video using phone camera,
//then returns to application after stopping.
startActivityForResult(callVideoAppIntent, ACTIVITY_START_CAMERA_APP);
}
});
//Play Button OnClickListener
mPlayView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mVideoView.start();
mPlayView.setEnabled(false);
mRecordView.setEnabled(false);
}
});
mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mPlayView.setEnabled(true);
mRecordView.setEnabled(true);
}
});
}
//Once video captured and returned to application,
//we need to capture response,
//on activty result function.
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if(requestCode == ACTIVITY_START_CAMERA_APP && resultCode == RESULT_OK) {
videoUri = data.getData();
mVideoView.setVideoURI(videoUri);
}
}
}

Correct intent to launch gallery and get uri for selected video to play in VideoView

My UI has ImageButton which I am using to launch gallery from which I want to select a video to be played in videoview. I used code as follow but it doesn't play the video.
public class MainActivity extends Activity {
VideoView videoView;
private static final int PICK_FROM_GALLERY=1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final VideoView videoView =(VideoView)findViewById(R.id.videoView1);
MediaController mediaController= new MediaController(this);
mediaController.setAnchorView(videoView);
ImageButton btn1 = (ImageButton)findViewById(R.id.galleryPicker);
btn1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("video/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Complete action using"),PICK_FROM_GALLERY);
}
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode != RESULT_OK) return;
if (requestCode == PICK_FROM_GALLERY) {
Uri mVideoURI = data.getData();
videoView.setVideoURI(mVideoURI);
videoView.start(); //edited
}
}
});
}
Layout: Activity_main.xml:
<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" >
<VideoView
android:id="#+id/videoView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true" />
<ImageButton
android:id="#+id/galleryPicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="32dp"
android:layout_marginLeft="16dp"
android:adjustViewBounds="true"
android:contentDescription="#string/gallery_picker"
android:scaleType="center"
android:src="#drawable/gallery_picker" />
Any help?
Do in this way:
public class MainActivity extends Activity
{
.... //variables declarations here
VideoView videoView;
MediaController mc;
protected void onCreate(Bundle savedInstanceState)
{
........
......
videoView = (VideoView)findViewById(R.id.videoView1);
mc = new MediaController(this);
mc.setAnchorView(videoView);
ImageButton btn1 = (ImageButton)findViewById(R.id.galleryPicker);
btn1.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent intent = new Intent();
intent.setType("video/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Complete action using"),PICK_FROM_GALLERY);
}
} );
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode != RESULT_OK) return;
if (requestCode == PICK_FROM_GALLERY)
{
Uri mVideoURI = data.getData();
videoView.setMediaController(mc);
videoView.setVideoURI(mVideoURI);
videoview.requestFocus();
videoview.setOnPreparedListener(new OnPreparedListener() {
// Close the progress bar and play the video
public void onPrepared(MediaPlayer mp) {
videoview.start();
}
});
}
}

Display video in gallery in VideoView

My UI has a button and a videoview.When we click button,go to Gallery Video and when we select a video in Video gallery,it return my UI and videoview will display video that is selected..I used code as follow but it don't display video :(
public class VideoGalleryActivity extends Activity {
/** Called when the activity is first created. */
Button button;
VideoView videoView;
private static final int PICK_FROM_GALLERY=1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button=(Button)findViewById(R.id.button);
videoView=(VideoView)findViewById(R.id.videoview);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setType("video/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Complete action using"),PICK_FROM_GALLERY);
}
}) ;
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode != RESULT_OK) return;
if (requestCode == PICK_FROM_GALLERY) {
Uri mVideoURI = data.getData();
videoView.setVideoURI(mVideoURI);
}
}
You have to let videoview start by adding videoview.start();. Code:
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode != RESULT_OK) return;
if (requestCode == PICK_FROM_GALLERY) {
Uri mVideoURI = data.getData();
videoView.setVideoURI(mVideoURI);
videoview.start(); //edited
}
}

Categories

Resources