VideoView Add a multiple videos and play - java
Lets pretend that a 5 videos in sdcard or internal storage.
now I already get the file path, my problem is how can i insert a multiple videos in VIDEO VIEW ?
The next or previous button is not functioning
here is my code
public void getFile()
{
urls = getIntent().getStringArrayListExtra("url_videoAll");
keys = getIntent().getStringArrayListExtra("key_videoAll");
for(int i = 0; i<urls.size();i++) {
String FileName = URLUtil.guessFileName(urls.get(0), null, MimeTypeMap.getFileExtensionFromUrl(urls.get(0)));
String yourFilePath = getBaseContext().getFilesDir() + "/" + FileName;
fileList.add(new File(yourFilePath));
}
playallvideo(fileList);
}
in this CODE ARE WORKING IN SINGLE VIDEO how about if i have 5 videos ?
private void playallvideo(final List<File> file)
{
mediaController = new MediaController(this);
video.setMediaController(mediaController);
video.setVideoPath(file.get(0).toString());
video.start();
mediaController.setPrevNextListeners(new View.OnClickListener() {
public void onClick(View v) {
video.setMediaController(mediaController);
video.setVideoPath(file.get(1).toString());
video.requestFocus();
video.start();
// code for next
}
}, new View.OnClickListener() {
public void onClick(View v) {
video.setMediaController(mediaController);
video.setVideoPath(file.get(1).toString());
video.requestFocus();
video.start();
// code for previous
}
});
}
I need this because I will use MediaController and press the next video
You can use a counter and increment the value of it as soon as the first video has stopped playing. This can be achieved by using the MediaPlayer.OnCompletionListener.
Below worked for me:
private static int currentVideo = 0;
private List<String> fileData = new ArrayList<String>();
videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
if(!(currentPosition < fileData.size())) {
return;
}
Uri nextUri = Uri.parse(fileData.get(currentPosition++));
videoView.setVideoURI(nextUri);
videoView.start();
//to keep looping into the list, reset the counter to 0.
//in case you need to stop playing after the list is completed remove the code.
if(currentPosition == fileData.size()) {
currentPosition = 0;
}
}
});
Though the post is too old to answer, the beginner developers who end up here will get as much help as I did.
Related
I get an error when clicking the buttons repeatedly. How can I fix it?
I have a question and answer application. I take the questions from the arraylist. but when moving on to the next question my application gets an error. There is an animation when moving to the next question in my application, and during this animation, the buttons of the previous question get an error when clicked again while moving. My guess is that when clicked again, he wants to check whether the previous question is true or false, but he cannot find the previous question because the code goes to the next question. but I couldn't find how to fix it. my mistake is attached below logcat: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setBackgroundTintList(android.content.res.ColorStateList)' on a null object reference at com.haktansoft.tytayt.islamidevir.checkAns(islamidevir.java:427) at com.haktansoft.tytayt.islamidevir.access$000(islamidevir.java:41) at com.haktansoft.tytayt.islamidevir$1.onClick(islamidevir.java:264) Here is the line of errors I get in Logcat java:427 = correctOption.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor("#14E39A"))); java:41 = public class islamidevir extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener { java:264 = checkAns((Button) v); java code : for (int i = 0; i < 4; i++) { linearLayout1.getChildAt(i).setOnClickListener(new View.OnClickListener() { #Override public void onClick(View v) { checkAns((Button) v); } }); } Random randomizer = new Random(); sira = (randomizer.nextInt(list.size())); txtnumberIndicator.setText(position + 1 + "/" + list.size()); playAnim(txtQuestions, 0, list.get(sira).getQuestions()); Next_btn.setOnClickListener(new View.OnClickListener() { #Override public void onClick(View v) { Next_btn.setEnabled(false); Next_btn.setAlpha(0.7f); enableOptions(true); position++; sira = (randomizer.nextInt(list.size())); } }); navigationDrawer(); } private void checkAns(Button selectedOptions) { enableOptions(false); Next_btn.setEnabled(true); Next_btn.setAlpha(1); if (selectedOptions.getText().toString().equals(list.get(sira).getCorrectAnswer())) { //correct Answer score++; final MediaPlayer mp = MediaPlayer.create(this, R.raw.ding); selectedOptions.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor("#14E39A"))); mp.start(); } else { //wrong Answer final MediaPlayer mp = MediaPlayer.create(this, R.raw.wrong_buzzer); selectedOptions.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor("#FF2B55"))); Button correctOption = linearLayout1.findViewWithTag(list.get(sira).getCorrectAnswer()); correctOption.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor("#14E39A"))); mp.start(); } }
The problem is that when you click multiple times you are moving to the next context, so the actual which is becoming the previous button doesn't exist anymore. I think you can add a verification inside your method checkAns(p pParamater) something like this : checkAns(P pParameter) { if(pParameter.notNullOrEmpty() { then ... } } Or having a state : isMoving and check on this state.
Deletion an element of an `ArrayList` and setting the info to a layout in `MainActivity` by the command of a custom `Dialog`
I am new working with this customized Dialog in android. I may not be able to clarify my problem properly by writing, So it humble request to understand what i want from given code. In MainActivity i have Button to delete an element of an ArrayList and setting the info from the ArrayList to a Layout. Code from CustomDialog: public class PermissionToDelete extends Dialog implements View.OnClickListener { public Button btnYes, btnNo; public TextView txtPermToDelete; public PermissionToDelete(Context context){ super(context); } #Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_ACTION_BAR); setContentView(R.layout.permission_delete); initialize(); } public void initialize(){ btnYes = findViewById(R.id.btnYes); btnNo = findViewById(R.id.btnNo); txtPermToDelete = findViewById(R.id.txtPermToDelete); btnYes.setOnClickListener(this); btnNo.setOnClickListener(this); } #Override public void onClick(View v) { if (v.getId() == R.id.btnNo){ dismiss(); } else if(v.getId() == R.id.btnYes){ MainActivity.deleteOrErase = "delete"; dismiss(); } } } There is a static String variable in MainActivity named deleteOrErase. From dialog it returns the string delete if Button Yes is clicked. Code from MainActivity: tv.setOnLongClickListener(new View.OnLongClickListener() { #Override public boolean onLongClick(View v) { String text = tv.getText().toString(); PermissionToDelete permissionToDelete = new PermissionToDelete(MainActivity.this); permissionToDelete.show(); if(deleteOrErase.equals("delete")){ String index = ""; for(int i = 1; text.charAt(i) != ')'; i++){ index += text.charAt(i); } tasksList.remove(Integer.parseInt(index) - 1); File rootFile = new File(rootFolder); if(rootFile.exists()){ String[]entries = rootFile.list(); for(String s: entries){ File currentFile = new File(rootFile.getPath(),s); currentFile.delete(); } rootFile.delete(); } dailyTasksLayout.removeAllViews(); makePrimaryFileAndFolder(); saveDataToFile(); setTaskList(); deleteOrErase = ""; } But the problem is before button from dialog is clicked, if(deleteOrErase.equals("delete")) is encountered when the value of deleteOrErase is still null. Therefore data isn't deleted at the first click. Its need second click to delete, because now the value of deleteOrErase is "delete". How can i delete it from first click? Or is there any other way??? thanks dear honorable members! <3 (I don't know either i could clarify my problem or not. Even i can't understand from mty writings :P )
Automatic horizontally scrolling textview getting initialized everytime an image is changing in runnable
I have developed an app in which I have a textview whose text keeps scrolling automatically. I also have an imageview whose image keeps changing automatically after every four seconds. To accomplish this I have created a new runnable having 4 seconds time. Suppose there are 5 images which gets displayed. The problem is every time a new image is loaded in imageview the text of textview gets reset and it starts scrolling from beginning and this happens only for the first iteration of all the images. After all images are displayed once then from the next iteration onwards the textview starts behaving properly and the text doesn't resets. I don't know what is the problem. Here is the code //In onCreate headlinesText = (TextView) findViewById(R.id.text_news); headlinesText.setSelected(true); //This is a separate method public void settingAds() { myRef.child("Advertisements").addValueEventListener(new com.google.firebase.database.ValueEventListener() { #Override public void onDataChange(com.google.firebase.database.DataSnapshot dataSnapshot) { imageUrl.clear(); for (com.google.firebase.database.DataSnapshot eachAdUrl : dataSnapshot.child("Adurls").getChildren()) { imageUrl.add(String.valueOf(eachAdUrl.getValue())); } headlineFromFirebase = String.valueOf(dataSnapshot.child("News").getValue()); adTimer = (long) dataSnapshot.child("Time").getValue(); headlinesText.setText(headlineFromFirebase); /*headlinesText.setOnClickListener(new View.OnClickListener() { #Override public void onClick(View v) { startActivity(new Intent(MainActivity.this, NewsList.class)); } });*/ //Randomly shuffling the array list Collections.shuffle(imageUrl); for(int i=0;i<imageUrl.size();i++) { Picasso.with(MainActivity.this) .load(imageUrl.get(i)) .fetch(); } //runnable code was here if(handler != null) { handler.removeCallbacks(runnable); } handler = new Handler(); runnable = new Runnable() { int imageCounter=0; public void run() { Picasso.with(MainActivity.this) .load(imageUrl.get(imageCounter)) .fit() .centerCrop() .into(mAdsView); imageCounter++; if (imageCounter > imageUrl.size()-1) { imageCounter = 0; } handler.postDelayed(this,adTimer); } }; handler.postDelayed(runnable, 250); } #Override public void onCancelled(DatabaseError databaseError) { } }); }
Array Image and Audio change by clicking on "Next" and "Previous" button in Android
I am making simple on Android and it has 99 Images and 99 Audio Sounds and I created separate array for both of them. I want when I click on next button then "R.drawable.p1" image appear and produce "R.raw.a1" sound and again when I click on next button then "R.drawable.p2" image appear and produce "R.raw.a2 sound". In this case my Images Array is working perfectly as I want but problem is that my audio is not working correct, my next button audio is working well but when I come back to previous button then it produce firstly next button sound and then come to the previous. I want like image1=sound1, image2=sound2, image3=sound3 but problem is that I clicking on Previous button its look like this image2=sound3, image1=sound2. Secondly, one more problem is that when one loop is completed then it muted all the sounds, why? If you know any method, any if-else OR Switch-case logic then plz reply me, any help will be appreciated. This is my code: public class MainActivity extends Activity { private ImageView hImageViewPic; private Button iButton, gButton; MediaPlayer ourSong; private int currentImage = 0; public int currentAudio = 0; int[] images = { R.drawable.p1, R.drawable.p2, R.drawable.p3, R.drawable.p4, R.drawable.p5, R.drawable.p6, R.drawable.p7, R.drawable.p8, R.drawable.p9, R.drawable.p10, R.drawable.p11, R.drawable.p12, R.drawable.p13, R.drawable.p14, R.drawable.p15, R.drawable.p16, R.drawable.p17, R.drawable.p18, R.drawable.p19, R.drawable.p20, R.drawable.p21, R.drawable.p22, R.drawable.p23, R.drawable.p24, R.drawable.p25, R.drawable.p26, R.drawable.p27, R.drawable.p28, R.drawable.p29, R.drawable.p30, R.drawable.p31, R.drawable.p32, R.drawable.p33, R.drawable.p34, R.drawable.p35, R.drawable.p36, R.drawable.p37, R.drawable.p38, R.drawable.p39, R.drawable.p40, R.drawable.p41, R.drawable.p42, R.drawable.p43, R.drawable.p44, R.drawable.p45, R.drawable.p46, R.drawable.p47, R.drawable.p48, R.drawable.p49, R.drawable.p50, R.drawable.p51, R.drawable.p52, R.drawable.p53, R.drawable.p54, R.drawable.p55, R.drawable.p56, R.drawable.p57, R.drawable.p58, R.drawable.p59, R.drawable.p60, R.drawable.p61, R.drawable.p62, R.drawable.p63, R.drawable.p64, R.drawable.p65, R.drawable.p66, R.drawable.p67, R.drawable.p68, R.drawable.p69, R.drawable.p70, R.drawable.p71, R.drawable.p72, R.drawable.p73, R.drawable.p74, R.drawable.p75, R.drawable.p76, R.drawable.p77, R.drawable.p78, R.drawable.p79, R.drawable.p80, R.drawable.p81, R.drawable.p82, R.drawable.p83, R.drawable.p84, R.drawable.p85, R.drawable.p86, R.drawable.p87, R.drawable.p88, R.drawable.p89, R.drawable.p90, R.drawable.p91, R.drawable.p92, R.drawable.p93, R.drawable.p94, R.drawable.p95, R.drawable.p96, R.drawable.p97, R.drawable.p98, R.drawable.p99 }; int[] audios = { R.raw.a1, R.raw.a2, R.raw.a3, R.raw.a4, R.raw.a5, R.raw.a6, R.raw.a7, R.raw.a8, R.raw.a9, R.raw.a10, R.raw.a11, R.raw.a12, R.raw.a13, R.raw.a14, R.raw.a15, R.raw.a16, R.raw.a17, R.raw.a18, R.raw.a19, R.raw.a20, R.raw.a21, R.raw.a22, R.raw.a23, R.raw.a24, R.raw.a25, R.raw.a26, R.raw.a27, R.raw.a28, R.raw.a29, R.raw.a30, R.raw.a31, R.raw.a32, R.raw.a33, R.raw.a34, R.raw.a35, R.raw.a36, R.raw.a37, R.raw.a38, R.raw.a39, R.raw.a40, R.raw.a41, R.raw.a42, R.raw.a43, R.raw.a44, R.raw.a45, R.raw.a46, R.raw.a47, R.raw.a48, R.raw.a49, R.raw.a50, R.raw.a51, R.raw.a52, R.raw.a53, R.raw.a54, R.raw.a55, R.raw.a56, R.raw.a57, R.raw.a58, R.raw.a59, R.raw.a60, R.raw.a61, R.raw.a62, R.raw.a63, R.raw.a64, R.raw.a65, R.raw.a66, R.raw.a67, R.raw.a68, R.raw.a69, R.raw.a70, R.raw.a71, R.raw.a72, R.raw.a73, R.raw.a74, R.raw.a75, R.raw.a76, R.raw.a77, R.raw.a78, R.raw.a79, R.raw.a80, R.raw.a81, R.raw.a82, R.raw.a83, R.raw.a84, R.raw.a85, R.raw.a86, R.raw.a87, R.raw.a88, R.raw.a89, R.raw.a90, R.raw.a91, R.raw.a92, R.raw.a93, R.raw.a94, R.raw.a95, R.raw.a96, R.raw.a97, R.raw.a98 }; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); hImageViewPic = (ImageView) findViewById(R.id.idImageViewPic); iButton = (Button) findViewById(R.id.bIleri); gButton = (Button) findViewById(R.id.bGeri); // Just set one Click listener for the image iButton.setOnClickListener(iButtonChangeImageListener); gButton.setOnClickListener(gButtonChangeImageListener); } View.OnClickListener iButtonChangeImageListener = new OnClickListener() { public void onClick(View v) { try { // Increase Counter to move to next Image currentImage++; currentImage = currentImage % images.length; hImageViewPic.setImageResource(images[currentImage]); ourSong = MediaPlayer.create(MainActivity.this, audios[currentAudio]); currentAudio++; ourSong.start(); } catch (Exception e) { } } }; View.OnClickListener gButtonChangeImageListener = new OnClickListener() { public void onClick(View v) { try { // Decrease Counter to move to previous Image currentImage--; currentImage = (currentImage + images.length) % images.length; hImageViewPic.setImageResource(images[currentImage]); MediaPlayer.create(MainActivity.this, audios[currentAudio]); currentAudio--; ourSong.start(); } catch (Exception e) { } } }; protected void onPause() { // TODO Auto-generated method stub super.onPause(); ourSong.release(); finish(); } }
Simply set this on your button click: int current_img = 0; int current_aud = 0; btn_next.setOnClickListener(new View.OnClickListener() { #Override public void onClick(View view) { current_img ++; if(current_img == pets.length){ current_img = 0; } itemimage.setImageResource(pets[current_img]); if(mp != null && mp.isPlaying()){ mp.stop(); } current_aud++; if(current_aud == petsAudio.length){ current_aud = 0; } mp.reset(); mp = MediaPlayer.create(ViewPagerActivity.this, petsAudio[current_aud]); current_aud --; mp.start(); } });
You must add this method in your code public void cleanup() { if (mp != null && mp.isPlaying()) { mp.stop(); mp.release(); mp = null; } } and call this method in both button listener before MediaPlayer.create(MainActivity.this, audios[currentAudio]);
Play Pause single button
I am trying to implement play/pause in a single button however when i try to press it again it just stars the song again overlapping the sound playing. i have search ways and tried this method and it seems it is not working for me. thank you private OnClickListener handler1 = new OnClickListener(){ public void onClick(View v){ MediaPlayer mpE = MediaPlayer.create(GuitarTuner.this, R.raw.test2 ); if (mpE.isPlaying()) { mpE.pause(); } else { mpE.start(); } } };"
You are creating the the media player every time. Instead try moving MediaPlayer mpE = MediaPlayer.create(GuitarTuner.this, R.raw.test2 ); to outside of the handler.
MediaPlayer mpE = MediaPlayer.create(GuitarTuner.this, R.raw.test2 ); play.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { if (mpE.isPlaying()) { mpE.pause(); play.setBackgroundResource(R.drawable.play); } else { mpE.start(); play.setBackgroundResource(R.drawable.pause); } } });