Reputation: 487
I have two activities, homework and closer. I have a song playing in homework but when a button is pressed, the media player pauses and closer is opened. In my onPause function, I'm trying to pause the media player but it won't work, the closer activity opens and music is still playing. Before, I had it set up so that in the button listener on the activity homework the media player paused but the mediaplayer wouldn't resume when I had it set in onResume. I'm thinking it might be a scope issue for mediaplayer, can anybody help me out with this?
package com.cis.lab4;
import java.io.IOException;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Homework extends Activity {
final MediaPlayer mediaplayer = new MediaPlayer();
int media_length;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_homework);
setPlayer();
mediaplayer.start();
Button next = (Button) findViewById(R.id.homeworkContinue);
final Intent openCloser = new Intent(this, EndActivity.class);
next.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
startActivity(openCloser);
}
});
}
public void onResume(Bundle savedInstanceState){
super.onResume();
mediaplayer.seekTo(media_length);
mediaplayer.start();
}
public void onPause(Bundle savedInstanceState){
mediaplayer.pause();
media_length = mediaplayer.getCurrentPosition();
}
public void setPlayer(){
AssetFileDescriptor afd;
try {
afd = getAssets().openFd("rev.mp3");
mediaplayer.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
mediaplayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Upvotes: 1
Views: 7609
Reputation: 425
you didnt override onPause properly.
try this onPause istead please:
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
mediaplayer.pause();
media_length = mediaplayer.getCurrentPosition();
}
that should work fine. you should do the same for onResume. good night.
Upvotes: 1