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);
}
}
}
Related
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.
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
I did not understand this problem, when i record a video through back camera then video view showing actually view which should showing in the video View but when i use front camera then its rotate to opposite. i am using one class for both then why i am getting this type behavior of video view?
video view code
public class VideoPreviewActivity extends Activity {
VideoView VideoPreview;
String videoURI ="VIDEO";
Button back;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.video_preview_screen);
VideoPreview = (VideoView) findViewById(R.id.video_preview);
back = (Button) findViewById(R.id.back);
Bundle bundle = getIntent().getExtras();
videoURI = bundle.getString("VIDEO");
PlayVideo();
back.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v) {
Intent i = new Intent(VideoPreviewActivity.this, PicRecordShareActivity.class);
startActivity(i);
}
});
}
private void PlayVideo()
{
try
{
getWindow().setFormat(PixelFormat.TRANSLUCENT);
MediaController mediaController = new MediaController(VideoPreviewActivity.this);
mediaController.setAnchorView(VideoPreview);
// Toast.makeText(getApplicationContext(), "Video:\t"+videoIndex, Toast.LENGTH_LONG).show();
Uri video = Uri.parse(videoURI);
VideoPreview.setMediaController(mediaController);
VideoPreview.setVideoURI(video);
VideoPreview.requestFocus();
VideoPreview.setOnPreparedListener(new MediaPlayer.OnPreparedListener()
{
public void onPrepared(MediaPlayer mp)
{
VideoPreview.start();
}
});
}
catch(Exception e)
{
System.out.println("Video Play Error :"+e.toString());
finish();
}
}
}
Thank You
New to Android coding here. I'm trying to have the user pick an audio file for the MediaPlayer to play using the "upload" button, and then the MediaPlayer should start playing the file once the user hits "play." Here is my code so far:
public class MainActivity extends Activity {
final int ACTIVITY_CHOOSE_FILE = 1;
File original;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) this.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent chooseFile;
Intent intent;
chooseFile = new Intent(Intent.ACTION_GET_CONTENT);
chooseFile.setType("audio/mpeg");
intent = Intent.createChooser(chooseFile, "Choose a file");
startActivityForResult(intent, ACTIVITY_CHOOSE_FILE);
}
});
Button playButton = (Button)findViewById(R.id.playbutton);
playButton.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(original.getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
}
});
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case ACTIVITY_CHOOSE_FILE: {
if (resultCode == RESULT_OK) {
Uri uri = data.getData();
original = new File(uri.getPath());
String tag = "TAG";
Log.d(tag, original.getAbsolutePath());
}
}
}
}
}
I can choose the audio file fine, but once I hit play nothing happens. What am I doing wrong?
After setting your mediaPlayer data source you have to call mediaPlayer.prepareAsync()
Media Playback in android
I am new to programming and upon referring google's dev site I came up with a simple media player that plays the file selected by the user. The app seems to been running fine when choosing the file to be played for the first time but crashes right after selecting a file for the second time. I have pasted the code below. Any help will be appreciated.
public class MainActivity extends Activity {
private static int Reqs =1;
private String a;
MediaPlayer md=new MediaPlayer();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button Start = (Button) findViewById(R.id.button);
final Button Stop = (Button) findViewById(R.id.button3);
final Button Pause = (Button) findViewById(R.id.button2);
final Button Select = (Button) findViewById(R.id.button4);
Pause.setEnabled(false);
Stop.setEnabled(false);
Start.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
md.start();
Toast.makeText(getApplicationContext(), "Playing", Toast.LENGTH_SHORT).show();
Pause.setEnabled(true);
Stop.setEnabled(true);
}
});
Stop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Pause.setEnabled(false);
Toast.makeText(getApplicationContext(), "Stopped", Toast.LENGTH_SHORT).show();
md.stop();
}
});
Pause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
md.pause();
Toast.makeText(getApplicationContext(), "Paused", Toast.LENGTH_SHORT).show();
}
});
Select.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("audio/mpeg");
startActivityForResult(Intent.createChooser(intent, "Choose"), Reqs);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode <= Reqs && resultCode ==-1) {
Uri videoUri = data.getData();
a = videoUri.toString();
md.setDataSource(a); //try-catch surrounding it
md.prepare(); //try-catch surrounding it
}
}
}
You are not calling reset() to reset the state of the MediaPlayer object.
As the Android's documentation states:
In order to reuse a MediaPlayer object that is in the Error state and recover from the error, reset() can be called to restore the object to its Idle state.
Check it at: https://developer.android.com/reference/android/media/MediaPlayer.html