Media Player is starting and playing stream fine. But if I press stop once, I cannot play that again. App is closed. Code is here
Button StartStop;
final MediaPlayer MyRadio = new MediaPlayer();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StartStop = (Button) findViewById(R.id.StartStop);
StartStop.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
if (MyRadio.isPlaying()){
MyRadio.stop();
MyRadio.release();
StartStop.setText("Start");
} else {
PrepareRadio("http://220.247.162.146:7170/");
MyRadio.start();
StartStop.setText("Stop");
}
}
});
}
public void PrepareRadio(String Source){
try {
MyRadio.reset();
MyRadio.setDataSource(Source);
MyRadio.prepare();
} catch (Exception e) {
e.printStackTrace();
}
}
I need your advice to solve this.
Initialize your MediaPlayer class on onCreate() method.
final MediaPlayer MyRadio;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyRadio = new MediaPlayer();
}
Move MyRadio.release(); and put it in onDestroy in your Activity
Related
This is my second activity and when open this activity threw button activity take load and open after some time and some times UI Freeze and some time black screen open for some seconds
public class AartiActivity extends AppCompatActivity {
final MediaPlayer player1 = new MediaPlayer();
private Button btn_playarti, btn_pausearti, btn_stoparti;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_aarti);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
btn_playarti = (Button) findViewById(R.id.btn_playarti);
btn_pausearti = (Button) findViewById(R.id.btn_pausearti);
btn_stoparti = (Button) findViewById(R.id.btn_stoparti);
try {
player1.setDataSource(STRING_URL);
player1.prepare();
} catch (IOException e) {
e.printStackTrace();
}
btn_playarti.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
player1.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer player) {
player.stop();
}
});
player1.start();
}
});
btn_pausearti.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (player1.isPlaying()) {
player1.pause();
}
}
});
btn_stoparti.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
player1.stop();
}
});
}
#Override
protected void onStop() {
super.onStop();
player1.stop();
}
}
I am trying to get the Image Button to play a sound each time it's clicked. For example, if the sound is playing for few seconds and I press the button again it should start from the beginning. I managed to get the sound working but it's not repeating it. How can I get it to start over?
ImageButton bt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt = (ImageButton) findViewById(R.id.clickme);
final MediaPlayer mp = MediaPlayer.create(this, R.raw.bird);
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mp.start();
}
});
}
Try this:
#Override
public void onClick(View v) {
try {
if (mp.isPlaying()) {
mp.stop();
mp.release();
mp = MediaPlayer.create(context, R.raw.bird);
}
mp.start();
} catch(Exception e) {
e.printStackTrace();
}
}
Use MediaPlayer.seekto() to seek to the start of the audio file.
ImageButton bt;
private MediaPlayer mp = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt = (ImageButton) findViewById(R.id.clickme);
mp = MediaPlayer.create(this, R.raw.bird);
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mp.isPlaying()) {
mp.seekTo(0);
} else {
mp.start();
}
}
});
}
I'm using MediaPlayer for play a click sound when user clicks on a button. Sometimes the sound will play fine but other times it is too slow. For example first click is fine but second click is too slow.
Here is my code:
private MediaPlayer mClickSound;
#Override
protected void onCreate(Bundle savedInstanceState) {
...
mClickSound = MediaPlayer.create(this, R.raw.click);
}
#Override
public void onClick(View view) {
try {
if (mClickSound.isPlaying()) {
mClickSound.stop();
mClickSound.release();
mClickSound = MediaPlayer.create(this, R.raw.click);
}
mClickSound.start();
} catch (Exception e) {
e.printStackTrace();
}
}
Try this:
mClickSound.reset();
AssetFileDescriptor afd = context.getResources().openRawResourceFd(R.raw.click);
if (afd == null) return;
mClickSound.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
mClickSound.start();
afd.close();
setDataSource is taken from here:
https://stackoverflow.com/a/20111291/6159609
The reset method is supposed to be faster.
Please try below code working fine for me...
public class MainActivity extends AppCompatActivity implements View.OnClickListener
{
Button btn;
MediaPlayer mClickSound;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button);
mClickSound = MediaPlayer.create(this, R.raw.click);
btn.setOnClickListener(this);
}
#Override
public void onClick(View view) {
if (mClickSound.isPlaying()) {
mClickSound.reset();
}
else {
mClickSound = MediaPlayer.create(this, R.raw.click);
mClickSound.start();
}
}
}
i am very new in "coding" for Googles Android.
At the moment i am programming a small app with one button. If you click on it, it plays a random sound. BUT :D ... it plays a sound, on every start an other. But no random sounds if you press the button often.
I dont know where is my mistake.
Here is the code (don't laugh!):
public class MainActivity extends Activity {
//MediaPlayer
MediaPlayer mp;
ImageButton soundbutton;
//Sounds
int[] sounds={R.raw.s1, R.raw.s2, R.raw.s3, R.raw.s4, R.raw.s5, R.raw.s6, R.raw.s7, R.raw.s8, R.raw.s9, R.raw.s10};
Random r = new Random();
int Low = 0;
int High = 10;
int rndm = r.nextInt(High-Low) + Low;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
//MediaPlayer
soundbutton = (ImageButton)this.findViewById(R.id.randomsoundbutton89);
mp = MediaPlayer.create(getApplicationContext(),sounds[rndm]);
soundbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
if (mp.isPlaying()) {
mp.stop();
mp.release();
mp = MediaPlayer.create(getApplicationContext(),sounds[rndm]);
}
mp.start();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
Help! (from Germany)!
Use this code to get different sound
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
//MediaPlayer
soundbutton = (ImageButton)this.findViewById(R.id.randomsoundbutton89);
mp = MediaPlayer.create(getApplicationContext(),sounds[rndm]);
soundbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
if (mp.isPlaying()) {
mp.stop();
mp.release();
rndm = r.nextInt(High-Low) + Low;
mp = MediaPlayer.create(getApplicationContext(),sounds[rndm]);
}
mp.start();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
I'm having a bit of a trouble,, here's the gen. idea,, I have created an app a simple game-like application.. however the problem is when I try to click the button in the title screen it doesn't play the click sound.. can anyone please help me.. here's the code for my sound
public class Effects extends Activity implements MediaPlayer.OnCompletionListener {
Button mPlay;
MediaPlayer mPlayer;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPlay = (Button)findViewById(R.id.btnStart);
mPlay.setOnClickListener(playListener);
setContentView(R.layout.activity_main);
}
#Override
public void onDestroy() {
super.onDestroy();
if(mPlayer != null) {
mPlayer.release();
}
}
private View.OnClickListener playListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mPlayer == null) {
try {
mPlayer = MediaPlayer.create(Effects.this, R.raw.button11);
mPlayer.start();
} catch (Exception e) {
e.printStackTrace();
}
} else {
mPlayer.stop();
mPlayer.release();
mPlayer = null;
}
}
};
#Override
public void onCompletion(MediaPlayer arg0) {
// TODO Auto-generated method stub
}
}
and here's my code for the main java that I want the sound to be played when I clicked this button
public class Main extends Activity implements OnClickListener
{
Button about;
Button Quit;
Button start;
protected void onCreate(Bundle onSavedInstanceState) {
super.onCreate(onSavedInstanceState);
setContentView(R.layout.activity_main);
about = (Button)findViewById(R.id.btnAbout);
about.setOnClickListener(this);
Quit = (Button)findViewById(R.id.btnQuit);
Quit.setOnClickListener(this);
start = (Button)findViewById(R.id.btnStart);
start.setOnClickListener(this);
}
public void onClick(View argo)
{
if(argo.getId()==R.id.btnAbout)
{
Intent i = new Intent(this,About.class);
this.startActivity(i);
}
if(argo.getId()==R.id.btnQuit)
{
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
if(argo.getId()==R.id.btnStart)
{
Intent start = new Intent(this,Howto.class);
this.startActivity(start);
Intent svc=new Intent(this,Effects.class);
startService(svc);
}
}
}
Use SoundPool to play your sounds: http://developer.android.com/reference/android/media/SoundPool.html you should init sounds in your onCreate() and then play them back in onClick()
And instead of doing thousands of pointless if(argo.getId()==R.id.btnQuit) (you should at least use else) get familiar with switch/case syntax)