0

i build a simple app with one layout and two buttons, and this is my code..

package com.example.tessound;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener
{

MediaPlayer player;
Button play,mute;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    play = (Button)findViewById(R.id.button1);
    play.setOnClickListener(this);
    mute = (Button)findViewById(R.id.button2);
    mute.setOnClickListener(this);
}

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

public void onClick(View view)
{
    if(view.getId()==R.id.button1)
    {
        playSound(1);
    }
    else if(view.getId()==R.id.button2)
    {
        playSound(2);
    }
}

public void playSound(int arg)
{
    try
    {
        if(player != null)
        {
            if (player.isPlaying()) 
            {
                player.stop();
                player.release();
            }
        }
    }
    catch(Exception e)
    {

    }

    if (arg == 1)
    {
        player = MediaPlayer.create(this, R.raw.atur);
    }
    else if (arg == 2)
    {
        player = MediaPlayer.create(this, R.raw.back);
    }

    if(player != null)
    {
        player.setLooping(false);
        player.start();
    }

    }
}

My raw folder contains: atur.mp3, back.mp3, belajar.mp3, level.mp3, skip.mp3, and start.mp3... While i click button mute, back.mp3 is played, but when i click button play, all of the mp3 in raw folder is played.. Any comments?

1
  • wts ur problem at first place ?
    – Sush
    Jan 4, 2014 at 4:25

2 Answers 2

0

I have copied your code and tested it. In my system it's work fine. I have just rename the songs and button id , because those raw songs I can't get. Please look this code, the code is same, because I just copied this code:

package com.mukherjee.satyaki.test;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity1 extends Activity implements OnClickListener
{

MediaPlayer player;
Button play,mute;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    play = (Button)findViewById(R.id.songid1);
    play.setOnClickListener(this);
    mute = (Button)findViewById(R.id.songid2);
    mute.setOnClickListener(this);
}

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

public void onClick(View view)
{
    if(view.getId()==R.id.songid1)
    {
        playSound(1);
    }
    else if(view.getId()==R.id.songid2)
    {
        playSound(2);
    }
}

public void playSound(int arg)
{
    try
    {
        if(player != null)
        {
            if (player.isPlaying()) 
            {
                player.stop();
                player.release();
            }
        }
    }
    catch(Exception e)
    {

    }

    if (arg == 1)
    {
        player = MediaPlayer.create(this, R.raw.ram1);
    }
    else if (arg == 2)
    {
        player = MediaPlayer.create(this, R.raw.ram3);
    }

    if(player != null)
    {
        player.setLooping(false);
        player.start();
    }

    }
}

You code is fine, as per my earlier assumption restart the emulator and wipe user data and then tests. I think the problem will be solved by your code.

Thanks.

2
  • i have restarted the emulator, clean, wipe user data, and it still played all the mp3 while i choose atur.mp3, level.mp3, skip.mp3.. when i choose back.mp3, belajar.mp3, and start.mp3, there was no problem.. :| Jan 4, 2014 at 5:45
  • But my dear friend I have used your code, it's give perfect result. Now I have no idea about this. Sorry friend. Jan 4, 2014 at 5:50
0

I remember having a similar issue, my code ended up looking like this:

if (!getmMediaPlayer().isPlaying()) {
    try {
        AssetFileDescriptor fd = getResources().openRawResourceFd(
                R.raw.snooze);
        getmMediaPlayer().setDataSource(fd.getFileDescriptor(),
                fd.getStartOffset(), fd.getLength());
        getmMediaPlayer().setAudioStreamType(
                AudioManager.STREAM_ALARM);
        getmMediaPlayer().setLooping(true);
        getmMediaPlayer().prepare();
        getmMediaPlayer().start();
    } catch (IllegalStateException e) {
        Log.e(TAG, e.toString());
    } catch (IOException e) {
        Log.e(TAG, e.toString());
    }
}

Specifically I believe it was the setDataSource that was important.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.