0

So I've been trying to make a simple sound effect app for android. Here is the relevant code:

public static final String LOG_TAG = "BCA";

public MediaPlayer mp;

@Override
public void onCreate(Bundle savedInstanceState) 
{
        Log.v(LOG_TAG, "creating");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_list);

    mp = new MediaPlayer();
    mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
        Log.v(LOG_TAG, "set stream type");
    playSound();
}

public void playSound()
{
    try {
        mp.setDataSource("R.raw.sound1");
            Log.v(LOG_TAG, "set data source");
        mp.setOnPreparedListener(this);
        mp.setOnErrorListener(this);
        mp.prepareAsync();
            Log.v(LOG_TAG, "preparing");
    } 
    catch (IllegalArgumentException e) {
        e.printStackTrace();
    } 
    catch (IllegalStateException e) {
        e.printStackTrace();
    } 
    catch (IOException e) {
        e.printStackTrace();
    }
}

public void onPrepared(MediaPlayer mediaPlayer)
{ 
                Log.v(LOG_TAG, "finished preparing; starting");
    mp.start();
        Log.v(LOG_TAG, "started music");
}

public boolean onError(MediaPlayer mp, int e, int f)
{
        Log.v(LOG_TAG, "There was an error");
        Log.v(LOG_TAG, mp + " " + e + " " + f);
    mp.reset();
    return true;
}

Basically it gets to the set "set data source" tag but never finishes preparing. the error code is (1, 4) the 1 apparently being an unknown error. I have used multiple sound files, one of which I know works as the player works when just using the mp.create( etc... )

I'm not sure what's going on here

Thanks in advance

EDIT: So I followed the example of the link that Alexis Cartier gave and now there are no errors. However, the FileinputStream never finishes. The program just seems to stall. Here is the new code:

public void playMusic()
{
    File file = new File("R.raw.music1");
        Log.v(LOG_TAG, "set file");
    try {
            Log.v(LOG_TAG, "in try block");
        FileInputStream is = new FileInputStream(file);
            Log.v(LOG_TAG, "set file input stream");
        mp.setDataSource(is.getFD());
            Log.v(LOG_TAG, "set data source");
        mp.setOnPreparedListener(this);
        mp.setOnErrorListener(this);
            Log.v(LOG_TAG, "set on prepared/error listeners");
        mp.prepareAsync();
            Log.v(LOG_TAG, "preparing");
    } 
user1600704
  • 57
  • 1
  • 4

1 Answers1

0

See the answer of this question to modify your code : MediaPlayer.setDataSource causes IOException for valid file

But you can't do mp.setDataSource("R.raw.sound1");

Community
  • 1
  • 1
Alexis C.
  • 91,686
  • 21
  • 171
  • 177
  • Ok thanks! This makes sense. I seem to have another problem now. No errors, but the program just never finishes setting the FileInputStream. Here is the new code: – user1600704 Aug 16 '12 at 14:47
  • But what type of sound do you want to play ? If its shorts sounds, its better to use the SoundPool class : http://developer.android.com/reference/android/media/SoundPool.html – Alexis C. Aug 16 '12 at 14:49
  • Some will be longer... as in up to around 20-30 sec. I can get a log message before the `FileInputStream is = new FileInputStream(file);` line but it never finishes. The file is just the R.raw.music1 file – user1600704 Aug 16 '12 at 14:54
  • sorry, I realized I forgot to copy the code in... `File file = new File("R.raw.chewy"); Log.v(LOG_TAG, "set file"); try { Log.v(LOG_TAG, "in try block"); FileInputStream is = new FileInputStream(file); Log.v(LOG_TAG, "set file input stream"); mp.setDataSource(is.getFD()); Log.v(LOG_TAG, "set data source"); mp.setOnPreparedListener(this); mp.setOnErrorListener(this); Log.v(LOG_TAG, "set on prepared/error listeners"); mp.prepareAsync(); Log.v(LOG_TAG, "preparing");` hopefully still readable, sorry – user1600704 Aug 16 '12 at 15:15