I am trying to play a song that is in remote server and is in this link. You also can check the song. but as per what i have coded the song is not getting played from the remote server.
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
try{
mySong = new MediaPlayer();
mySong.setDataSource("http://www.hrupin.com/wp-content/uploads/mp3/testsong_20_sec.mp3");
mySong.setAudioStreamType(AudioManager.STREAM_MUSIC);
mySong.prepareAsync();
mySong.start();
}
catch(Exception ee){
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText(ee.getMessage());
}
finally{
mySong.reset();
mySong.release();
}
}
});
are you serious?
You are starting it just to reset and release it instantly?find the wrong logic!
Or do you think the finally statement will be executed after the song is played through?
You call prepareAsync() in your code. Because you are preparing asynchronously, you're going to receive a callback called onPrepared(MediaPlayer) after you've declared that your Activity implements MediaPlayer.OnPreparedListener. It is here that you should be calling mySong.start(). Calling it right after prepareAsync would most likely cause an IllegalStateException to occur because the MediaPlayer isn't prepared yet. Finally, you'll want to set a MediaPlayer.OnCompletionListener so you can release the MediaPlayer there instead of the finally block. Also, resetting the MediaPlayer and releasing is redundant. If you're going to release it right away, there's no reason to reset it.
Related
In my project I am trying to make a media player which plays a shoutcast stream. Everything seemed to be working well until I pressed the back button on my device, which I think stops the activity and causes the device to recreate the activity when launched again. The problem is , when the activity is recreated , I lose the control of the mediaplayer and a new mediaplayer is created.
I need to be able to have the mediaplayer's control back at that point. How is it possible?
This part of code belongs to onCreate
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button);
if (mediaPlayer == null){
mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource(getString(R.string.yayin));
} catch (Exception e) {
e.printStackTrace();
}
mediaPlayer.prepareAsync();
}
if(!isPlaying){
btn.setBackgroundResource(R.drawable.oynat);
}
else{
btn.setBackgroundResource(R.drawable.durdur);
}
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(!isPlaying){
playOnReady();
isPlaying = true;
btn.setBackgroundResource(R.drawable.durdur);
}
else{
mediaPlayer.reset();
isPlaying = false;
btn.setBackgroundResource(R.drawable.oynat);
}
}
});
This part of code belongs to the function playOnReady()
private void playOnReady(){
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
}
Take a look at the Android Activity lifecycle flowchart: https://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle
You need to account for the path where onPause is called when you leave the Activity and then onResume is called when you enter it again. The solution for you could be as simple as moving some/all of your code from onCreate into onResume.
Using this tutorial I have managed to build a service which handles the mediaplayer that I can have the control of anytime I need.
I have created a simple Ukulele tuner (The market I think lacks a visually pleasing and extremely simple tuner).
Any how, firstly, through the developer console I can see that there is a Null Pointer Exception at the Button Onclick Events. I have not been able to recreate this, however it has been reported four times.
Secondly, looking at the log while using the app I can see this warning;
E/MP3Extractor(68): Unable to resync. Signalling end of stream.
This entry here MediaPlayer array causing null pointer in Android seems to be along the same lines.
How it works.
Through the use of radio buttons the user selects either to play a single note or a continuous note. I have created a subroutine called StopMediaPlayer that stops, resets and instantiates the MediaPlayers again. This was used because I could never seem to stop the continuous play back but only pause it.
Is the warning and the NullPointerException related? Is there a more efficient/better means of stopping MediaPlayer that will mean that I wont have to re instantiate the notes every time.
Thank You
One of the offending Onclicks
Button gButton = (Button) findViewById(R.id.gButton);
gButton.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
if (singleRadio.isChecked() == true)
{
StopMediaPLayer();
gNote.setLooping(false);
gNote.start();
}
else if (contRadio.isChecked() == true)
{
StopMediaPLayer();
gNote.setLooping(true);
gNote.start();
}
}
});
Stop Media Player Subroutine
public void StopMediaPLayer()
{
Log.i("UkuleleTuner", "Stop Media Player");
gNote.setLooping(false);
cNote.setLooping(false);
eNote.setLooping(false);
aNote.setLooping(false);
gNote.stop();
cNote.stop();
eNote.stop();
aNote.stop();
gNote.reset();
cNote.reset();
eNote.reset();
aNote.reset();
gNote = MediaPlayer.create(this, R.raw.g_note);
cNote = MediaPlayer.create(this, R.raw.c_note);
eNote = MediaPlayer.create(this, R.raw.e_note);
aNote = MediaPlayer.create(this, R.raw.a_note);
}
I am working in android. I am creating a mediaPlayer which is running audio files. i have 10 buttons. i have assigned different url to each button. So when i press button1 then song of url with respect to button 1 is playing. and then i click on 2nd button then song of button 2 is also playing with song 1. but i want to stop song of button 1 when i press button 2.
this is the code i am using for this functionality:-
public void onClick(View v)
{
MediaPlayer mediaPlayer=new MediaPlayer();
if (mediaPlayer.isPlaying())
{
mediaPlayer.stop();
mediaPlayer.release();
}
int i = Integer.parseInt((v.getTag()).toString());
String str=urls[i];
try {
mediaPlayer.setDataSource(str);
mediaPlayer.prepare();
mediaPlayer.start();
} catch (Exception e)
{
e.printStackTrace();
}
}
please check my code and let me know what is mistake done by me.
you have construct a new MediaPlayer object each time the user click you view
how could it be in the running state !!!
calling release() method on a MediaPlayer object it is in thre End state
Once the MediaPlayer object is in the End state, it can no longer be used and there is no way to bring it back to any other state.
but in case you want to reuse a MediaPlayer object you should call the
call the following method in the same order
reset()
make the mediaPlayer enter the Idle state
setDataSource()
set your data source note : the mediaplayer shoud be in the idle state
prepare()
start()
I've made a button and when you click it, it gives away a short sound(Max one second-sound). But after I've clicked the button about 20 times in a row I get force close..
The code is:
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Perform action on clicks
MediaPlayer mp = MediaPlayer.create(getBaseContext(), R.raw.buzzer);
mp.start();
}
});
I've tried with mp.stop(); but then my sound stops after it have been played half of the time...
One more thing, does anyone know how to "prepare" the sound when I click? Because the sound gets delayed with some milliseconds the first time I press the button.
Create a MediaPlayer member variable and initialize it in onCreate() the same way you are doing in the listener. Then in the listener just use this code:
if(mPlayer.isPlaying()) {
mPlayer.stop();
}
mPlayer.start();
Then call mPlayer.release() in your finish() Activity. My guess is that since none of your MediaPlayer instances are being released, it's running out of memory to use.
The official document for MediaPlayer is actually incredibly descriptive and helpful:
http://developer.android.com/reference/android/media/MediaPlayer.html
I am trying to create a music streaming app.
So far it works just fine.
I am using the
MediaPlayer.create(thisContext, Uri.parse(PATH_TO_STREAM));
convenience method to prepare the infinite stream (24x7 mp3 stream).
It hangs for just a few seconds on this call which I have neatly tucked into my startPlaying() method.
The button doesn't show it's getting clicked until after the stream starts playing so at first the user is left wondering if they tapped the button or missed.
So I want to update a TextView label next to the button that says "Wait..." or "Buffering" etc. then clear it after the stream starts so the user knows they pressed the button OK.
Even if I step through this in debug the label doesn't update until after the onClick is finished. I can comment out the last line that clears the label text and can see it set to "Buffering..." OK. But only after it exits the onClick. Is this a limitation of using the media player create() method?
final Button startbutton = (Button) findViewById(R.id.Button01);
this.tvBuffering = (TextView) findViewById(R.id.tvBuffering);
startbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
tvBuffering.setText("Buffering...");
//do something like invalidate() here??
startPlaying(); //blocks here for a few seconds to buffer then plays.
tvBuffering.setText(" "); //clear the text since it's playing by now.
}
});
It's not such a great idea to intentionally include that sort of delay in the UI, because that will block just about anything the user tries to do for those few seconds. I'm assuming that your startPlaying() includes a call to prepare(), as well as start(). When taking data from a source that won't be immediately available (such as a stream), you should use prepareAsync() instead, which will start preparation and return immediately instead of blocking until preparation is complete.
You can attach a callback to your MediaPlayer to then take action once preparation has completed through a MediaPlayer.OnPreparedListener
Here's a simple example. Note that your OnClickListener can stay the same, as long as you change the prepare() in the startPlaying() method to prepareAsync() and remove the start() call from startPlaying().
startbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
tvBuffering.setText("Buffering...");
startPlaying(); //which should call prepareAsync() instead of prepare()
//and have no call to start()
}
});
mYourMediaPlayer.setOnPreparedListener( new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
start();
tvBuffering.setText(" ");
}
});