Android Studio and mediaPlayer - android-studio

I am developing a simple app in that my video is playing proper. but now I want to give "thank" message after finishing my video
How can I do this?
MainActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
this.setSupportActionBar(toolbar);
final MediaPlayer player = MediaPlayer.create(MainActivity.this,R.raw.video);
Button button = (Button)findViewById(R.id.video_Play);
button.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
player.start();
startActivity(new Intent("com.example.lenovo.play.app.VideoPlay"));
}
});
}
and Second VideoPlay.java
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.video_layout);
view = (VideoView)findViewById(R.id.videoView);
String url = "android.resource://"+getPackageName()+"/"+R.raw.video;
view.setVideoURI(Uri.parse(url));
view.start();
int duration = view.getDuration();
}

Implement MediaPlayer.OnCompletionListener then add your message to onCompletion method:
#Override
public void onCompletion(MediaPlayer mp) {
//message
}

Related

How to add onClickListener to open barcode scanner?

I had write a code on trigger Barcode Scanner. I would like to trigger it after i clink on a button, but it doesn't work when i drop the code inside the onclicklistener. Need help on how to resolve this.
I had also include the button inside.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_isbn = (TextView) findViewById(R.id.tv_display);
btn_camera = (Button) findViewById(R.id.btn_camera);
btn_camera.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
camera();
}
});
}
```

Two differents button for one Animation Drawable

I have a problem with my code I develop a game on android studio and I meet the following concern.
There are two buttons on my app, a Punch button and a Kick Button and an Imageview in which I animate my images.
When I click on the punch button, I create a drawable animation of two images
when I click on the kick button, I create a drawable animation of two images as well.
The problem is that when I execute this code with two drawable animations, the application crashes !!
public class MainActivity extends AppCompatActivity {
private Button btnPunch;
private Button btnKick;
private ImageView imageView1 ;
private ImageView imageView2 ;
private AnimationDrawable[] animationDrawable = new
AnimationDrawable[2];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnPunch = (Button) findViewById(R.id.btnPunch);
imageView1 = (ImageView)
findViewById(R.id.imageView1);
imageView1.setBackgroundResource(
R.drawable.animationpunch);
animationDrawable[0] = (AnimationDrawable)
imageView1.getBackground();
imageView2 = (ImageView)
findViewById(R.id.imageView2);
imageView2.setBackground
Resource(R.drawable.animatio
nkick);
animationDrawable[1] = (AnimationDrawable)
imageView2.getBackground();
btnPunch.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v) {
animationDrawable[1].stop();
animationDrawable[0].setOneShot(true);
animationDrawable[0].start();
}
});
btnKick.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
animationDrawable[0].stop()
animationDrawable[1].setOneShot(true);
animationDrawable[1].start();
}
});
}
#Override
public void onWindowFocusChanged(boolean hasFocus)
{
super.onWindowFocusChanged(hasFocus);
//animationDrawable.start();
}
}
</Code>
I yet use two different imageview for both animations
on the other hand when I remove an animation like the following code, it works well but suddenly I am limited to use the punch button, I would like it to work with both buttons
public class MainActivity extends AppCompatActivity {
private Button btnPunch;
private ImageView imageView1 ;
private AnimationDrawable[] animationDrawable = new
AnimationDrawable[2];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnPunch = (Button) findViewById(R.id.btnPunch);
imageView1 = (ImageView)
findViewById(R.id.imageView1);
imageView1.setBackground
Resource(R.drawable.animatio
npunch);
animationDrawable[0] = (AnimationDrawable)
imageView1.getBackground();
btnPunch.setOnClickListener(new
View.OnClickListener() {
#Override
public void onClick(View v) {
animationDrawable[1].stop();
animationDrawable[0].setOneShot(true);
animationDrawable[0].start();
}
}}
}
#Override
public void onWindowFocusChanged(boolean hasFocus)
{
super.onWindowFocusChanged(hasFocus);
//animationDrawable.start();
}
}
click on punch button, it animates my animation unch
I click on kick button, it animates my animation kick
a help?

Converting a sensor value using simple arithmetic

I am currently working on a light sensor. I managed to make the sensor work. But I want to change the value of the sensor being shown by multiplying it with 0.002 with a press of a button. I can't seem to get the code of the button to multiply because it keeps saying operator (*) cannot be applied to java.lang.String, double. I have tried the double.parse but can't seems to get to work. Here is my code:
public class MainActivity extends AppCompatActivity implements SensorEventListener {
TextView textView;
SensorManager sensorManager;
Sensor sensor;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);
sensorManager = (SensorManager) getSystemService(Service.SENSOR_SERVICE);
sensor = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
Button button = (Button) findViewById(R.id.button3);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View multiply) {
textView.setText(Integer.parseInt(textView.getText().toString()*0.002));
}
});
}
#Override
protected void onPause() {
super.onPause();
sensorManager.unregisterListener(this);
}
#Override
protected void onResume() {
super.onResume();
sensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL);
}
#Override
public void onSensorChanged(SensorEvent sensorEvent) {
if (sensorEvent.sensor.getType() == Sensor.TYPE_LIGHT) {
textView.setText("" + sensorEvent.values[0]);
}
}
#Override
public void onAccuracyChanged(Sensor sensor, int i) {
}

stop all sound on a soundboard app

I want to make a soundboard app and I want it so if I press the button for the sound it first stops all sounds but how do you do that?
public class MainActivity extends AppCompatActivity {
MediaPlayer smileMore;
MediaPlayer intro;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
smileMore = MediaPlayer.create(this,R.raw.smilemore);
intro = MediaPlayer.create(this,R.raw.romanatwoodintro);
}
public void smileMoreBtn(View view) {
smileMore.start();
}
public void introBtn(View view) {
intro.start();
}
}

Refresh WebView every 5 seconds

I want to reload the WebView every some seconds using timer or delay method
or even delay within a loop
this is my code
private WebView mWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.loadUrl("http://www.google.com");
}
Timer repeatTask = new Timer();
repeatTask.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
webview.loadUrl("yoururl");
}
});
}
}, 0, 5000);

Resources