I've written an application which plays a video until you tap the screen and it exits. A basic screensaver essentially.
The app will launch and play the video however it stops on the last frame rather than loops.
Code from my MainActivity.java below
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.VideoView;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
VideoView videoView = findViewById(R.id.videoView);
Uri uri=Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.hab2);
videoView.setVideoURI(uri);
videoView.requestFocus();
videoView.start();
videoView.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
finish();
}
}); }
}
You can handle easily if you use MediaPlayer
videoView.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
mediaPlayer.setLooping(true);
}
});
Related
Here is the code for the button which should start and pause the audio.
I have checked the button-text == "start" or "pause" and change the text and use the appropriate methods accordingly
i.e mediaplayer.start() and mediaplayer.pause().
But still, the audio won't play.
package com.example.demo;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity
{
MediaPlayer mediaPlayer;//Mediaplayer
Button button;
public void start(View view)
{
String text = button.getText().toString();
if (text == "start")
{
mediaPlayer.start(); //starting the audio
button.setText("pause");
}
else
{
mediaPlayer.pause(); //pausing the audio
button.setText("start");
}
}
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.start);
mediaPlayer = MediaPlayer.create(this,R.raw.music);
}
}
There are a few steps you should try:
First, try this because I think It gives always a false condition:
if(text.trim().equals("start"))
If Still not work Try this one:
public class MainActivity extends AppCompatActivity
{
MediaPlayer mediaPlayer;//Mediaplayer
Button button;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.start);
mediaPlayer = MediaPlayer.create(this,R.raw.music);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String text = button.getText().toString().trim();
if (text.equals("start"))
{
mediaPlayer.start();
button.setText("pause");
}
else
{
mediaPlayer.pause();
button.setText("start");
}
}
}
} }
I have this code:
import android.annotation.SuppressLint;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
public class MainActivity extends AppCompatActivity {
MediaPlayer mp;
//Buttons
ImageButton peroperoperopero;
ImageButton personajitosdos;
peroperoperopero = (ImageButton) findViewById(R.id.peroperoperopero);
personajitosdos = (ImageButton) findViewById(R.id.personajitosdos);
//code
peroperoperopero.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mp = MediaPlayer.create(MainActivity.this,R.raw.peroperopero);
mp.start();
}
});
peroperoperopero.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
Intent compartirAudio = new Intent(android.content.Intent.ACTION_SEND);
compartirAudio.setType("audio/*");
compartirAudio.putExtra(Intent.EXTRA_STREAM,
Uri.parse("android.resource://" + getApplicationContext().getPackageName() + "/raw/" + R.raw.peroperopero));
startActivity(Intent.createChooser(compartirAudio, "Compartir vía"));
return false;
}
});
personajitosdos.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mp = MediaPlayer.create(MainActivity.this,R.raw.unospersonajitos);
mp.start();
}
});
}
}
I would need to know what to modify to:
Pressing the button peroperoperopero share it in WhatsApp (currently when I share it, a document is sent but not the audio)
I want only one sound to play at the same time, now if I precede the two buttons at the same time the sounds are superimposed.
I also want that while the sound is playing, if I press the button again, it stops.
please tell me what do I have to change in the code? Thank you very much.
For audio play and stop use this following:
MediaPlayer mp;
mp = MediaPlayer.create(context, R.raw.sound_one);
mp.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.reset();
mp.release();
mp=null;
}
});
mp.start();
To share audio with what's app you can go to the following link
Sharing audio file
I'm a beginner and have developed a simple app which plays a sound when you click some button. The app runs just fine, but when i click it, there´s no sound.
Olá! Fiz um app que simplesmente reproduz um som ao clicar em um botão.
O app emula normalmente (abre no emulador), mas quando clico no botão para reproduzir um som, ele não funciona.
MainActivity.java
package mucap.esy.es.preplay;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public class BasicScreenActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button one = (Button)this.findViewById(R.id.button1);
final MediaPlayer mp = MediaPlayer.create(this, R.raw.justdoit);
one.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
mp.start();
}
});
}
}
}
I have everything set (file, button) but it won´t work with this code.
I found nothing in logcat, can someone help me?
please Update your MainActivity like this:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button one = (Button)this.findViewById(R.id.button1);
final MediaPlayer mp = MediaPlayer.create(this, R.raw.justdoit);
one.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
mp.start();
}
});
}}
Android app, i wrote to test image button doesn't work. I have created a image button and implement an event listener for that button. What's wrong with this source code?
import android.view.View;
import android.view.View.OnClickListener;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageButton;
import android.widget.Toast;
public class ImageButtonTestApp extends Activity {
ImageButton imageButton;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void eventListenerOnButton() {
imageButton = (ImageButton) findViewById(R.id.imageButton1);
imageButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(ImageButtonTestApp.this, "ImageButton is clicked!", Toast.LENGTH_SHORT).show();
}
});
}
}
u have written setOnClickListener in different method but u didnot call that method any where call that method in oncreate.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
eventListenerOnButton();
}
try this one,
Try it
imageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(ImageButtonTestApp.this, "ImageButton is clicked!", Toast.LENGTH_SHORT).show();
}
});
package my.app.bhaktamar;
import android.app.Activity;
//import android.content.Intent;
import android.media.MediaPlayer;
//import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
//import android.view.View;
public class MainActivity extends Activity {
MediaPlayer dontcall;
#Override
protected void onCreate(Bundle savedInstanceState) {
Log.e("Pickle", "onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
protected void onResume() {
Log.e("pickle", "onResume");
dontcall = MediaPlayer.create(this, R.raw.gpad1);
dontcall.start();
super.onResume();
}
#Override
protected void onPause() {
Log.e("pickle", "onPause");
dontcall.stop();
dontcall.release();
super.onPause();
}
}
What should i do so sound should play after autolock ?
use stop mediaplayer content in ondestroy() in your activity,so when your close activity after then the mediaplayer will stop.
#Override
public void onDestroy()
{
dontcall.stop();
dontcall.release();
super.onDestroy();
}
You need to use Service to play music background. If you want to play after autolock, use WakeLock.