I'm trying to make my application play a sound when I click on the picture drawn in the Array. What I want to do is play a different sound when you click a different picture.
This my Java code so far:
public class gorendis extends Activity implements MediaPlayer.OnCompletionListener{
private ImageView play;
private ImageView a2;
private MediaPlayer mp;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.huruf);
play=(ImageView)findViewById(R.id.a1);
a2=(ImageView)findViewById(R.id.a2);
//gambar=(ImageButton)findViewById(R.id.gambar);
//gambar.setImageResource(R.drawable.a1);
play.setOnClickListener( new View.OnClickListener() {
public void onClick(View view) {
play();
}
});
a2.setOnClickListener( new View.OnClickListener() {
public void onClick(View view) {
play();
}
});
setup();
}
#Override
public void onDestroy() {
super.onDestroy();
}
public void onCompletion(MediaPlayer mp) {
stop();
}
private void stop() {
// TODO Auto-generated method stub
}
private void play() {
mp.start();
play.setEnabled(true);
}
private void loadClip() {
try {
mp=MediaPlayer.create(this, R.raw.doajodoh);
mp.setOnCompletionListener(this);
} catch (Throwable t) {
goBlooey(t);
}
}
private void setup() {
loadClip();
play.setEnabled(true);
}
private void goBlooey(Throwable t) {
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setTitle("Exception!")
.setMessage(t.toString())
.setPositiveButton("OK", null)
.show();
}
}
Main layout XML:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView>
<LinearLayout>
<ImageView
android:id="#+id/play"
android:layout_width="wrap_content"
android:layout_height="53px"
android:background="#android:color/transparent"
android:scaleType="fitCenter"
android:src="#drawable/a1" >
</ImageView>
<ImageView
android:id="#+id/a2"
android:layout_width="wrap_content"
android:layout_height="53px"
android:background="#android:color/transparent"
android:scaleType="fitCenter"
android:src="#drawable/a2" >
</ImageView>
</LinearLayout>
</ScrollView>
If you simply want to play different sounds on buttonclick, then i would advise this:
Use the setTag() method of your buttons. Like button1.setTag(0);
or button1.setTag("guitarmusic");
You will notice that the View.OnClickListener's onClick() method
gets a View as parameter.
You know, that you set this OnClickListener only to buttons, so you
can cast this View in the onClick() method to Button like
Button clicked = (Button) view;
Then you can access the tag, you set earlier to your buttons, and
cast them too, like: Integer buttonTag = (Integer)
clicked.getTag();
Then you can decide in a simple switch, wich sound you want to
play, like: switch(buttonTag){case 0: play first sound... break;
...}
This way you wont need a separate OnClickListener to every sound you want to play :)
finaly I found this sollution..
THIS WORK AND EVEN WITH THE LONG CODE !!
public class part1 extends Activity implements MediaPlayer.OnCompletionListener{
/** Called when the activity is first created. */
private ImageView a2;
private ImageView a3;
private ImageView a4;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
a2=(ImageView)findViewById(R.id.a2);
a3=(ImageView)findViewById(R.id.a3);
a4=(ImageView)findViewById(R.id.a4);
a2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
a2();
}
});
setup();
a3.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
a3();
}
});
setup2();
a4.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
a4();
}
});
setup3();
public void onCompletion(MediaPlayer mp) {
stop();
}
private void stop() {
mp.stop();
}
private void a2() {
mp.stop();
loadClip();
mp.start();
a2.setEnabled(true);
}
private void a3() {
mp.stop();
loadClip2();
mp.start();
a3.setEnabled(true);
}
private void a4() {
mp.stop();
loadClip3();
mp.start();
a4.setEnabled(true);
}
private void loadClip() {
try {
mp=MediaPlayer.create(this, R.raw.a2);
mp.setOnCompletionListener(this);
}
catch (Throwable t) {
goBlooey(t);
}
}
private void loadClip2() {
try {
mp=MediaPlayer.create(this, R.raw.a3);
mp.setOnCompletionListener(this);
}
catch (Throwable t) {
goBlooey(t);
}
}
private void loadClip3() {
try {
mp=MediaPlayer.create(this, R.raw.a4);
mp.setOnCompletionListener(this);
}
catch (Throwable t) {
goBlooey(t);
}
}
private void setup() {
loadClip();
a2.setEnabled(true);
}
private void setup2() {
loadClip2();
a3.setEnabled(true);
}
private void setup3() {
loadClip3();
a4.setEnabled(true);
}
private void goBlooey(Throwable t) {
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder
.setTitle("Exception!")
.setMessage(t.toString())
.setPositiveButton("OK", null)
.show();
}
Related
This is my second activity and when open this activity threw button activity take load and open after some time and some times UI Freeze and some time black screen open for some seconds
public class AartiActivity extends AppCompatActivity {
final MediaPlayer player1 = new MediaPlayer();
private Button btn_playarti, btn_pausearti, btn_stoparti;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_aarti);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
btn_playarti = (Button) findViewById(R.id.btn_playarti);
btn_pausearti = (Button) findViewById(R.id.btn_pausearti);
btn_stoparti = (Button) findViewById(R.id.btn_stoparti);
try {
player1.setDataSource(STRING_URL);
player1.prepare();
} catch (IOException e) {
e.printStackTrace();
}
btn_playarti.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
player1.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer player) {
player.stop();
}
});
player1.start();
}
});
btn_pausearti.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (player1.isPlaying()) {
player1.pause();
}
}
});
btn_stoparti.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
player1.stop();
}
});
}
#Override
protected void onStop() {
super.onStop();
player1.stop();
}
}
I have two ImageViews with different sounds which plays sound when clicked on one of them. When click on the first ImageView I want the sound to play and before the first ImageView finish playing the sound, if the second ImageView is click, I want to restrict the second ImageView to play a sound because the first ImageView is still playing the sound. In fact, I want to restrict a sound to play when another sound is playing. But, I didn't get the desired result.
public class Transportation extends AppCompatActivity {
private MediaPlayer sound,sound1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.transportation);
sound = MediaPlayer.create(this,R.raw.vatunabus);
final ImageView imageView= (ImageView)findViewById(R.id.vatunabussound);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(sound1.isPlaying()){
sound.stop();
sound.release();
}
else if(sound.isPlaying()){
sound.pause();
imageView.setBackgroundResource(R.mipmap.playicon);
}
else {
sound.start();
imageView.setBackgroundResource(R.mipmap.pauseicon);
}
}
});
sound.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
imageView.setBackgroundResource(R.mipmap.playicon);
}
});
sound1 = MediaPlayer.create(this,R.raw.varowlsanabus);
final ImageView imageView1 = (ImageView)findViewById(R.id.firstbussound);
imageView1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(sound.isPlaying()){
sound1.stop();
sound1.release();
}
else if(sound1.isPlaying()){
sound1.pause();
imageView1.setBackgroundResource(R.mipmap.playicon);
}
else {
sound1.start();
imageView1.setBackgroundResource(R.mipmap.pauseicon);
}
}
});
sound1.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
imageView1.setBackgroundResource(R.mipmap.playicon);
}
});
}
}
Here's an idea, let's create a boolean value to tell us if a sound is currently playing.
private MediaPlayer sound,sound1;
private boolean soundPlaying = false; //new boolean variable
Next, before playing a sound, we'll check to make sure it's still false:
public void onClick(View v) {
//Add a check that exits the function if a sound is playing
//Add this check in both onClick methods
if(soundPlaying){
return;
}
...
}
Also, after sound.start() and sound1.start(), let's set the boolean to true:
soundPlaying = true;
Finally, in both instances of the onCompletion methods, let's reset the boolean to false:
public void onCompletion(MediaPlayer mp) {
soundPlaying = false;
...
}
The final code will look like this:
public class Transportation extends AppCompatActivity {
private MediaPlayer sound,sound1;
private boolean soundPlaying = false; //new boolean variable
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.transportation);
sound = MediaPlayer.create(this,R.raw.vatunabus);
final ImageView imageView= (ImageView)findViewById(R.id.vatunabussound);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Add a check that exits the function if a sound is playing
if(soundPlaying){
return;
}
if(sound1.isPlaying()){
sound.stop();
sound.release();
}
else if(sound.isPlaying()){
sound.pause();
imageView.setBackgroundResource(R.mipmap.playicon);
}
else {
sound.start();
imageView.setBackgroundResource(R.mipmap.pauseicon);
}
}
});
sound.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
soundPlaying = false;
imageView.setBackgroundResource(R.mipmap.playicon);
}
});
sound1 = MediaPlayer.create(this,R.raw.varowlsanabus);
final ImageView imageView1 = (ImageView)findViewById(R.id.firstbussound);
imageView1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Add a check that exits the function if a sound is playing
if(soundPlaying){
return;
}
if(sound.isPlaying()){
sound1.stop();
sound1.release();
}
else if(sound1.isPlaying()){
sound1.pause();
imageView1.setBackgroundResource(R.mipmap.playicon);
}
else {
sound1.start();
imageView1.setBackgroundResource(R.mipmap.pauseicon);
}
}
});
sound1.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
soundPlaying = false;
imageView1.setBackgroundResource(R.mipmap.playicon);
}
});
}
}
I hope this helps. Couldn't try it in live code though, so let me know how it goes.
How to solve java.lang.IllegalStateException?
This happens when moving to another activity
this logcat :
05-24 01:48:07.312 25905-25905/andie16n.abc_ind_english E/MediaPlayer:
prepareAsync called in state 8, mPlayer(0x9a318160)
this java code :
package andie16n.abc_ind_english;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
/**
* Created by andie on 11/05/2017.
*/
public class Abc_play_all extends Activity implements MediaPlayer.OnCompletionListener {
private ImageButton tombol_play;
private ImageButton tombol_play_a;
private ImageButton tombol_play_b;
private ImageButton tombol_play_c;
private ImageButton tombol_play_d;
private ImageButton tombol_play_e;
private ImageButton tombol_play_f;
private ImageButton tombol_play_g;
private ImageButton tombol_play_h;
private ImageButton tombol_play_i;
private ImageButton tombol_play_j;
private ImageButton tombol_play_k;
private ImageButton tombol_stop;
private MediaPlayer MPlayer;
private MediaPlayer MPlayer_a;
private MediaPlayer MPlayer_b;
private MediaPlayer MPlayer_c;
private MediaPlayer MPlayer_d;
private MediaPlayer MPlayer_e;
private MediaPlayer MPlayer_f;
private MediaPlayer MPlayer_g;
private MediaPlayer MPlayer_h;
private MediaPlayer MPlayer_i;
private MediaPlayer MPlayer_j;
private MediaPlayer MPlayer_k;
private MediaPlayer MPlayer_back;
private ImageButton back_abc_home_play_all;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.abc_play_all);
tombol_play_a = (ImageButton) findViewById(R.id.Btn_a_play);
tombol_play_b = (ImageButton) findViewById(R.id.Btn_b_play);
tombol_play_c = (ImageButton) findViewById(R.id.Btn_c_play);
tombol_play_d = (ImageButton) findViewById(R.id.Btn_d_play);
tombol_play_e = (ImageButton) findViewById(R.id.Btn_e_play);
tombol_play_f = (ImageButton) findViewById(R.id.Btn_f_play);
tombol_play_g = (ImageButton) findViewById(R.id.Btn_g_play);
tombol_play_h = (ImageButton) findViewById(R.id.Btn_h_play);
tombol_play_i = (ImageButton) findViewById(R.id.Btn_i_play);
tombol_play_j = (ImageButton) findViewById(R.id.Btn_j_play);
tombol_play_k = (ImageButton) findViewById(R.id.Btn_k_play);
tombol_play = (ImageButton) findViewById(R.id.Btn_play_all_abc);
tombol_stop = (ImageButton) findViewById(R.id.Btn_stop_all_abc);
tombol_stop.setEnabled(false);
aktif();
tombol_play_a.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
play_a();
}
});
tombol_play_b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
play_b();
}
});
tombol_play_c.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
play_c();
}
});
tombol_play_d.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
play_d();
}
});
tombol_play_e.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
play_e();
}
});
tombol_play_f.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
play_f();
}
});
tombol_play_g.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
play_g();
}
});
tombol_play_h.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
play_h();
}
});
tombol_play_i.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
play_i();
}
});
tombol_play_j.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
play_j();
}
});
tombol_play_k.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
play_k();
}
});
tombol_play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
play_abc();
}
});
tombol_stop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
MPlayer.stop();
try {
MPlayer.prepare();
MPlayer.seekTo(0);
} catch (Throwable t) {
error_(t);
}
stop_all();
}
});
}
#Override
public void onResume() {
super.onResume();
stop_back();
play_back();
}
private void TombolIsEnabled() {
tombol_play.setEnabled(true);
tombol_play_a.setEnabled(true);
tombol_play_b.setEnabled(true);
tombol_play_c.setEnabled(true);
tombol_play_d.setEnabled(true);
tombol_play_e.setEnabled(true);
tombol_play_f.setEnabled(true);
tombol_play_g.setEnabled(true);
tombol_play_h.setEnabled(true);
tombol_play_i.setEnabled(true);
tombol_play_j.setEnabled(true);
tombol_play_k.setEnabled(true);
}
private void play_a() {
MPlayer_a.stop();
try {
MPlayer_a.prepare();
MPlayer_a.seekTo(0);
} catch (Throwable t) {
error_(t);
}
stop_all();
MPlayer_a.start();
tombol_play_a.setImageResource(R.drawable.a_menu_2);
tombol_play_a.setEnabled(false);
tombol_stop.setEnabled(true);
}
private void play_b() {
MPlayer_b.stop();
try {
MPlayer_b.prepare();
MPlayer_b.seekTo(0);
} catch (Throwable t) {
error_(t);
}
stop_all();
MPlayer_b.start();
tombol_play_b.setImageResource(R.drawable.b_menu_2);
tombol_play_b.setEnabled(false);
tombol_stop.setEnabled(true);
}
private void play_c() {
MPlayer_c.stop();
try {
MPlayer_c.prepare();
MPlayer_c.seekTo(0);
} catch (Throwable t) {
error_(t);
}
stop_all();
MPlayer_c.start();
tombol_play_c.setImageResource(R.drawable.c_menu_2);
tombol_play_c.setEnabled(false);
tombol_stop.setEnabled(true);
}
private void play_d() {
MPlayer_d.stop();
try {
MPlayer_d.prepare();
MPlayer_d.seekTo(0);
} catch (Throwable t) {
error_(t);
}
stop_all();
MPlayer_d.start();
tombol_play_d.setImageResource(R.drawable.d_menu_2);
tombol_play_d.setEnabled(false);
tombol_stop.setEnabled(true);
}
private void play_e() {
MPlayer_e.stop();
try {
MPlayer_e.prepare();
MPlayer_e.seekTo(0);
} catch (Throwable t) {
error_(t);
}
stop_all();
MPlayer_e.start();
tombol_play_e.setImageResource(R.drawable.e_menu_2);
tombol_play_e.setEnabled(false);
tombol_stop.setEnabled(true);
}
private void play_f() {
MPlayer_f.stop();
try {
MPlayer_f.prepare();
MPlayer_f.seekTo(0);
} catch (Throwable t) {
error_(t);
}
stop_all();
MPlayer_f.start();
tombol_play_f.setImageResource(R.drawable.f_menu_2);
tombol_play_f.setEnabled(false);
tombol_stop.setEnabled(true);
}
private void play_g() {
MPlayer_g.stop();
try {
MPlayer_g.prepare();
MPlayer_g.seekTo(0);
} catch (Throwable t) {
error_(t);
}
stop_all();
MPlayer_g.start();
tombol_play_g.setImageResource(R.drawable.g_menu_2);
tombol_play_g.setEnabled(false);
tombol_stop.setEnabled(true);
}
private void play_h() {
MPlayer_h.stop();
try {
MPlayer_h.prepare();
MPlayer_h.seekTo(0);
} catch (Throwable t) {
error_(t);
}
stop_all();
MPlayer_h.start();
tombol_play_h.setImageResource(R.drawable.h_menu_2);
tombol_play_h.setEnabled(false);
tombol_stop.setEnabled(true);
}
private void play_i() {
MPlayer_i.stop();
try {
MPlayer_i.prepare();
MPlayer_i.seekTo(0);
} catch (Throwable t) {
error_(t);
}
stop_all();
MPlayer_i.start();
tombol_play_i.setImageResource(R.drawable.i_menu_2);
tombol_play_i.setEnabled(false);
tombol_stop.setEnabled(true);
}
private void play_j() {
MPlayer_j.stop();
try {
MPlayer_j.prepare();
MPlayer_j.seekTo(0);
} catch (Throwable t) {
error_(t);
}
stop_all();
MPlayer_j.start();
tombol_play_j.setImageResource(R.drawable.j_menu_2);
tombol_play_j.setEnabled(false);
tombol_stop.setEnabled(true);
}
private void play_k() {
MPlayer_k.stop();
try {
MPlayer_k.prepare();
MPlayer_k.seekTo(0);
} catch (Throwable t) {
error_(t);
}
stop_all();
MPlayer_k.start();
tombol_play_k.setImageResource(R.drawable.k_menu_2);
tombol_play_k.setEnabled(false);
tombol_stop.setEnabled(true);
}
#Override
public void onDestroy() {
super.onDestroy();
if (tombol_stop.isEnabled()) {
stop_all();
}
}
#Override
public void onCompletion(MediaPlayer MP) {
stop_all();
}
private void play_abc() {
MPlayer_a.stop();
MPlayer_b.stop();
MPlayer_c.stop();
MPlayer_d.stop();
MPlayer_e.stop();
MPlayer_f.stop();
MPlayer_g.stop();
MPlayer_h.stop();
MPlayer_i.stop();
MPlayer_j.stop();
MPlayer_k.stop();
MPlayer.stop();
try {
MPlayer.prepare();
MPlayer.seekTo(0);
} catch (Throwable t) {
error_(t);
}
stop_all();
MPlayer.setLooping(true);
MPlayer.start();
tombol_play.setEnabled(false);
tombol_stop.setEnabled(true);
tombol_play_a.setEnabled(false);
tombol_play_b.setEnabled(false);
tombol_play_c.setEnabled(false);
tombol_play_d.setEnabled(false);
tombol_play_e.setEnabled(false);
tombol_play_f.setEnabled(false);
tombol_play_g.setEnabled(false);
tombol_play_h.setEnabled(false);
tombol_play_i.setEnabled(false);
tombol_play_j.setEnabled(false);
tombol_play_k.setEnabled(false);
}
private void stop_all() {
tombol_stop.setEnabled(false);
tombol_play_a.setImageResource(R.drawable.a_menu);
tombol_play_b.setImageResource(R.drawable.b_menu);
tombol_play_c.setImageResource(R.drawable.c_menu);
tombol_play_d.setImageResource(R.drawable.d_menu);
tombol_play_e.setImageResource(R.drawable.e_menu);
tombol_play_f.setImageResource(R.drawable.f_menu);
tombol_play_g.setImageResource(R.drawable.g_menu);
tombol_play_h.setImageResource(R.drawable.h_menu);
tombol_play_i.setImageResource(R.drawable.i_menu);
tombol_play_j.setImageResource(R.drawable.j_menu);
tombol_play_k.setImageResource(R.drawable.k_menu);
try {
TombolIsEnabled();
} catch (Throwable t) {
error_(t);
}
}
private void aktif() {
try {
MPlayer = MediaPlayer.create(this, R.raw.abc);
MPlayer.setOnCompletionListener(this);
MPlayer_a = MediaPlayer.create(this, R.raw.a);
MPlayer_a.setOnCompletionListener(this);
MPlayer_b = MediaPlayer.create(this, R.raw.b);
MPlayer_b.setOnCompletionListener(this);
MPlayer_c = MediaPlayer.create(this, R.raw.c);
MPlayer_c.setOnCompletionListener(this);
MPlayer_d = MediaPlayer.create(this, R.raw.d);
MPlayer_d.setOnCompletionListener(this);
MPlayer_e = MediaPlayer.create(this, R.raw.e);
MPlayer_e.setOnCompletionListener(this);
MPlayer_f = MediaPlayer.create(this, R.raw.f);
MPlayer_f.setOnCompletionListener(this);
MPlayer_g = MediaPlayer.create(this, R.raw.g);
MPlayer_g.setOnCompletionListener(this);
MPlayer_h = MediaPlayer.create(this, R.raw.h);
MPlayer_h.setOnCompletionListener(this);
MPlayer_i = MediaPlayer.create(this, R.raw.i);
MPlayer_i.setOnCompletionListener(this);
MPlayer_j = MediaPlayer.create(this, R.raw.j);
MPlayer_j.setOnCompletionListener(this);
MPlayer_k = MediaPlayer.create(this, R.raw.k);
MPlayer_k.setOnCompletionListener(this);
MPlayer_back = MediaPlayer.create(this, R.raw.backsound);
} catch (Throwable t) {
error_(t);
}
TombolIsEnabled();
}
private void play_back() {
if (MPlayer_back != null && !MPlayer_back.isPlaying()) {
MPlayer_back.setLooping(true);
MPlayer_back.start();
} else {
MPlayer_back.setLooping(true);
}
}
private void stop_for_back() {
MPlayer_back.stop();
try {
MPlayer_back.prepare();
MPlayer_back.seekTo(0);
} catch (Throwable t) {
error_(t);
}
}
private void stop_back() {
if (MPlayer_back.isPlaying()) {
stop_for_back();
MPlayer_back.reset();
}
}
private void error_(Throwable t) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Failed!").setMessage(t.toString())
.setPositiveButton("OK", null).show();
}
public void btn_back_all_abc_cliked(View v) {
stop_for_back();
Intent panggil_class = new Intent(this, Abc.class);
startActivity(panggil_class);
MPlayer_a.reset();
MPlayer_b.reset();
MPlayer_c.reset();
MPlayer_d.reset();
MPlayer_e.reset();
MPlayer_f.reset();
MPlayer_g.reset();
MPlayer_h.reset();
MPlayer_i.reset();
MPlayer_j.reset();
MPlayer_k.reset();
}
#Override
public void onBackPressed() {
if (MPlayer.isPlaying() || MPlayer_back.isPlaying()) {
stop_back();
MPlayer.reset();
}
finish();
}
}
No need to prepare the player in case of the raw file
var mediaPlayer: MediaPlayer? = MediaPlayer.create(context, R.raw.sound_file_1)
mediaPlayer?.start() // no need to call prepare(); create() does that for you
I met the same mistake when I want to play a video using SurfaceView and MediaPlayer.
mMediaPlayer.prepareAsync();
mMediaPlayer.setDisplay(mSurfaceView.getHolder());
mMediaPlayer.start();
I solve the problem through the following code:
mMediaPlayer.setDisplay(mSurfaceView.getHolder());
mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mMediaPlayer.start();
}
});
But I don't know what the cause of problem is....
I want to add a SeekBar to control my mediaplayer process, but i do not know how to add code.Please help me, and give some example in my project code!
package lamTo.OSDC_Project;
public class OSDC_Mini_ProjectActivity extends Activity implements MediaPlayer.OnCompletionListener {
private ImageButton play,pause,stop;
private MediaPlayer mp;
private SeekBar seekbar;
#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.bigbang);
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("Error!")
.setMessage(t.toString())
.setPositiveButton("Yes", null)
.show();
}
}
In your design layout, add this
<SeekBar
android:id="#+id/SeekBar01"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
/>
In your onCreate() add this
seekBar = (SeekBar) findViewById(R.id.SeekBar01);
seekBar.setMax(mPlayer.getDuration());
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
seekChange(seekBar, progress, fromUser);
}
});
}
// This makes the seekbar follow the music
public void startPlayProgressUpdater() {
seekBar.setProgress(mPlayer.getCurrentPosition());
if (mPlayer.isPlaying()) {
Runnable notification = new Runnable() {
public void run() {
startPlayProgressUpdater();
}
};
handler.postDelayed(notification,1000);
}else{
mPlayer.pause();
buttonPlayStop.setText(getString(R.string.play_str));
seekBar.setProgress(0);
}
}
// This is event handler thumb moving event
private void seekChange(SeekBar seekBar, int progress,
boolean fromUser){
mPlayer.seekTo(progress);
}
Complete Tutorial is here with source code.
I have been trying to add sound to my alphabet android application, but I don't seem to have been successful. Below is my code.
public class Sound extends Activity implements OnCompletionListener {
/** Called when the activity is first created. */
private ImageView b;
private ImageView t;
private ImageView j;
private MediaPlayer mp;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.alphabet);
b=(ImageView)findViewById(R.drawable.b);
t=(ImageView)findViewById(R.drawable.t);
j=(ImageView)findViewById(R.drawable.j);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
b();
}
});
setup();
t.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
t();
}
});
setup2();
j.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
j();
}
});
setup3();
}
public void onCompletion(MediaPlayer mp) {
stop();
}
private void stop() {
mp.stop();
}
private void b() {
mp.stop();
loadClip();
mp.start();
b.setEnabled(true);
}
private void t() {
mp.stop();
loadClip2();
mp.start();
t.setEnabled(true);
}
private void j() {
mp.stop();
loadClip3();
mp.start();
j.setEnabled(true);
}
private void loadClip() {
try {
mp=MediaPlayer.create(this, R.raw.b);
mp.setOnCompletionListener(this);
}
catch (Throwable t) {
goBlooey(t);
}
}
private void loadClip2() {
try {
mp=MediaPlayer.create(this, R.raw.t);
mp.setOnCompletionListener(this);
}
catch (Throwable t) {
goBlooey(t);
}
}
private void loadClip3() {
try {
mp=MediaPlayer.create(this, R.raw.j);
mp.setOnCompletionListener(this);
}
catch (Throwable t) {
goBlooey(t);
}
}
private void setup() {
loadClip();
b.setEnabled(true);
}
private void setup2() {
loadClip2();
t.setEnabled(true);
}
private void setup3() {
loadClip3();
j.setEnabled(true);
}
private void goBlooey(Throwable t) {
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder
.setTitle("Exception!")
.setMessage(t.toString())
.setPositiveButton("OK", null)
.show();
}
}
Would you please be so kind to take a look on my code and let me know where or what is missing.
All the images are in the drawable and all clips are in the raw.
Regards.
A quick skim through your code and I would say your mp doesn't exist in order to be stopping it before loading another clip on the first run.
At the top put
private MediaPlayer mp=null;
then on every line where it says mp.stop(); change it to if(mp!=null) mp.stop();