I tried to play an audio file from res/raw folder, here is the code, which I used to play the file, this is not generating any error and my device volume is max, but not getting any sound. I tried MP3 and WAV files.
public void btnPlay(View v) {
MediaPlayer mPlayer = MediaPlayer.create(con, R.raw.horse);
try {
mPlayer.start();
}catch (Exception e) {
e.printStackTrace();
Toast.makeText(con,e.toString(),Toast.LENGTH_SHORT).show();
}
}
I'm getting following message on LogCat after start
02-07 17:50:42.331: I/MediaPlayer(16345): Don't send intent. msg.arg1 = 0, msg.arg2 = 0
Help me to resolve this issue and it will be appreciated.
int resID = getResources().getIdentifier("name of sound" , "name of folder", getPackageName());
MediaPlayer mediaPlayer = MediaPlayer.create(getBaseContext(), resID);
mediaPlayer.start();
If you want an easy way to play audio from the raw folder, try this code:
import androidx.appcompat.app.AppCompatActivity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
public class Main2Activity extends AppCompatActivity {
MediaPlayer mplayer;
public void play(View view) {
mplayer.start();
}
public void pause(View view) {
mplayer.pause();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
mplayer =MediaPlayer.create(this, R.raw.ab);
}
}
Related
I wanna make "Play/Stop" button. When button "Play" is clicked, the song must be played and its text should be converted into "Stop" and when "Stop" is clicked, the button text should be changed into "Play" again and song should start again to play from the beginning.
import android.media.MediaPlayer;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
private Button btn_playStop;
private MediaPlayer mediaPlayer;
private boolean flag = false;
#Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getSupportActionBar();
actionBar.setTitle("Play Music");
mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.zara_sa);
btn_playStop = (Button)findViewById(R.id.btn_play_stop);
btn_playStop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mediaPlayer.isPlaying() && flag==true){
stopSong();
}
else if (flag == false){
playSong();
}
}
});
}
public void playSong(){
mediaPlayer.start();
btn_playStop.setText("Stop");
flag = true;
}
public void stopSong() {
mediaPlayer.stop();
btn_playStop.setText("Play");
flag = false;
}
}
You must call mediaPlayer.prepare(); if you called mediaPlayer.stop(); so in the first time you can just call mediaPlayer.start(); but in the next times you should call mediaPlayer.prepare(); before mediaPlayer.start();
public void playSong(){
try {
mediaPlayer.prepare();
} catch (IOException e) {
}
mediaPlayer.start();
btn_playStop.setText("Stop");
flag = true;
}
When i click a imgniatp at the display, audio play very well, but if i click imgbukap too, before audio of imgniatp finish, imgbukap also play audio. so both audio play together. How to play one sound at a time?
package sodik.com.ramadhan;
import sodik.com.ramadhan.R;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.Toast;
import android.widget.TextView;
public class Puasa3Activity extends Activity implements OnClickListener{
MediaPlayer mp;
ImageButton imgniatp1, imgbukap2;
String p1, p2, p3, p4, p5, p6, p7, p8, p9;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.puasa3);
TextView p1tk = (TextView) findViewById(R.id.p1tk);
TextView p2tk = (TextView) findViewById(R.id.p2tk);
imgniatp1=(ImageButton) findViewById(R.id.imgniatp);
imgniatp1.setOnClickListener(this);
imgbukap2=(ImageButton) findViewById(R.id.imgbukap);
imgbukap2.setOnClickListener(this);
}
public void onClick(View v){
switch (v.getId()){
case R.id.imgniatp:
mp= MediaPlayer.create(this, R.raw.bukapuasa);
mp.setVolume(1,1);
mp.start();
Toast.makeText(this, " Memainkan Buka Puasa", Toast.LENGTH_SHORT).show();
break;
case R.id.imgbukap:
mp = MediaPlayer.create(this, R.raw.niatpuasa);
mp.setVolume(1,1);
mp.start();
Toast.makeText(this, " Mainkan Niat Puasa ", Toast.LENGTH_SHORT).show();
break;
}
}
`
You could check to see if the MediaPlayer is already playing something, and start the clip in an onCompletionListener if it is
if(mp != null && mp.isPlaying()) {
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp = MediaPlayer.create(this,R.raw.bukapuasa);
mp.start();
}
});
} else {
mp = MediaPlayer.create(this,R.raw.bukapuasa);
mp.start();
}
package project.kalmas;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
public class one extends Activity {
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.one);
}
public void onclick2(View view)
{
Intent i=new Intent("project.two");
startActivity(i);
}
public void onclick3(View view)
{
MediaPlayer mp= MediaPlayer.create(this,R.raw.one);
if(mp.isPlaying()){
mp.stop();
} else {
mp.start();
}
}
}
When i click button it play sound then again i click button to stop it wont stop and play sound again which results in double sound playing at one time.Please help
You are creating a new MediaPlayer with every click, instead of keeping a reference to the first one. The MediaPlayer that is playing the sound is different than the MediaPlayer that you are calling isPlaying() on. You need to turn the mp variable into a field so you can keep a reference to it.
I imagine something like this would work:
public class one extends Activity {
MediaPlayer mp;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.one);
mp = MediaPlayer.create(this,R.raw.one);
}
public void onclick2(View view)
{
Intent i=new Intent("project.two");
startActivity(i);
}
public void onclick3(View view)
{
if(mp.isPlaying()){
mp.stop();
} else {
mp.start();
}
}
}
I'm new to android and I need it for a project I'm working on. I need it to play a sound by clicking a button. I followed online tutorials but my code does not play sounds at all and I'm getting an error on MediaPlayer Error(-19,0). I've tried a lot of the fixes I saw here and I can't seem to make it work. Any help? Here's the code
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
MediaPlayer player;
#Override
protected void onCreate(Bundle savedInstanceState) {
try {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AudioManager audioManager = (AudioManager) getSystemService(MainActivity.AUDIO_SERVICE);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 20, 0);
Button buttonHello = (Button) findViewById(R.id.button1);
buttonHello.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MediaPlayer mp = Medi aPlayer.create(MainActivity.this, R.raw.button);
mp.start();
mp.setOnCompletionListener(new OnCompletion Listener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
};
});
}
});
} catch (Exception e) {
System.out.println("Error!");
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
This links http://marakana.com/forums/android/examples/59.html has the great example to use MediaPlayer class to play song from raw folder.
To play song from files in the memory you can use the following code.
MediaPlayer mp = new MediaPlayer();
mp.reset();
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DATA, filename.getAbsolutePath());
Uri selectedImage=RingtoneDownload.this.getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
try {
mp.setDataSource(RingtoneDownload.this,selectedImage);
mp.prepare();
mp.start();
}catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
This code is working for me while set the source from file.
I'm just learning here. I'm trying to make a soundboard with around forty sounds, but I'm having some trouble how to get it to work using a maphash. Can anyone save me?
--------------soundboard-------------------------
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import java.util.HashMap;
import java.util.Map;
public class main extends Activity {
MediaPlayer mp=null;
\\\if I put put "MediaPlayer mp;" here it only plays one sound\\\
ImageButton Button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
map.put(R.id.button1, R.raw.sound1);
map.put(R.id.button2, R.raw.sound2);
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
mp = MediaPlayer.create(this, entry.getValue());
\\\if I put "final MediaPlayer mp = MediaPlayer.create(this, entry.getValue());" here I cant stop MediaPlayer with onpause and onstop overrides.\\\
ImageButton button = (ImageButton) findViewById(entry.getKey());
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mp.start();
}
});
}
}
#Override
protected void onStop() {
super.onStop();
if(mp.isPlaying()){
mp.stop();
mp.release();
}
}
#Override
public void onDestroy(){
super.onDestroy();
mp.release();
}
}
As I suggested in your previous question, don't create all those mediaplayer instances, for two reasons:
You lose all instances and have only the last one. always.
The onCreate() become very long method for no reason.
instead, remove mp = MediaPlayer.create(this, entry.getValue()); from your for loop, and move it to inside of your listener, something like that (not tested...):
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int sound = map.get(v.getId());
mp = MediaPlayer.create(main.this, sound);
mp.start();
}
});
So you would create the mediaplayer instance only when needed.
BTW, main is not a good name for a class.