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.
Related
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();
}
}
}
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);
}
}
}
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
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
}
}
I am creating my own class and extending activity to use VideoView class.
How can i extend my own class from VideoView class. here is my code.
public class VideoPlayer extends Activity {
private ProgressDialog mProgressdialog = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.videoview);
StartVideoPlayer();
}
private void StartVideoPlayer(){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
showProgressDialog();
VideoView videoView = (VideoView) findViewById(R.id.surface_view);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
Uri video=Uri.parse("rtsp://192.168.1.80/movie4.mp4");
videoView.setMediaController(mediaController);
videoView.setVideoURI(video);
videoView.start();
videoView.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
mProgressdialog.dismiss();
}
});
videoView.setOnErrorListener(new OnErrorListener() {
public boolean onError(MediaPlayer mp, int what, int extra) {
mProgressdialog.dismiss();
return false;
}
});
}
private void showProgressDialog()
{
/** Progress dialog displayed while loading video*/
mProgressdialog = ProgressDialog.show(VideoPlayer.this,
"", "Streaming...", true);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
}
Now i want to extend VideoPlayer class from VideoView how can i use that. Please help.
Thanks,
Sriram
You can simply create a class public class extends VideoView. But from my experience, VideoView is not meant to be extended, it has its own logic and states and most of it's fields are private, and it is not a good attempt to extend it.
(See a related question of mine regarding the same VideoView)