I am trying to use media player in my app and I have trying to stop media player from other activity my coding is following:
FirstActivity:
public void stop() {
if (playPause == false) {
control.setBackgroundResource(R.drawable.play);
mediaPlayer.stop();
new Player().cancel(true);
media.stop();
media.reset();
mediaPlayer.reset();
if (mediaPlayer.isPlaying())
mediaPlayer.stop();
mediaPlayer.reset();
media.stop();
playPause = true;
} else {
control.setBackgroundResource(R.drawable.pause);
if (intialStage) {
new Player()
.execute(URL);
} else {
if (!mediaPlayer.isPlaying())
mediaPlayer.start();
}
playPause = false;
}
}
SecondActivity:
Have to stop media player after the timer is end:
#Override
public void onFinish() {
textViewTime.setText(hmsTimeFormatter(timeCountInMilliSeconds));
// call to initialize the progress bar values
setProgressBarValues();
// hiding the reset icon
imageViewReset.setVisibility(View.GONE);
// changing stop icon to start icon
imageViewStartStop.setImageResource(R.drawable.icon_start);
// making edit text editable
editTextMinute.setEnabled(true);
// changing the timer status to stopped
timerStatus = TimerStatus.STOPPED;
MainActivity main = new MainActivity();
main.stop();
}
}.start();
countDownTimer.start();
}
the above coding is shows error:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setBackgroundResource(int)' on a null object reference
at com.digitamatix.mukilfm.MainActivity.stop(MainActivity.java:549)
I have to stop media player in first activity please help me on my coding to fix my issues
see i have started activity with startActivityForResult . onActivityResult you will get result.
startActivityForResult(Activity1.createIntent(this), 1001);
//handle callback result
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == 1001) {
}
}
}
Below code will call in next activity where you want to callback and finish the activity. You can pass any value with this intent.
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
So, as per requirement you can use this. Stop media player. Other option local broadcast too.
Hope this will help you.
(Based on the code you posted)
Looks like the problem here is that you are calling a method which refers to an xml resource from an activity which is not visible.
You are creating an istance of the MainActivity but, since it's never been shown, the onCreate and other lifecycle methods are never fired and your control is null.
You have at least 3 ways for doing it:
Create a method which doesn't refer to any xml resource, and it just stop the player
1B. Try using setResult, it's not a good practice to reference a new istance of a running activity. Just use StartActivityForResult(activityB) and setResult(res) for firing the onActivityResult callback.
Put the player in a Singleton and refer to it wherever you are
Use fragment navigation instead of activity and refer to the player in main activity
Hope this helps!
Related
I'd like to play music across Activities and I'm using a simple class to implement it. This class ( BackgroundMusic ) starts the music with MediaPlayer when I call the startMusic() method and stops it when I call the stopMusic() method. When I use it only in one Activity it works perfectly. OnCreate calls startMusic() method and onPause calls stopMusic() method and the MediaPlayer behave on the right way. The problem starts when I'd like to move to another Activity. When I'd like to stop the music it throws me NullPointerExepction for the mediaplayer.stop() . So it looks like the app thinks that I want to stop a never started MediaPlayer. I tried to call the startMusic() method in every onCreate method but the music starts again and again and I'd like to play only one music which don't stop and starts again when I move to another Activity. Is it possible to do that with class or I have to use Service? I hope you can help me to that with class.
BackgroundMusic
public void startMusic() {
mediaPlayer1 = MediaPlayer.create(context, R.raw.zenenegy);
if(palya <= 5 || palya > 15){
mediaPlayer1.start();
mediaPlayer1.setVolume(0.2f, 0.2f);
mediaPlayer1.setLooping(true);
play = true;
}
}
public void stopMusic(){
if(play){
mediaPlayer1.stop();
mediaPlayer1.reset();
mediaPlayer1.release();
mediaPlayer1 = null;
play = false;
}
}
An Activity
BackgroundMusic bm;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fomenu);
bm = new BackgroundMusic(mentes,this);
if(sounds){
bm.startMusic();
}
}
#Override
protected void onPause() {
if(sounds){
bm.stopMusic();
}
super.onPause();
}
If I set the mediaplayer to static in the BackgroundMusic it works perfectly.
i am searched many solution but could not find proper result when i press the back button of mobile i am using one counter variable and put the condition like when counter is 0 it called the music file.
but when i am comeout from that activity means press back button of mobile still that activity is runnig and called the music file .i am using this below code for music play.please anyone have solution then help me.i am using one timer function.like this when i==0 that time call one music file but when i press back button. this timer i wants to stop.
public void btntimer(){
new Handler().postDelayed((new Runnable() {
#Override
public void run() {
if(i!=32) {
matchNo();
}
else {
try{
showdialog();
media=1;
}catch (Exception e){
media=0;
}
updatepoints();
}
}
}), 4000);
}
song=MediaPlayer.create(this,R.raw.cartoon1);
song.start();
song.setLooping(false);
If I understand you correct, you want to stop playing music on back button pressed.
Call stop() in onBackPressed method.
#Override
public void onBackPressed() {
song.stop();
song.release();
song = null;
}
I'm using a small 2 second sound effect attached to a button. When clicked, the sound effect plays and the user is taking to the next activity. The function I created is outside onCreate and uses Intent to send the user to the next activity. When I add the mp variables I get an error saying there's a problem with playGame(). What is causing the MediaPlayer to not play when it's placed in this function? Eclipse suggests changing mp.create() to MediaPlayer.create but that doesn't fix the issue.
public class SplashScreenActivity extends ActionBarActivity {
public MediaPlayer mp;
public void playGame(View view) {
mp.create(this, R.raw.bulletricochet);
mp.start();
// Do something in response to button
Intent intent = new Intent(this, QuizActivity.class);
startActivity(intent);
}
public void playRules(View view) {
Intent intentR = new Intent(this, RulesActivity.class);
startActivity(intentR);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
}}
Your mp object is null and yet you are calling create method on it.
To create a MediaPlayer object use MediaPlayer.create. You say you tried it but probably didn't assign the return value to your mp variable.
mp = MediaPlayer.create(this, R.raw.bulletricochet);
I am developing app related to music player, its having equalizer settings, i know every device having a default equalizer. code for getting the default equalizer
equilizer.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent i = new Intent(AudioEffect.ACTION_DISPLAY_AUDIO_EFFECT_CONTROL_PANEL);
i.putExtra(AudioEffect.EXTRA_AUDIO_SESSION, mp.getAudioSessionId());
startActivityForResult(i, 11113);
}
});
}
In onActivityResult:
protected void onActivityResult(int requestCode,int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
return;
}
}
Using the above code we can get default equalizer, but my requirement is design a equalizer with different UI and add some more effects to the equalizer.
Can any one give me an idea how can I do that?
There is an Equalizer effect. You can pass the audio session ID of the MediaPlayer to its constructor:
// Given a MediaPlayer mp
Equalizer eq = new Equalizer(0, mp.getAudioSessionId());
Here is an example program that uses it. I haven't built it to see, but it at least hooks up the equalizer correctly.
I have just started to develop my first Android app, and I am having a hard time figuring out how to start the microphone and have it listen, which is a main feature of my app.
I've searched the Android docs and I can't find much info on this.
Thanks in advance.
Maybe this can help (actually from the Android docs):
Audio Capture
Create a new instance of android.media.MediaRecorder.
Set the audio source using MediaRecorder.setAudioSource(). You will probably want to use MediaRecorder.AudioSource.MIC.
Set output file format using MediaRecorder.setOutputFormat().
Set output file name using MediaRecorder.setOutputFile().
Set the audio encoder using MediaRecorder.setAudioEncoder().
Call MediaRecorder.prepare() on the MediaRecorder instance.
To start audio capture, call MediaRecorder.start().
To stop audio capture, call MediaRecorder.stop().
When you are done with the MediaRecorder instance, call MediaRecorder.release() on it. Calling MediaRecorder.release() is always recommended to free the resource immediately.
or:
Android Audio Recording Tutorial
You can use custom recorder:
final static int RQS_RECORDING = 1;
Uri savedUri;
Button buttonRecord;
#Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
buttonRecord = (Button) findViewById(R.id.record);
buttonRecord.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(
MediaStore.Audio.Media.RECORD_SOUND_ACTION);
startActivityForResult(intent, RQS_RECORDING);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if (requestCode == RQS_RECORDING) {
savedUri = data.getData();
Toast.makeText(MainActivity.this,
"Saved: " + savedUri.getPath(), Toast.LENGTH_LONG).show();
}
}