Media Player android - java

I'm struggling on designing a little audio player.
Just a list of 4 songs - each has it's own textView and when a user clicks on a textView a song starts playing but i have no idea how to make this. I though about making a onClick method for each textView but really dont like this idea.
public class MainActivity extends AppCompatActivity {
MediaPlayer varMediaPlayer;
TextView song1 = (TextView) findViewById(R.id.song1);
TextView song2 = (TextView) findViewById(R.id.song2);
TextView song3 = (TextView) findViewById(R.id.song3);
TextView song4 = (TextView) findViewById(R.id.song4);
/* MediaPlayer hell = MediaPlayer.create(this, R.raw.Hell);
MediaPlayer bells = MediaPlayer.create(this, R.raw.HellsBells);
MediaPlayer shoot = MediaPlayer.create(this, R.raw.Shoot);
MediaPlayer thunder = MediaPlayer.create(this, R.raw.Thunderstruck);*/
public void playMusic (View view) {
varMediaPlayer = MediaPlayer.create(this, R.raw.Hell);
varMediaPlayer.start();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}

the following variables:
TextView song1 = (TextView) findViewById(R.id.song1);
TextView song2 = (TextView) findViewById(R.id.song2);
TextView song3 = (TextView) findViewById(R.id.song3);
TextView song4 = (TextView) findViewById(R.id.song4);
will make your app crash as your assigning the variables when the MainActivity object is created.
move those to onCreate(Bundle) and assign a click listener to each of those TextViews
private static final int RAW_MUSIC_1 = R.raw.Hell;
private static final int RAW_MUSIC_2 = R.raw.HellsBells;
private static final int RAW_MUSIC_3 = R.raw.Shoot;
private static final int RAW_MUSIC_4 = R.raw.Thunderstruck;
private MediaPlayer varMediaPlayer;
private TextView song1, song2, song3, song4;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// assign after setContentView(...) as findViewById scan the content view layout
song1 = (TextView) findViewById(R.id.song1);
song2 = (TextView) findViewById(R.id.song2);
song3 = (TextView) findViewById(R.id.song3);
song4 = (TextView) findViewById(R.id.song4);
// add click listeners
song1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View view) {
playMusic(RAW_MUSIC_1);
}
};
song2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View view) {
playMusic(RAW_MUSIC_2);
}
};
song3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View view) {
playMusic(RAW_MUSIC_3);
}
};
song4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View view) {
playMusic(RAW_MUSIC_4);
}
};
}
and change playMusic(View) method to playMusic(int) and add release and reset methods to release object from memory
private void playMusic(final int musicId) {
try {
if (mediaPlayer.isPlaying()) mediaPlayer.reset();
} catch (final Exception e) {}
try {
mediaPlayer.release();
} catch (final Exception e) {}
mediaPlayer = MediaPlayer.create(this, id);
mediaPlayer.start();
}

Replace your code with this one and try running this...
public class MainActivity extends AppCompatActivity {
MediaPlayer varMediaPlayer;
MediaPlayer mediaPlayer = new MediaPlayer();
public void playMusic (Integer id) {
if(mediaPlayer.isPlaying()){
mediaPlayer.reset();
}
mediaPlayer = MediaPlayer.create(this , id);
mediaPlayer.start();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView song1 = (TextView) findViewById(R.id.song1);
TextView song2 = (TextView) findViewById(R.id.song2);
TextView song3 = (TextView) findViewById(R.id.song3);
TextView song4 = (TextView) findViewById(R.id.song4);
song1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
playMusic(R.raw.hell);
}
});
song1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
playMusic(R.raw.HellsBells);
}
});
song1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
playMusic(R.raw.Shoot);
}
});
song1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
playMusic(R.raw.Thunderstruck);
}
});
}
}

Related

Like hitting counter in recyclerview

How to implement like button hitting count in RecyclerView. I have already created a like button and counter TextView with this code but when I hit the like button the application crashes. This is my code, what's wrong? I am using toggle button for like option and a TextView for counting like hitting.
This is my viewholder
public class WritingsViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView MainTitle;
public ImageView Mainimage;
public ImageView Profimage;
public TextView ProfName;
public TextView txv;
public ToggleButton liketglbtn;
public TextView MainDesc, Idvkanme;
private ItemClickListner itemClickListner;
public void setItemClickListner(ItemClickListner itemClickListner) {
this.itemClickListner = itemClickListner;
}
public WritingsViewHolder(View itemView) {
super(itemView);
MainTitle = (TextView)itemView.findViewById(R.id.RTitle);
Mainimage = (ImageView)itemView.findViewById(R.id.rImageView);
Profimage = (ImageView)itemView.findViewById(R.id.Profile_Image);
ProfName = (TextView)itemView.findViewById(R.id.Prof_Name);
MainDesc = (TextView)itemView.findViewById(R.id.rDescriptionTv);
Idvkanme = (TextView)itemView.findViewById(R.id.Idavaka_Name);
txv = (TextView)itemView.findViewById(R.id.tx);
liketglbtn = (ToggleButton) itemView.findViewById(R.id.likeToggleBtn);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View v) {
itemClickListner.onClick(v,getAdapterPosition(),false);
}
}
This is my activity
public class WritingsActivity extends AppCompatActivity {
RecyclerView recyclerView;
FirebaseRecyclerOptions<IdavakaModel> options;
FirebaseRecyclerAdapter<IdavakaModel, WritingsViewHolder> adapter;
DatabaseReference MCC;
String categoryId = "";
RecyclerView.LayoutManager layoutManager;
TextView smsCountText;
int pendingSMSCount = 10;
static Button notifCount;
static int mNotifCount = 0;
Button FBcard, YoutubeCard;
private static final String APP_ID = "ca-app-pub-8867939169855032~9998384849";
Button mSaveBtn, mShareBtn;
private long backPressedTime;
private Toast backToast;
private int mCounter = 0;
ToggleButton btn;
TextView txv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_writings);
FBcard = (Button) findViewById(R.id.button1);
FBcard.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent toy = new Intent(WritingsActivity.this, IdavakaNewsUploadActivity.class);
startActivity(toy);
WritingsActivity.this.finish();
}
});
MobileAds.initialize(this,APP_ID);
AdView adView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
//Bottom Navigation Bar
final BottomNavigationView bottomNavigationView = (BottomNavigationView)findViewById(R.id.botmnavigation);
bottomNavigationView.setSelectedItemId(R.id.action_privacy);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
switch (menuItem.getItemId()){
case R.id.myhome:
startActivity(new Intent(getApplicationContext(),MainActivity.class));
overridePendingTransition(0,0);
return true;
case R.id.action_notification:
startActivity(new Intent(getApplicationContext(),ChristianNewsMainActivity.class));
overridePendingTransition(0,0);
return true;
case R.id.action_privacy:
return true;
}
return false;
}
});
MCC= FirebaseDatabase.getInstance().getReference().child("Writings");
recyclerView = (RecyclerView) findViewById(R.id.readingroom_recyclerView);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setReverseLayout(true);
linearLayoutManager.setStackFromEnd(true);
recyclerView.setLayoutManager(linearLayoutManager);
LoadData(categoryId);
}
private void LoadData(String categoryId) {
options = new FirebaseRecyclerOptions.Builder<IdavakaModel>().setQuery(MCC,IdavakaModel.class).build();
adapter = new FirebaseRecyclerAdapter<IdavakaModel, WritingsViewHolder>(options) {
#Override
protected void onBindViewHolder(#NonNull WritingsViewHolder writingsViewHolder, int i, #NonNull IdavakaModel idavakaModel) {
writingsViewHolder.MainTitle.setText(idavakaModel.getTitle());
writingsViewHolder.MainDesc.setText(idavakaModel.getDesc());
writingsViewHolder.ProfName.setText(idavakaModel.getProfname());
writingsViewHolder.Idvkanme.setText(idavakaModel.getIdavakaname());
Picasso.get().load(idavakaModel.getPostimage())
.into(writingsViewHolder.Mainimage);
Picasso.get().load(idavakaModel.getProfimage())
.into(writingsViewHolder.Profimage);
writingsViewHolder.setItemClickListner(new ItemClickListner() {
#Override
public void onClick(View view, int position, boolean isLongClick) {
Intent detailsIntent = new Intent(WritingsActivity.this, IdavakaDetailActivity.class);
detailsIntent.putExtra("CategoryId", adapter.getRef(position).getKey());
startActivity(detailsIntent);
}
});
writingsViewHolder.liketglbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mCounter++;
txv.setText(Integer.toString(mCounter));
}
});
}
#NonNull
#Override
public WritingsViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.writngs_items, parent, false);
return new WritingsViewHolder(v);
}
};
adapter.startListening();
recyclerView.setAdapter(adapter);
}
#Override
public void onBackPressed() {
if (backPressedTime + 2000 > System.currentTimeMillis()) {
backToast.cancel();
super.onBackPressed();
WritingsActivity.this.finish();
return;
} else {
backToast = Toast.makeText(getBaseContext(), "See You Soon", Toast.LENGTH_SHORT);
backToast.show();
}
backPressedTime = System.currentTimeMillis();
WritingsActivity.this.finish();
}
public void onCustomToggleClick (View view) {
Toast.makeText(this, "Liked",Toast.LENGTH_SHORT).show();
}
}
You are getting NullPointerException because the TextView you are using is in Activity named txv where you are setting counter. But the problem is you have declared TextView in Activity but have not initialized anywhere in the Activity. You MUST initialize that variable via findViewbyId method before setting values.
Furthermore, you have initialized txv TextView and accessing on Activity that is why you are getting NLP. TextView is a part of Activity layout which you have not Initialized. On the hand you have initialized txv in Adapter which is of no use.

Sound buttons stopped working after some time

I have problem with my android app. It's soundbar application.
After run, everything works perfectly. After some time and clicking on buttons, I have error:
E/MediaPlayer: start called in state 0, mPlayer(0x7d176f4300)
E/MediaPlayer: error (-38, 0)
I run this on my Samsung S7, I would ask for understanding becouse I am beginner. I saw similar topics but I can't find answer which helps me. Here is my code:
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final MediaPlayer AaaCoJeszcze = MediaPlayer.create(this, R.raw.aaa_co_jeszcze);
Button But1 = this.findViewById(R.id.Button1);
But1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
AaaCoJeszcze.start();
}
});
final MediaPlayer CoNieMoznaMP = MediaPlayer.create(this, R.raw.co_nie_mozna);
Button But2 = this.findViewById(R.id.Button2);
But2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
CoNieMoznaMP.start();
}
});
final MediaPlayer CoToJaWinnyMP = MediaPlayer.create(this, R.raw.co_to_ja_winny);
Button But3 = this.findViewById(R.id.Button3);
But3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
CoToJaWinnyMP.start();
}
});
final MediaPlayer debilMP = MediaPlayer.create(this, R.raw.debil);
Button But4 = this.findViewById(R.id.Button4);
But4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
debilMP.start();
}
});
final MediaPlayer iTenMP = MediaPlayer.create(this, R.raw.i_ten_o_co_chodzi);
Button But5 = this.findViewById(R.id.Button5);
But5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
iTenMP.start();
}
});
final MediaPlayer JedziemyMP = MediaPlayer.create(this, R.raw.jedziemy_z_kurwami);
Button But6 = this.findViewById(R.id.Button6);
But6.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
JedziemyMP.start();
}
});
final MediaPlayer JegoMP = MediaPlayer.create(this, R.raw.jego_robota_moze_poczekac);
Button But7 = this.findViewById(R.id.Button7);
But7.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
JegoMP.start();
}
});
final MediaPlayer ZanimMP = MediaPlayer.create(this, R.raw.zanim_podeszlem);
Button But8 = this.findViewById(R.id.Button8);
But8.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ZanimMP.start();
}
});
final MediaPlayer MozePanOdejscMP = MediaPlayer.create(this, R.raw.moze_pan_odejsc);
Button But9 = this.findViewById(R.id.Button9);
But9.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
MozePanOdejscMP.start();
}
});
final MediaPlayer MozeZrobilPomylkeMP = MediaPlayer.create(this, R.raw.moze_zrobil_pomylke);
Button But10 = this.findViewById(R.id.Button10);
But10.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
MozeZrobilPomylkeMP.start();
}
});
final MediaPlayer MowiszTakTakMP = MediaPlayer.create(this, R.raw.mowisz_tak_tak);
Button But11 = this.findViewById(R.id.Button11);
But11.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
MowiszTakTakMP.start();
}
});
final MediaPlayer PaniMnieMP = MediaPlayer.create(this, R.raw.pani_mnie_rozprasza);
Button But12 = this.findViewById(R.id.Button12);
But12.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
PaniMnieMP.start();
}
});
final MediaPlayer PrawdopodobnieMP = MediaPlayer.create(this, R.raw.prawdopodobnie_polak);
Button But13 = this.findViewById(R.id.Button13);
But13.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
PrawdopodobnieMP.start();
}
});
final MediaPlayer ProszeNiePrzeszkadzacMP = MediaPlayer.create(this, R.raw.prosze_nie_przeszkadzac_dobrze);
Button But14 = this.findViewById(R.id.Button14);
But14.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ProszeNiePrzeszkadzacMP.start();
}
});
final MediaPlayer PrzepraszamPaniaMP = MediaPlayer.create(this, R.raw.przepraszam_pania_spoczko);
Button But15 = this.findViewById(R.id.Button15);
But15.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
PrzepraszamPaniaMP.start();
}
});
final MediaPlayer RozpraszaMP = MediaPlayer.create(this, R.raw.rozprasza_mnie);
Button But16 = this.findViewById(R.id.Button16);
But16.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
RozpraszaMP.start();
}
});
final MediaPlayer RozumiszMP = MediaPlayer.create(this, R.raw.rozumisz);
Button But17 = this.findViewById(R.id.Button17);
But17.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
RozumiszMP.start();
}
});
final MediaPlayer spoczkoMP = MediaPlayer.create(this, R.raw.spoczko);
Button But18 = this.findViewById(R.id.Button18);
But18.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
spoczkoMP.start();
}
});
final MediaPlayer SzefiemozeszMP = MediaPlayer.create(this, R.raw.szefie_mozesz_odejsc);
Button But19 = this.findViewById(R.id.Button19);
But19.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
SzefiemozeszMP.start();
}
});
final MediaPlayer AleodejdzMP = MediaPlayer.create(this, R.raw.szefie_odejdz_ale_odejdz);
Button But20 = this.findViewById(R.id.Button20);
But20.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
AleodejdzMP.start();
}
});
final MediaPlayer MozeMP = MediaPlayer.create(this, R.raw.moze_moze_moze);
Button But21 = this.findViewById(R.id.Button21);
But21.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
MozeMP.start();
}
});
final MediaPlayer RekoczynyMP = MediaPlayer.create(this, R.raw.zadnych_rekoczynow);
Button But22 = this.findViewById(R.id.Button22);
But22.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
RekoczynyMP.start();
}
});
final MediaPlayer PowinnoDojscMP = MediaPlayer.create(this, R.raw.juz_powinno_dojsc);
Button But23 = this.findViewById(R.id.Button23);
But23.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
PowinnoDojscMP.start();
}
});
}}

i want to create a counter with a text view

i want to create a counter with a text view and want for every 15 click , to change the text in the text view and that is what i have wrote to create the counter ........ i have two text views .... one for the no. of clicks and the another for the text i want to show ,,,,,,,,,,, so i want to use "if" to set the counter to 0 (the counter is tvx) and to change the text (tvx2) to "click 15 more"
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tips_2);
btn = (Button) findViewById(R.id.bt);
txv = (TextView) findViewById(R.id.tx);
txv2 = (TextView) findViewById(R.id.tx2);
btn.setOnClickListener(new View.OnClickListener()
{
public int mCounter;
public Integer tx;
#Override
public void onClick(View v) {
mCounter++;
txv.setText(Integer.toString(mCounter));
}
});
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tips_2);
btn = (Button) findViewById(R.id.bt);
txv = (TextView) findViewById(R.id.tx);
txv2 = (TextView) findViewById(R.id.tx2);
public int mCounter=0;
public Integer tx =15;
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mCounter++;
if(mCounter==tx){
txv.setText(Integer.toString(mCounter));
//reset the counter after 15 click
mCounter=0
}
}
}
);
}
}
you can this way, use the ValueAnimator
public void animateText(Integer startValue, Integer endValue) {
setStartValue(startValue);
setEndValue(endValue);
ValueAnimator animator = ValueAnimator.ofInt(startValue, endValue);
animator.setInterpolator(getInterpolator());
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
public void onAnimationUpdate(ValueAnimator animation) {
String cleanString = animation.getAnimatedValue().toString().replaceAll("[,]", "");
BigDecimal parsed = new BigDecimal(cleanString);
String formatted = NumberFormat.getNumberInstance(Locale.US).format(parsed);
tv.setText(formatted);
}
});
animator.setEvaluator(new TypeEvaluator<Integer>() {
public Integer evaluate(float fraction, Integer startValue, Integer endValue) {
return Math.round(startValue + (endValue - startValue) * fraction);
}
});
animator.setDuration(getDuration());
animator.start();
}
Hello Try this if it may help
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tips_2);
btn = (Button) findViewById(R.id.bt);
txv = (TextView) findViewById(R.id.tx);
txv2 = (TextView) findViewById(R.id.tx2);
btn.setOnClickListener(new View.OnClickListener() {
public int mCounter=0;
//initialize mCounter with 0
public Integer tx;
#Override
public void onClick(View v) {
if(mCounter==15){
//check if mCounter equals 15
txv2.setText(Integer.toString("text to display after 15 click"));
//reset mCounter for another iteration
mCounter=0;
}
mCounter++;
//increment the mCounter
txv.setText(Integer.toString(mCounter));
}
});
}
I'm not really understand what do you want, but if you want to increase the counter every 15 click, just add if conditional in your code like this:
btn.setOnClickListener(new View.OnClickListener() {
public int mCounter;
public Integer tx;
#Override
public void onClick(View v) {
mCounter++;
if(mCounter == 15){
tx++;
txv.setText(String.valueOf(tx));
}
}
}
);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tips_2);
btn = (Button) findViewById(R.id.bt);
txv = (TextView) findViewById(R.id.tx);
txv2 = (TextView) findViewById(R.id.tx2);
btn.setOnClickListener(new View.OnClickListener() {
public int mCounter;
public Integer tx;
public int mCounter2;
#Override
public void onClick(View v) {
mCounter++;
mCounter2++;
txv.setText(""+mCounter);
if(mCounter2==15){
txv2.setText(""+mCounter2);
mCounter2=0;
}
}
}
);
}
I hope and you serve this
public class MainActivity extends AppCompatActivity {
private Button btn;
private TextView txv,txv2;
private int contador = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.bt);
txv = (TextView) findViewById(R.id.tx);
txv2 = (TextView) findViewById(R.id.tx2);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
++contador;
if(contador <= 15){
txv.setText(String.valueOf(contador));
}else{
txv2.setText("your text");
}
}
});
}
}

How to make the button loads the previous page in Android?

I have search a lot in different sites to solve this problem but I can't till now.
I have a simple app with 50 articles, two buttons, previous and next.
All works fine till the last article 50 where the problem is that the user can't choose the previous button 49, only can load from the beginning.
Here is my code: MainActivity.java
public class MainActivity extends AppCompatActivity {
private TextView meditCenterText;
private Button mStartButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
meditCenterText = (TextView) findViewById(R.id.editCenterText);
mStartButton = (Button) findViewById(R.id.startButton);
meditCenterText.setMovementMethod(new ScrollingMovementMethod());
mStartButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String TextView = meditCenterText.getText().toString();
startStory(TextView);
}
});
}
private void startStory(String TextView) {
Intent intent = new Intent(this, StoryActivity.class);
intent.putExtra("TextView", TextView);
startActivity(intent);
}
#Override
protected void onResume() {
super.onResume();
meditCenterText.setText("..... ");
}}
StoryActivity.java
public class StoryActivity extends AppCompatActivity {
public static final String TAG = StoryActivity.class.getSimpleName();
private Story mStory = new Story();
private ImageView mImageView;
private TextView mTextView;
private Page mCurrentPage;
private String mName;
private Button mPreviousButton;
private Button mNextButton;
private ScrollView mScrollView;
private Button mStartButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_story2);
//Intent intent = getIntent();
//mName = intent.getStringExtra(getString(R.string.key_name));
if(mName == null){
mName = "";
}
Log.d(TAG, mName);
mImageView = (ImageView)findViewById(R.id.storyImageView);
mTextView = (TextView)findViewById(R.id.storyTextView);
mPreviousButton = (Button)findViewById(R.id.previousButton);
mNextButton = (Button)findViewById(R.id.nextButton);
mTextView.setMovementMethod(new ScrollingMovementMethod());
loadPage(0);
}
private void loadPage(int choice){
mCurrentPage = mStory.getPage(choice);
Drawable drawable = getResources().getDrawable(mCurrentPage.getImageId());
mImageView.setImageDrawable(drawable);
String pageText = mCurrentPage.getText();
//add the name if placeholder included.
//pageText = String.format(pageText,mName);
mTextView.setText(pageText);
if(mCurrentPage.isSingle()){
mPreviousButton.setVisibility(View.VISIBLE);
mNextButton.setText("Start From The Beginning");
mNextButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mPreviousButton.setVisibility(View.VISIBLE);
loadPage(0);
}
});
}else{
mPreviousButton.setText(mCurrentPage.getChoices().getText1());
mNextButton.setText(mCurrentPage.getChoices().getText2());
mPreviousButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int previousPage = mCurrentPage.getChoices().getPreviousPage();
loadPage(previousPage);
}
});
mNextButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
int nextPage = mCurrentPage.getChoices().getNextPage();
loadPage(nextPage);
}
});
}}
}
Thanks for help.
It's probably the logic in your Page class. Have a proper look at it, otherwise from the android side everything is alright. One last thing, you should probably use a ViewPager for your app instead of changing the text every time you load a new oage

How to stop already playing sound and immediately start another

When a sound that the user previously selected is playing, how can I stop that sound and immediately play the new selected sound? The code I have so far only stops the new sound, but the user has to select the sound again in order to play it.
public class MainActivity extends Activity {
public MediaPlayer mediaPlayer = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button a = (Button) findViewById(R.id.First);
a.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (mediaPlayer == null) {
mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.firstsound);
mediaPlayer.stop();
}
else {
mediaPlayer.start();
mediaPlayer = null;
}
}
}
);
Button b = (Button) findViewById(R.id.Second);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (mediaPlayer == null) {
mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.secondsound);
mediaPlayer.stop();
}
else {
mediaPlayer.start();
mediaPlayer = null;
}
}
}
);
I am making a soundboard with over 30 sounds, if that helps at all. What (slight I assume) changes would I need to make to my code? Any help would be greatly appreciated!
public class MainActivity extends Activity {
public MediaPlayer mediaPlayer = null;
public Boolean playing=false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button a = (Button) findViewById(R.id.First);
a.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
(playing)mediaPlayer.stop();
mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.firstsound);
mediaPlayer.start();
playing=true;
}
});
Button b = (Button) findViewById(R.id.Second);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
(playing)mediaPlayer.stop();
mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.firstsound);
mediaPlayer.start();
playing=true;
}
});
}
}

Categories