Background audio for one specific activity - java

I want to add my recorded audio in background of the activity in which I am explaining an example. Problem is that audio do not stop after ending the activity and it keeps on playing. I want audio only to be played when my activity is at front. Kindly put the service class in following code
public class exp extends Activity {
ImageView imview1,imview2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addexample);
imview2 = (ImageView) findViewById(R.id.imageView2);
imview2.setVisibility(View.INVISIBLE);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
imview2.setVisibility(View.VISIBLE);
}
}, 2000);
}
}

In onResume start your audio
#Override
protected void onResume() {
super.onResume();
//start your audio track here
}
Stop the same in onPause
#Override
protected void onPause() {
//Sop it over here
super.onPause();
}
Create the player instances in onCreate

public class exp extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addexample);
//player starting code goes here.
}
protected void onPause() {
//stop the music player here.
}
}

Try to stop your audio in onPause:
public class exp extends Activity {
ImageView imview1,imview2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addexample);
imview2 = (ImageView) findViewById(R.id.imageView2);
imview2.setVisibility(View.INVISIBLE);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
imview2.setVisibility(View.VISIBLE);
}
}, 2000);
}
#Override
public void onPause(){
super.onPause();
//Kill your audio
}
}
I think your solution is something like that. Try it and comment
Good look!

Related

why I can not use create method out side a function in AppCompatActivity

My app is crashing when I launch when I write code like this
public class MainActivity extends AppCompatActivity {
MediaPlayer mplayer= MediaPlayer.create(this,R.raw.song);
public void playMusic(View view){
mplayer.start();
}
public void pauseMusic(View view){
mplayer.pause();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
but when I write above code like this then it's working perfectly fine
public class MainActivity extends AppCompatActivity {
MediaPlayer mplayer;
public void playMusic(View view){
mplayer= MediaPlayer.create(this,R.raw.song);
mplayer.start();
}
public void pauseMusic(View view){
mplayer.pause();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
please anybody tell me what is wrong with first code
Thanks
because the player must be initialized at the moment when the context is not empty. That is, in the upper code, context == null in player. Therefore, the application crashes.

How to finish other activity when back button is pressed in splash activity?

So im working on slideshow-like project, but my problem is, when im at the middle of slideshow, I want to go back at my main activity where my slideshow will start using back button on my smartphone. What happen is, it will go back as soon as I pressed back button, but the other slideshows will carry on after 3 seconds which I set in my handler.postDelayed(r1, 3000);. Please help. Thank you
public class Dad1 extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dad1);
Runnable r1 = new Runnable() {
#Override
public void run() {
Intent i = new Intent(getApplicationContext(), Dad2.class);
startActivity(i);
finish();
}
};
Handler handler = new Handler();
handler.postDelayed(r1, 3000);
}
}
What you can do is keep the Handler object at the Activity level. When back button is pressed then just cancel the Handler callbacks. So in that case your Handler's run method will not be called.
Please find the sample code below:
public class Dad1 extends AppCompatActivity {
private Handler handler;
private Runnable myRunnable;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initRunnable();
handler.postDelayed(myRunnable,3000);
}
private void initRunnable() {
myRunnable = new Runnable() {
#Override
public void run() {
Intent i = new Intent(getApplicationContext(), Dad2.class);
startActivity(i);
finish();
}
};
}
#Override
public void onBackPressed() {
super.onBackPressed();
if(handler!=null){
handler.removeCallbacks(myRunnable);
}
}
}

How to start an Activity task using Threads?

I want to start the Main2Activity's task(Loading the WebView) when button is pressed but not to show the Main2Activity's Screen until onPageFinished() is called. i had some ideas to get this by Intents however it seems doesn't work.
Here my code :
MainActivity.java :
public class MainActivity extends AppCompatActivity {
Handler handler = new Handler(){
#Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
Intent intent = new Intent(MainActivity.this,Main2Activity.class);
startActivity(intent);
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button = (Button)findViewById(R.id.Button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Runnable runnable = new Runnable() {
#Override
public void run() {
handler.sendEmptyMessage(0);
}
};
Thread thread = new Thread(runnable);
thread.start();
}
}
);
}
}
Main2Activity.java :
public class Main2Activity extends AppCompatActivity {
Intent intent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
final WebView webView = (WebView)findViewById(R.id.webview);
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(webView, url);
intent.putExtra("DONE",1);
// Toast.makeText(getApplicationContext(), "Done!",
//Toast.LENGTH_SHORT).show();
}
});
webView.loadUrl("file:///android_asset/2.2.html");
}
}
is this possible using Intents or i have to do this with some other techniques?
How can i Achieve this goal ?
Thanks All.
I think you should directly go to Main2Activity on button click, without calling any thread in MainActivity.
Then in Main2Activity show an image or something until onPageFinished() is called.

android studio sensor HEART Rate app

I created a simple app to learn how to use sensor heart beat with one button and label here is my code:
public class MainActivity extends AppCompatActivity implements SensorEventListener {
Button show;
TextView showHeartRate;
SensorManager sensorMgr;
Sensor heartRate;
String heartRateValue;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
show = (Button) findViewById(R.id.button);
show.setOnClickListener(displayHeartRate);
showHeartRate = (TextView) findViewById(R.id.showHeartRate);
sensorMgr = (SensorManager)this.getSystemService(SENSOR_SERVICE);
heartRate = sensorMgr.getDefaultSensor(Sensor.TYPE_HEART_RATE);
}
View.OnClickListener displayHeartRate = new View.OnClickListener() {
#Override
public void onClick(View view) {
showHeartRate.setText(heartRateValue);
}
};
#Override
protected void onResume() {
super.onResume();
sensorMgr.registerListener(this,heartRate,SensorManager.SENSOR_DELAY_NORMAL);
}
#Override
protected void onPause() {
super.onPause();
sensorMgr.unregisterListener(this);
}
#Override
public void onSensorChanged(SensorEvent sensorEvent) {
heartRateValue = Integer.toString(sensorEvent.sensor.getType());
}
#Override
public void onAccuracyChanged(Sensor sensor, int i) {
}
I add to the manifest:
<uses-permission android:name="android.permission.BODY_SENSORS"/>
the problem is that the onSensorChanged() is not called.
i check a lot of solutions here but i did not find anything
First, why would you expect it to ask permissions to use your camera if its heartrate sensor that you are trying to use?
Second, I think by the time you click 'display heartrate', you just don't have any data from the sensor yet (heartRateValue is empty), and when data finally comes you don't really update UI, you only update your state. What I suggest is to update your UI state on every sensor change, for example:
#Override
public void onSensorChanged(SensorEvent sensorEvent) {
heartRateValue = Integer.toString(sensorEvent.sensor.getType());
showHeartRate.setText(heartRateValue);
}
or you can avoid code duplication (even thought its only one line), create a setter and and call it from both click handler and sensor handler:
private void setHeartrate(String rate) {
showHeartRate.setText(rate);
}
#Override
public void onSensorChanged(SensorEvent sensorEvent) {
heartRateValue = Integer.toString(sensorEvent.sensor.getType());
setHeartrate(heartRateValue);
}
View.OnClickListener displayHeartRate = new View.OnClickListener() {
#Override
public void onClick(View view) {
setHeartrate(heartRateValue);
}
};
Third, you are not using data from your sensor, look at SensorEvent, data comes in values array and you are only trying to displat sensor type, why? Try this:
#Override
public void onSensorChanged(SensorEvent sensorEvent) {
heartRateValue = Integer.toString(sensorEvent.values.length > 0 ? sensorEvent.values[0] : 0.0f);
showHeartRate.setText(heartRateValue);
}

Background audio for activity

I want to add my recorded audio in background of the activity in which I am explaining an example. Problem is that audio do not stop after ending the activity and it keeps on playing. I want audio only to be played when my activity is at front. Kindly put the service class in following code
public class exp extends Activity {
ImageView imview1,imview2;
final MediaPlayer mediaPlayer=new MediaPlayer();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addexample);
imview2 = (ImageView) findViewById(R.id.imageView2);
mediaPlayer.create(exp.this,R.raw.babydoll);
imview2.setVisibility(View.INVISIBLE);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
imview2.setVisibility(View.VISIBLE);
mediaPlayer.start();
mediaPlayer.setLooping(true);
mediaPlayer.setVolume(100,100);
}
}, 2000);
}
protected void onPause(){
super.onPause();
mediaPlayer.pause();
}
}
First. you must use a Service for playing music, but if you do not want use a service. the way to instaciate a Media player is mediaPlayer = MediaPlayer.create(this,R.raw.facebook_tone);.
this code when the activity pass to background the music is paused, when the activity pass to foreground the music continue playing.
public class MainActivity extends AppCompatActivity {
MediaPlayer mediaPlayer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//get the MediaPlayer object
mediaPlayer = MediaPlayer.create(this,R.raw.facebook_tone);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
mediaPlayer.start();
mediaPlayer.setLooping(true);
mediaPlayer.setVolume(100,100);
}
}, 2000);
}
#Override
protected void onPause() {
super.onPause();
//pause the music
mediaPlayer.pause();
}
#Override
protected void onResume() {
super.onResume();
//validate if the mediaplayer is not null and not playing
if(mediaPlayer != null && !mediaPlayer.isPlaying()){
mediaPlayer.start();
}
}
}

Categories