Android Studio Share mp3 and play/stop songs - java

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

Related

Playing random sound when ImageButton is clicked on Android

I'm new in android and I'm trying to play random sound when ImageButton is clicked.
I'm still getting the same audio when I press the button. It should play different sounds every time the button is pressed. Also I would like to add a stop button later.
Here is my code:
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.media.MediaPlayer;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
import java.util.Random;
public
class MainActivity extends AppCompatActivity {
MediaPlayer mp;
ImageButton soundbutton;
int[] sounds = {R.raw.audi, R.raw.berlin, R.raw.bratanki, R.raw.budzik, R.raw.cztery, R.raw.drzyz, R.raw.dziewczyny, R.raw.emeryt, R.raw.enter, R.raw.faza};
Random r = new Random();
int rndm = r.nextInt();
#Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
//media player
soundbutton = (ImageButton)this.findViewById(R.id.playButton);
mp = MediaPlayer.create(getApplicationContext(), sounds[rndm]);
soundbutton.setOnClickListener(new View.OnClickListener() {
#Override public void onClick(View v) {
try {
if (mp.isPlaying()) {
mp.stop();
mp.release();
rndm = r.nextInt();
mp = MediaPlayer.create(getApplicationContext(), sounds[rndm]);
}
mp.start();
}
catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
Thank you for your help.
You need to give a maximum to your Random. You have 10 sounds so you want a number between 0-9.
rndm = r.nextInt(10);
This will give you a number between 0-9.

Problem in playing audio file using media player

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");
}
}
}
} }

How to loop a VideoView element in MainActivity.java?

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);
}
});

Runtime error in thread of music app

**Down here I am trying to build a music app in android studio,my app is working fine displaying all my SD card songs and play too ,all button works but when I click on next button it works normally but don't know why app sometimes gets stopped working and shows runtime exception ->>please open the image of run log of the application.
package com.example.ramandeepsingh.tunes;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.Toast;
import java.io.File;
import java.util.ArrayList;
import java.util.jar.Manifest;
public class PlayerActivity extends AppCompatActivity{
static MediaPlayer mp;//assigning memory loc once or else multiple songs will play at once
int position;
SeekBar sb;
ArrayList<File> mySongs;
Thread updateSeekBar;
Button pause,forward,reverse,next,previous;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player);
pause = (Button)findViewById(R.id.pause);
forward = (Button)findViewById(R.id.forward);
previous = (Button)findViewById(R.id.previous);
next = (Button)findViewById(R.id.next);
reverse = (Button)findViewById(R.id.reverse);
sb=(SeekBar)findViewById(R.id.seekBar);
updateSeekBar=new Thread(){
#Override
public void run(){
int totalDuration = mp.getDuration();
int currentPosition = 0;
// sb.setMax(totalDuration);
while(currentPosition < totalDuration){
try{
sleep(500);
currentPosition=mp.getCurrentPosition();
}
catch (InterruptedException e){
e.printStackTrace();
System.out.println(e);
}
sb.setProgress(currentPosition);
}
}
};
if(mp != null){
mp.stop();
mp.release();
}
Intent i = getIntent();
Bundle b = i.getExtras();
mySongs = (ArrayList) b.getParcelableArrayList("songs");
position = b.getInt("pos",0);
Uri u = Uri.parse(mySongs.get(position).toString());
mp = MediaPlayer.create(getApplicationContext(),u);
mp.start();
sb.setMax(mp.getDuration());
updateSeekBar.start();
sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
mp.seekTo(seekBar.getProgress());
}
});
pause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
sb.setMax(mp.getDuration());
if(mp.isPlaying()){
pause.setText(">");
mp.pause();
}
else {
pause.setText("||");
mp.start();
}
}
});
forward.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
sb.setMax(mp.getDuration());
mp.seekTo(mp.getCurrentPosition()+5000);
}
});
reverse.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
sb.setMax(mp.getDuration());
mp.seekTo(mp.getCurrentPosition()-5000);
}
});
next.setOnClickListener(new
View.OnClickListener() {
#Override
public void onClick(View v) {
mp.stop();
mp.reset();
position=((position+1)%mySongs.size());
Uri u = Uri.parse(mySongs.get( position).toString());
mp = MediaPlayer.create(getApplicationContext(),u);
mp.start();
sb.setMax(mp.getDuration());
updateSeekBar.start();
}
});
previous.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mp.stop();
mp.release();
position=((position-1)<0)?(mySongs.size()-1):(position-1);
Uri u = Uri.parse(mySongs.get( position).toString());//%mysongs so that it do not go to invalid position
mp = MediaPlayer.create(getApplicationContext(),u);
mp.start();
}
});
}}

Stop current audio file to prevent multiple files simultaneously

I'm trying to make a sort of soundboard app in Android Studio. For now I have three buttons: 2 of them play specific audio files and the other one stops the audio. Everything works except if you play one audio file while another one is going already they both play at the same time and the stop button no longer works on either of them. You have to exit the app. I cannot figure out how to get the first file to quit before playing the second. Here is my code so far:
package com.something.example.com;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
private Button startLie, startTruth,
stopScan;
private MediaPlayer play;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startLie = (Button) findViewById(R.id.startlie);
startLie.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
play = MediaPlayer.create(MainActivity.this, R.raw.lie_detector);
play.start();
play.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer play) {
play.release();
}
});
}
});
stopScan = (Button) findViewById(R.id.stopScan);
stopScan.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
play.stop();
};
});
startTruth = (Button) findViewById(R.id.starttruth);
startTruth.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
play = MediaPlayer.create(MainActivity.this, R.raw.truth_detector);
play.start();
play.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer play) {
play.release();
}
});
}
});
So I seem to have been able to finally get this to work. I basically created a stopPlaying function (placed at the end of my file. and then called it every time before the audio files play. Here's the code again, and this time with a couple more buttons.
package com.something.example;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
private Button startLie, startTruth,
startIdiot, startGenius, stopScan;
private MediaPlayer play;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startLie = (Button) findViewById(R.id.startlie);
startLie.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
stopPlaying();
play = MediaPlayer.create(MainActivity.this, R.raw.lie_detector);
play.start();
play.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer play) {
play.release();
}
});
}
});
stopScan = (Button) findViewById(R.id.stopScan);
stopScan.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
play.stop();
};
});
startTruth = (Button) findViewById(R.id.starttruth);
startTruth.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
stopPlaying();
play = MediaPlayer.create(MainActivity.this, R.raw.truth_detector);
play.start();
play.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer play) {
play.release();
}
});
}
});
startIdiot = (Button) findViewById(R.id.startidiot);
startIdiot.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
stopPlaying();
play = MediaPlayer.create(MainActivity.this, R.raw.idiot_detector);
play.start();
play.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer play) {
play.release();
}
});
}
});
startGenius = (Button) findViewById(R.id.startgenius);
startGenius.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
stopPlaying();
play = MediaPlayer.create(MainActivity.this, R.raw.genius_detector);
play.start();
play.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer play) {
play.release();
}
});
}
});
}
private void stopPlaying() {
if (play != null) {
play.stop();
play.release();
play = null;
}
}

Categories