Reputation: 698
So basically the MediaPlayer is playing the music as I want it to. The issue I am having is trying to get it to release onBackPress() so that 1) it stops playing and 2) it releases it from memory. Here is the code I'm currently using.
public class Audio extends Activity {
MediaPlayer mp,lastPlayed;
String song = "http://www.pocketjourney.com/downloads/pj/tutorials/audio.mp3";
int playClick=0;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.audio);
mp = new MediaPlayer();
lastPlayed = new MediaPlayer();
final Button song1 = (Button)findViewById(R.id.song);
song1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(playClick%2==0){
song1.setBackgroundResource(R.drawable.button_pause);
try {
mp.setDataSource(song);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
mp.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mp.start();
playClick++;
lastPlayed = mp;
}
else if (playClick%2==1){
song1.setBackgroundResource(R.drawable.button_play);
mp.pause();
playClick=0;
}
}
});
}
public void onBackPressed(){
mp.pause();
mp.release();
finish();
}
}
Here is the output I get from the log cat.
09-02 22:35:05.167: ERROR/ActivityManager(1086): ANR in com.fallacystudios.talent (com.fallacystudios.talent/.Audio)
09-02 22:35:05.167: ERROR/ActivityManager(1086): Reason: keyDispatchingTimedOut 09-02 22:35:05.167: ERROR/ActivityManager(1086): Load: 1.57 / 0.74 / 0.35 09-02 22:35:05.167: ERROR/ActivityManager(1086): CPU usage from 9503ms to 2795ms ago: 09-02 22:35:05.167: ERROR/ActivityManager(1086): mediaserver: 17% = 15% user + 1% kernel / faults: 17 minor 09-02 22:35:05.167: ERROR/ActivityManager(1086): ystudios.talent: 17% = 16% user + 0% kernel / faults: 7 minor 09-02 22:35:05.167: ERROR/ActivityManager(1086): system_server: 7% = 4% user + 2% kernel / faults: 5 minor 09-02 22:35:05.167: ERROR/ActivityManager(1086): qtouch_obp_ts_w: 0% = 0% user + 0% kernel 09-02 22:35:05.167: ERROR/ActivityManager(1086): battd: 0% = 0% user + 0% kernel / faults: 3 minor 09-02 22:35:05.167: ERROR/ActivityManager(1086): dsi: 0% = 0% user + 0% kernel 09-02 22:35:05.167: ERROR/ActivityManager(1086): omap2_mcspi: 0% = 0% user + 0% kernel 09-02 22:35:05.167: ERROR/ActivityManager(1086): putmethod.latin: 0% = 0% user + 0% kernel / faults: 79 minor 1 major 09-02 22:35:05.167: ERROR/ActivityManager(1086): m.android.phone: 0% = 0% user + 0% kernel 09-02 22:35:05.167: ERROR/ActivityManager(1086): TOTAL: 35% = 29% user + 5% kernel + 0% irq + 0% softirq 09-02 22:37:19.863: ERROR/Tethering(1086): attempting to remove unknown iface (usb0), ignoring 09-02 22:48:24.214: ERROR/Tethering(1086): attempting to remove unknown iface (usb0), ignoring 09-02 22:49:44.269: ERROR/MediaPlayer(10964): pause called in state 1
I have tried it with and without the pause() in the onBackPressed. Wasn't sure if it would help but figured it was worth a try. It happen like this. I click to play the music... the music plays just fine... I press the back button (most times the first time it does what it should) then I go back in play the music again (which works fine) then I press back again and it delays a few moments and then force closes. The delay happens even when it doesn't force close. I eventually want there to be numerous buttons to play diff songs in which case I'd have the next release the last. If the release isn't working or there is another issue I'd like to find it before I continue forward. Thanks and any and all help is appreciated. I also appreciate you taking the time to read this long thing. Take care.
Upvotes: 0
Views: 846
Reputation: 9189
Some MediaPlayer methods are synchronous, like start(). So what is happening is that the main ui thread is getting stuck at playing and cannot process the next event, such as back button. Run the media player in another thread. Also the implementation of onBackPressed() should be moved to onStop minus the finish().
Upvotes: 1