No Sound Coming From Android Emulator | Android Studio - java

MediaPlayer player;
// Playing Sound
public void playSound(View v){
player = MediaPlayer.create(this, R.raw.song);
player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
player.start();
Log.e("start","player çalıştı");
}
});
player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
player.release();
Log.e("stop", "player durdu");
}
});
}
My code is here and with this code i do not get any sound from physical and virtual device.

Related

Only play one audio file at a time?

How do I make this to play only the sound I selected until I press another ImageButton so they don't sound over each other.
public class MainActivity extends AppCompatActivity {
MediaPlayer leaf,water,fire,wind;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
leaf=MediaPlayer.create(MainActivity.this,R.raw.forest);
water=MediaPlayer.create(MainActivity.this,R.raw.sea);
fire=MediaPlayer.create(MainActivity.this,R.raw.fire);
wind=MediaPlayer.create(MainActivity.this,R.raw.wind);
}
public void playleaf(View view ) {
leaf.setLooping(true);
leaf.start();
}
public void playwater(View view ) {
water.setLooping(true);
water.start();
water.start();
}
public void playfire(View view ) {
fire.setLooping(true);
fire.start();
}
public void playwind(View view ) {
wind.setLooping(true);
wind.start();
}
}
Do something like this.
MediaPlayer player;
public void playleaf(View view) {
stopMedia();
player = new MediaPlayer();
player.setDataSource(R.raw.leaf);
player.prepareAsync();
player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
player.setLooping(true);
player.start();
}
});
}
public void playwater(View view) {
stopMedia();
player = new MediaPlayer();
player.setDataSource(R.raw.leaf);
player.prepareAsync();
player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
player.setLooping(true);
player.start();
}
});
}
private void stopMedia() {
if (player != null) {
player.release();
player = null;
}
}
public void playfire(View view) {
stopMedia();
player = new MediaPlayer();
player.setDataSource(R.raw.leaf);
player.prepareAsync();
player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
player.setLooping(true);
player.start();
}
});
}
public void playwind(View view) {
stopMedia();
player = new MediaPlayer();
player.setDataSource(R.raw.leaf);
player.prepareAsync();
player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
player.setLooping(true);
player.start();
}
});
}

How to terminate music using back button of phone and created back button?

I was having some bugs with playing a music in my app. It wont stop playing even if I exit the app. It will only stop if I hit pause or terminate the app. Here's my code for music. What should I do code to stop it.
public void play_brain(View g){
final MediaPlayer brainMP = new MediaPlayer();
try {
brainMP.setDataSource("https://firebasestorage.googleapis.com/v0/b/capstone-katugna-001.appspot.com/o/meditation%20music%201%20minute%20peaceful%20music%201%20minute%20mindfulness%20meditation%201%20minute%20relax%20music%201.mp3?alt=media&token=f34fe7c7-54f1-4645-88cf-5e7a21f401d8");
brainMP.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
brainMP.prepare();
} catch (IOException e) {
e.printStackTrace();
}
pause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
brainMP.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.pause();
}
});
brainMP.pause();
}
});
}
You can try using this code:
public class MainActivity extends AppCompatActivity {
private MediaPlayer mediaPlayer;
private Button buttonStartPause;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
// Initialize your MediaPlayer object here
Uri myUri = ....; // initialize Uri here
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(getApplicationContext(), myUri);
mediaPlayer.prepare();
}
buttonStartPause.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (mediaplayer.isPlaying()) {
mediaplayer.pause();
// here you can set the play icon to your button
} else {
mediaplayer.start();
// here you can set the pause icon to your button
}
}
});
// To capture back button's pression you can override onBackPressed() in your Activity:
#Override
public void onBackPressed() {
if (mediaplayer.isPlaying()) {
mediaplayer.pause();
}
super.onBackPressed();
}
}

How to play mp in other java class than MainActivity?

I have two java classes: MainActivity and Explosion. I want to use this code in the Explosion java class. (this, R.raw.explosionsound) doesn't work and (Explosion.this, R.raw.explosionsound) also doesn't work.
How do I fix this?
MediaPlayer mp = null;
mp = MediaPlayer.create(this, R.raw.explosionsound);
if (mp != null) {
mp.stop();
mp.release();
}
mp.start();
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.stop();
mp.release();
}
});
If Explosion is not an activity then you must supply a Context to instantiate the MediaPlayer object.
Explosion explosion = new Explosion(context);
explosion.play();
class Explosion{
Context context;
public Explosion(Context context){
this.context = context;
}
public void play(){
MediaPlayer mp = MediaPlayer.create(context, R.raw.explosionsound);
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.stop();
mp.release();
}
});
mp.start();
}
}
Also as it was allready pointed out you can't release the MediaPlayer object and then call start();

Audio player with listview

i have audio files when i'm play another file then previous file play continuously.
so if you have solution please let me know.
here is my main.java
public class MusicAndroidActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
buttonPlay = (Button) findViewById(R.id.play);
music = (ListView)findViewById(R.id.music);
// music.setAdapter(new ArrayAdapter<String>(this,, str));
music.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
switch (position){
case 0:
MediaPlayer mp = MediaPlayer.create(getApplicationContext(),R.raw.hosannatamil);
mp.start();
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
break;
case 1:
MediaPlayer mp1 = MediaPlayer.create(getApplicationContext(),R.raw.one_less);
mp1.start();
mp1.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
break;
case 2:
MediaPlayer mp2 = MediaPlayer.create(getApplicationContext(),R.raw.words);
mp2.start();
mp2.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
break;
}
}
});
}
You need to manually call MediaPlayer.stop() if you want to stop the playback.
If you want to start the same MediaPlayer again you have to call .prepare() before that.
For more information read the reference page and look at the state diagram there.
if(mediaPlayer.isPlaying())
{
//stop or pause your media player mediaPlayer.stop(); or mediaPlayer.pause();
mediaPlayer.pause();
}
else
{
mediaPlayer.start();
}
Put this Condition inside switch statements before mediaPlayer.start() also create a global reference of Media Player instead of creating three references like MediaPlayer mediaPlayer instead of MediaPlayer mp,mp1,mp2 etc,because you can create media Player object in the same reference as one condition must be true all the time,also put a default condition in the switch statement!

NullPointerException with android 2.3.3 mediaplayer

This is just a simple android app I am developing, it's meant to play a sound every time you click the button....it works when I click the button at a slow pace, but always crashes if I click the button at a fast pace due to a runtime error - NullPointerException!.....I don't know what I am doing wrong.
public class OSDC_Mini_ProjectActivity extends Activity implements MediaPlayer.OnCompletionListener {
private ImageButton play,pause,stop;
private MediaPlayer mp;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
play = (ImageButton)findViewById(R.id.play);
pause = (ImageButton)findViewById(R.id.pause);
stop = (ImageButton)findViewById(R.id.stop);
play.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
play();
}
});
pause.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
pause();
}
});
stop.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
stop();
}
});
setup();
}
#Override
public void onDestroy() {
super.onDestroy();
if (stop.isEnabled()) {
mp.stop();
}
}
public void onCompletion(MediaPlayer mp) {
stop();
}
private void play() {
mp.start();
play.setEnabled(false);
pause.setEnabled(true);
stop.setEnabled(true);
}
private void stop() {
mp.stop();
pause.setEnabled(false);
stop.setEnabled(false);
try {
mp.prepare();
mp.seekTo(0);
play.setEnabled(true);
}
catch (Throwable t) {
error(t);
}
}
private void pause() {
mp.pause();
play.setEnabled(true);
pause.setEnabled(false);
stop.setEnabled(true);
}
private void loadClip() {
try {
mp=MediaPlayer.create(this, R.raw.test);
mp.setOnCompletionListener(this);
}
catch (Throwable t) {
error(t);
}
}
private void setup() {
loadClip();
play.setEnabled(true);
pause.setEnabled(false);
stop.setEnabled(false);
}
private void error(Throwable t) {
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder
.setTitle("報錯啦!")
.setMessage(t.toString())
.setPositiveButton("確定", null)
.show();
}
}
Add null check in every method. Also start media play only if mp is currently not playing anything

Categories