mayank_droid
mayank_droid

Reputation: 1015

Having problems in Playing mp3 files in android

I am getting problems in playing audio(mp3) files this music files are like click sounds its residing at the raw folder, the problem is if there are many clicks at random intervals it throws an exception of nullPointer. It occurs anywhere when the click is done and anytime, is it related to the memory issue or MediaPlayer related problem, pls any suggestion will be appreceated. Its simple media player object that i m calling, but its a games so on touch it plays the files, so in game i have many things to drag so i want a click sound at that time, sometime it works fine but when exceeds certain limit it throws null pointer exceptions. this is the code:

   MediaPlayer mp= MediaPlayer.create(context,R.raw.soun1);
   mp.start();

thats it:

Upvotes: 3

Views: 9624

Answers (4)

Kimmi
Kimmi

Reputation: 11

To play media player...we need two classes.. let us suppose mainactivity.java is our first file.. here we define two buttons - start_button & stop_button

mButton_start.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Intent mIntent=new Intent(MainActivity.this,maservice.class);
        startService(mIntent);

    }
});
mButton_stop.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Intent mIntent=new Intent(MainActivity.this,maservice.class);
        stopService(mIntent);

    }
});

maservice.java is our another java file. Here we define media player and also there should be 3 methods: onCreate(), onStart(), onDestroy().

Here is the code:

MediaPlayer mPlayer;
@Override
public void onCreate()
{
    super.onCreate();
    mPlayer=MediaPlayer.create(this, R.raw.kyun);
    mPlayer.setLooping(true);
}
@Override
public void onStart(Intent miIntent, int startid)
{
    super.onStart(miIntent, startid);
    mPlayer.start();

}

@Override
public void onDestroy()
{
    super.onDestroy();
    mPlayer.stop();
}

We also have to define these java files in manifest file

  • mainactivity.java is defined under activity tag
  • but maservice.java is defined under service tag

Upvotes: 1

nithinreddy
nithinreddy

Reputation: 6197

The issue is with the MP3 encoding. I tried with the same code, few work and few don't. So please try with a different one if it shows up the same error next time.

Upvotes: 0

mayank_droid
mayank_droid

Reputation: 1015

I got my answer, its SoundPool, especially created when the concern of game like application where the sound files are used continuously, so here we should use SoundPool except of MediaPlayer.

Upvotes: 0

Nikunj Patel
Nikunj Patel

Reputation: 22076

just try this ::

MediaPlayer mp = new MediaPlayer();
    mp= MediaPlayer.create(this,R.raw.soun1);
    mp.start();

permission in manifest file:::

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

Upvotes: 1

Related Questions