Using two exoplayer inistances for gapless playback Java - java

I am preparing the next video in another exoplayer instance while playing the current one for gapless playback.
But after setting player visibility gone video is not playing.
Here is my function for playing current video and initializing next
void prepare_next_media() {
int next_index = playing_index + 1;
if (next_index > videoFiles.size() - 1) {
next_index = 0;
}
if (current_view == 1) {
player1.setMediaItem(MediaItem.fromUri(videoFiles.get(next_index).getAbsolutePath()));
player1.prepare();
} else {
player.setMediaItem(MediaItem.fromUri(videoFiles.get(next_index).getAbsolutePath()));
player.prepare();
}
}
void play_next_media() {
int current_index = playing_index + 1;
if (current_index > videoFiles.size() - 1) {
current_index = 0;
}
if (current_view == 1) {
playerView.setVisibility(StyledPlayerView.GONE);
playerView1.setVisibility(StyledPlayerView.VISIBLE);
if (player1.getMediaItemCount() == 0) {
player1.setMediaItem(MediaItem.fromUri(videoFiles.get(current_index).getAbsolutePath()));
player1.prepare();
}
player1.play();
} else {
playerView.setVisibility(StyledPlayerView.VISIBLE);
playerView1.setVisibility(StyledPlayerView.GONE);
if (player.getMediaItemCount() == 0) {
player.setMediaItem(MediaItem.fromUri(videoFiles.get(current_index).getAbsolutePath()));
player.prepare();
}
player.play();
}
current_view = 1 - current_view;
playing_index = current_index;
prepare_next_media();
}
The problem is when I set playerView1.setVisibility(StyledPlayerView.GONE), and prepare it and on completing the previous one playerView.setVisibility(StyledPlayerView.VISIBLE); make it visible but the video is not playing only the image is showing but the video changes when ended.
on onPlaybackStateChanged, it shows the video is playing. I also tried with set alpha but did not work can anyone suggest me better way to solve this issue or issue of gapless playback?
Thanks in advance.

Related

Why doesn't the text on my buttons update in my Android App?

So I am working on developing an app in Android Studio.
At this stage of development, my app seems to be working fine except for the update of the text in my buttons.
The code for my app that should update the text on my buttons is below.
private void selectedAnswer(int answerSelection) {
Question currentQuestion = selectQuestion();
answer0button.setText(currentQuestion.answer0);
answer1button.setText(currentQuestion.answer1);
answer2button.setText(currentQuestion.answer2);
answer3button.setText(currentQuestion.answer3);
if (answerSelection == 0) {
if (answerSelection == currentQuestion.correctAnswer - 1) {
answer0button.setText(currentQuestion.answer0 + " ✔");
addPoints();
}
else {
answer0button.setText(currentQuestion.answer0 + " ✗");
}
}
else if (answerSelection == 1) {
if (answerSelection == currentQuestion.correctAnswer - 1) {
answer1button.setText(currentQuestion.answer1 + " ✔");
addPoints();
}
else {
answer1button.setText(currentQuestion.answer1 + " ✗");
}
}
else if (answerSelection == 2) {
if (answerSelection == currentQuestion.correctAnswer - 1) {
answer2button.setText(currentQuestion.answer2 + " ✔");
addPoints();
}
else {
answer2button.setText(currentQuestion.answer2 + " ✗");
}
}
else if (answerSelection == 3) {
if (answerSelection == currentQuestion.correctAnswer - 1) {
answer3button.setText(currentQuestion.answer3 + " ✔");
addPoints();
}
else {
answer3button.setText(currentQuestion.answer3 + " ✗");
}
}
nextQuestion();
}
However, the line nextQuesiton() stops the text from updating properly. Without this line, the code works as expect.
The line in questions executes the following method.
private void nextQuestion(){
gameRound += 1;
displayQuestion(selectQuestion());
}
This method, in turn, executes this method.
private void displayQuestion(Question question){
questionTextView.setText(question.questionText);
answer0button.setText(question.answer0);
answer1button.setText(question.answer1);
answer2button.setText(question.answer2);
answer3button.setText(question.answer3);
}
It seems that possibly the lines in the selectedAnswer() method and the displayQuestion() method execute at the same time, but the displayQuestion() method overrides the lines in selectedAnswer().
Can anyone tell me what is causing my problem exactly, and how to fix it.
I think there is nothing wrong with your code, but the update of the buttons happens too fast and it changes to the second text before you can even realize that it changed to another text before that. I think that, for the desired behaviour, you should implement an async call that executes the "nextQuestion" after some milliseconds.
Something like:
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
nextQuestion();
}
}, 1000);

Troubles with implementing retroactive audio recording (save last X min instead of whole recoding) functionality to audio recording app Android Studio

Using TVAC Studio's tutorial, I built an app that records audio and saves the files in a list which can be played back within the app.
My problem is that I want to continuously record and upon repressing the mic button, save the last X minutes instead of the whole recoding. I tried to implement a circular/ring buffer, I have not been successful being left with countless errors and cannot seem to get anywhere so I am back to square one. Project can be accessed here and the buffer code I have made a start with is below:
drive: https://drive.google.com/drive/folders/1zHz70clC2hYrSUekjYf9ybZV2jDxLsRM?usp=sharing
//Write
void API_WriteToBuffer(int data_element)
{
if(Is_BufferFull())
{
printf("\nBuffer is Full");
}
else
{
circularBuffer[Head] = data_element;
printf("\nAPI_WriteToBuffer: %d", circularBuffer[Head]);
Head = Head + 1 % BUFFER_SIZE;
if(Head == Tail){
IsFull_Flag =1;
}
}
}
//Read
void API_ReadFromBuffer(int * data_element)
(
if(Is_BufferEmpty())
{
printf("\nBuffer is Empty");
)
else
{
*data_element = circularBuffer[Tail];
printf(".\nAPI_ReadFromBuffer: %d"), CircularBuffer[Tail]);
Tail = (Tail + 1) % BUFFER_SIZE;
IsFull_Flag = 0;
}
}
)
int Is_BufferFull(void)
{
return(IsFull_Flag);
}
int Is_BufferEmpty(void)
{
int temp = 0;
if((Head == Tail) &&
(IsFull_Flag !=1))
{
temp = 1;
}
return(temp);
}

How to render sprite array efficiently in j2me game?

I am working on a landscape java game.
I have a sprite array like this;
Sprite stickArray[] = new Sprite[10];
and initializing it like this with visibility as false.
for (int j = 0; j < stickArray.length; j++) {
if (stickArray[j] != null) {
stickArray[j].setPosition(stickX, stickY);
stickArray[j].setVisible(false);
}
}
Later I want to position it like one after another vertically on repeating a key.pressBool is false initially.
public void keyRepeatInGame(int keyCode) {
int gameKey = getGameAction(keyCode);
Graphics g = getGraphics();
if (gameKey == FIRE || keyCode == KEY_NUM5) {
pressBool = true;
}
}
Later I have written code like this;
Initially one Sticksprite is there.above this sprite, I want to place each sticks on clalling keyrepeat()in space key.
for (int i = 0; i < stickArray.length; i++) {
if (pressBool) {
if (i == 0) {
stickArray[i].setPosition(stickSprite.getX(),
stickSprite.getY() - stickSprite.getHeight());
stickArray[i].setVisible(true);
} else {
stickArray[i].setPosition(stickArray[i-1].getX(),
stickArray[i-1].getY() - stickArray[i].getHeight());
stickArray[i].setVisible(true);
}
}
}
This code is working partially.Stick is getting added on the sprite,all at once.
How can I change the code to make the sticks repeatedly getting added on calling keyrepeat() only, and make it visible one by one properly?
I changed the code with an additional condition and break statement like this;
if (pressBool) {
if (stickSprite != null && !stickArray[i].isVisible()
&& stickArray[i] != null) {
if (i == 0) {
stickArray[i].setPosition(
stickSprite.getX(),
stickSprite.getY());
stickArray[i].setVisible(true);
break;
} else if (i > 0) {
stickArray[i].setPosition(
stickArray[i].getX(),
stickArray[i - 1].getY()
- stickArray[i].getHeight());
stickArray[i].setVisible(true);
break;
}
}
}
It worked.

Uno Project: Making sure that a illegal plays return correctly?

I am writing a chunk of program to play Uno with other classes. My Uno project is almost finished, but I need to be sure that the part which checks to make sure that on moves after a Wild Card is played, I play either a legal card or return a -1. Here is my code:
import java.util.*;
public class AlexaL_UnoPlayer implements UnoPlayer
{
public int play(List<Card> hand, Card upCard, Color calledColor, GameState state)
{
int play = -1;
boolean haveWild = false;
boolean matchesWildCall = false;
int indexOfWild = 0;
//turn number of cards all players are holding into ints for later use
int[] array = state.getNumCardsInHandsOfUpcomingPlayers();
int playerNext = array[0];
int playerTwoNext = array[1];
int playerBefore = array[2];
Color upCardColor = upCard.getColor();
for(int i = 0; i < hand.size(); i++)
{
//see if I have any wilds
if(hand.get(i).getRank().equals(Rank.WILD) || hand.get(i).getRank().equals (Rank.WILD_D4))
{
haveWild = true;
indexOfWild = i;
}
//set upCard color to calledColor if wild or wild_d4 are played
if (upCard.getRank().equals(Rank.WILD) || upCard.getRank().equals(Rank.WILD_D4))
{
upCardColor = calledColor;
}
//always play a card matching rank of upCard, if possible, or play the first in hand which matches color
if(hand.get(i).getColor().equals(upCardColor))
{
if(hand.get(i).getNumber() == upCard.getNumber())
{
play = i;
}
}
//if cornered(no matching number or color), play a wild
else if(haveWild == true)
{
play = indexOfWild;
}
//hold reverse cards until person next after me has less cards than person before me
if(hand.get(i).getRank().equals(Rank.REVERSE) && playerNext < playerBefore)
{
play = i;
}
//play skips when person next to me has less cards than me
if((hand.get(i).getRank().equals(Rank.SKIP) || hand.get(i).getRank().equals(Rank.DRAW_TWO)) && playerNext < hand.size())
{
play = i;
}
}
return play;
}
public Color callColor(List<Card> hand)
{
//strategy: change the color to the one i'm holding the most of
Color changeTo = Color.GREEN;
int numBlues = 0;
int numGreens = 0;
int numReds = 0;
int numYellows = 0;
//find out how many of each color i'm holding
for(int i = 0; i < hand.size(); i++)
{
if(hand.get(i).getColor().equals(Color.BLUE))
{
numBlues++;
}
else if(hand.get(i).getColor().equals(Color.RED))
{
numReds++;
}
else if(hand.get(i).getColor().equals(Color.GREEN))
{
numGreens++;
}
else if(hand.get(i).getColor().equals(Color.YELLOW))
{
numYellows++;
}
}
//find out which i'm holding the most of and call that color
//if no majority, return my favorite color(green)
if(numBlues > numReds && numBlues > numGreens && numBlues > numYellows)
{
changeTo = Color.BLUE;
}
else if(numReds > numBlues && numReds > numGreens && numReds > numYellows)
{
changeTo = Color.RED;
}
else if(numGreens > numBlues && numGreens > numYellows && numGreens > numReds)
{
changeTo = Color.GREEN;
}
else if(numYellows > numBlues && numYellows > numGreens && numYellows > numReds)
{
changeTo = Color.YELLOW;
}
else
{
changeTo = Color.GREEN;
}
return changeTo;
}
}
For some reason, my output is telling me this:
You were given this hand:
0. G7
1. G5
2. G+2
and the up card was: W
and the called color was: YELLOW
and you (wrongly) returned 2.
Valid plays would have included: -1
Can anyone provide some insight on why I am getting this error and how to fix it? Much appreciated!
Based on just this code, it is hard to say what is wrong with your implementation.
Did you try step-by-step debugging of your play function?
Alternately, it would be beneficial to instrument your code with more trace statements to better figure out where the issue might be occurring.
For example:
Every where your play variable gets assigned a value, print its value to console with some context of where you are in code.
if(hand.get(i).getNumber() == upCard.getNumber())
{
play = i;
System.out.println("number match step: play = " + play ); // You can add such a line
}
This should give you an idea as to when play's value is getting mutated to 2 unexpectedly.
Just clutching at straws here, but what are the expected values of getColor and getNumber for wild cards.
In case you have them set to Green or 2 respectively, it might explain the output you are seeing because of the following fragment of code.
//always play a card matching rank of upCard, if possible, or play the first in hand which matches color
if(hand.get(i).getNumber() == upCard.getNumber())
{
play = i;
}
else if(hand.get(i).getColor() == upCardColor)
{
play = i;
}

Why does my Loop mechanism not work?

Sorry if this is a very closed question that won't be useful to others, but I'm just stumped by this bug and I haven't been able to solve it for weeks!
I am working on a wave based survival game and am currently working on a spawning mechanism.
The code I wrote works perfectly for one wave, but somehow doesn't restart for further waves.
I have written the code below:
public void run() {
while (ingame) {
if (enemyList.isEmpty()) {
stopSpawn = false;
try {
Thread.sleep(500);
spawnNewEnemy();
} catch (InterruptedException ex) {
System.out.println("Interrupted");
}
} else {
if (!enemyList.isEmpty() && !stopSpawn) {
// getEnemyAmount returns the amount of enemies that should be spawned this wave
for (int enemiesSpawned = 0; enemiesSpawned < getEnemyAmount(); enemiesSpawned++) {
try {
Thread.sleep(500);
} catch (InterruptedException ex) {
}
System.out.println(currentWave);
spawnNewEnemy();
}
stopSpawn = true;
}
}
}
}
Here is the spawnNewEnemy method
public void spawnNewEnemy() {
Random spawn = new Random();
int spawnX = spawn.nextInt(500);
int spawnXTest = b.bX - spawnX;
if (spawnXTest < 20) {
spawnX = spawnX + 20;
} else {
}
int spawnY = spawn.nextInt(500);
int spawnYTest = b.bX - spawnY;
if (spawnYTest < 20) {
spawnY = spawnY + 20;
} else {
}
spawnY = spawnY + 20;
spawnX = spawnX + 20;
enemyList.add(new Enemy(spawnX, spawnY));
}
I can read the following in your code:
If the list of enemies is empty, you set stopSpawn to false and spawn an enemy.
That triggers your else-statement.
There, you spawn enemies based on the enemy count.
stopSpawn is set to true, thus your else-statement doesn't get triggered anymore.
Nothing happens anymore untill your enemylist is empty.
If your enemylist is empty again, you start over.
The logics seems ok, so I'm thinking either the way you spawn enemies through spawnNewEnemy() is faulty, or the way you remove enemies from the enemyList is faulty. I see neither of that code so that is as far as I can go in this answer.
I guess your problem with a loop is in stopSpawn value.
You set it to true after the first wave and likely not setting to `false' before starting the next wave.

Categories