0

I have been trying to play an MP3 file. It just won't work.

I did manage to play it and add all sorts of functionalities to it with the very same/similar code a few days ago. Just tried to make it all over again and now these errors...

Can someone point out the error also why does this happen.

Just to clarify I used different formats of URI, none of them work. Thought maybe its the prepare method.

package com.player.phoneagent.gui;


import java.io.IOException;

import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;


public class AndroidClientActivity extends Activity {
    /** Called when the activity is first created. */

    private MediaPlayer mp;
    String path = "android.resource://com.player.phoneagent/raw/test";

    ImageButton btn_prev;
    ImageButton btn_play;
    ImageButton btn_pause;
    ImageButton btn_next;


    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        btn_prev = (ImageButton) findViewById(R.id.ImageButton01);
        btn_pause = (ImageButton) findViewById(R.id.ImageButton02);
        btn_play = (ImageButton) findViewById(R.id.ImageButton03);
        btn_next = (ImageButton) findViewById(R.id.ImageButton04);

        mp = new MediaPlayer();
        mp.setAudioStreamType(AudioManager.STREAM_MUSIC);


        btn_play.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                try {
                    mp.setDataSource(path); 

                } catch (IllegalArgumentException e) {

                    e.printStackTrace();
                } catch (IllegalStateException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                try {
                    mp.prepare();
                } catch (IllegalStateException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                mp.start();

            }
        });

    }

}






08-01 07:06:00.142: W/KeyCharacterMap(407): No keyboard for id 0
    08-01 07:06:00.142: W/KeyCharacterMap(407): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
    08-01 07:06:05.821: D/dalvikvm(407): GC_EXPLICIT freed 21K, 53% free 2568K/5379K, external 3207K/3962K, paused 55ms
    08-01 07:06:20.311: D/dalvikvm(444): GC_EXTERNAL_ALLOC freed 52K, 53% free 2552K/5379K, external 1969K/2137K, paused 58ms
    08-01 07:06:43.171: E/MediaPlayer(444): error (1, -2147483648)
    08-01 07:06:43.171: W/System.err(444): java.io.IOException: Prepare failed.: status=0x1
    08-01 07:06:43.171: W/System.err(444):  at android.media.MediaPlayer.prepare(Native Method)
    08-01 07:06:43.171: W/System.err(444):  at com.player.phoneagent.gui.AndroidClientActivity$1.onClick(AndroidClientActivity.java:60)
    08-01 07:06:43.171: W/System.err(444):  at android.view.View.performClick(View.java:2485)
    08-01 07:06:43.171: W/System.err(444):  at android.view.View$PerformClick.run(View.java:9080)
    08-01 07:06:43.181: W/System.err(444):  at android.os.Handler.handleCallback(Handler.java:587)
    08-01 07:06:43.181: W/System.err(444):  at android.os.Handler.dispatchMessage(Handler.java:92)
    08-01 07:06:43.181: W/System.err(444):  at android.os.Looper.loop(Looper.java:123)
    08-01 07:06:43.181: W/System.err(444):  at android.app.ActivityThread.main(ActivityThread.java:3683)
    08-01 07:06:43.181: W/System.err(444):  at java.lang.reflect.Method.invokeNative(Native Method)
    08-01 07:06:43.181: W/System.err(444):  at java.lang.reflect.Method.invoke(Method.java:507)
    08-01 07:06:43.181: W/System.err(444):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
    08-01 07:06:43.181: W/System.err(444):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
    08-01 07:06:43.181: W/System.err(444):  at dalvik.system.NativeStart.main(Native Method)
    08-01 07:06:43.181: E/MediaPlayer(444): start called in state 0
    08-01 07:06:43.181: E/MediaPlayer(444): error (-38, 0)
    08-01 07:06:43.181: E/MediaPlayer(444): Error (-38,0)
Achilles
  • 711
  • 2
  • 13
  • 35
  • Possible duplicate of: http://stackoverflow.com/questions/9008770/media-player-called-in-state-0-error-38-0 – Cat Aug 01 '12 at 06:24
  • I saw that question but that one suggest using some kind of OnListener... what annoys me is that this used to work perfectly, i made few changes and now it refuses to work – Achilles Aug 01 '12 at 06:33
  • Perhaps go back over your changes, make them individually and see which one causes the issue? – Cat Aug 01 '12 at 06:35

2 Answers2

1

I always had problems trying to use "raw" folder... I suggest putting the sound file in "assets" and using the methods here to set / prepare / and play:

http://droidapp.co.uk/2011/06/08/android-dev-playing-a-mp3-from-assets/

Tony
  • 2,335
  • 21
  • 25
  • This works perfectly, but my way used to work as well, specially that I might need to play songs from SD card as well as raw folder... want to know why it stopped working – Achilles Aug 01 '12 at 06:31
1

Just do something like:

MediaPlayer mp;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);       

    setContentView(R.layout.main);       

    mp = MediaPlayer.create(this, R.raw.yourSongFileName);

    btn_play.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            mp.stop();
            mp.release();
            mp = null;

            mp = MediaPlayer.create(this, R.raw.yourSongFileName);

            mp.start();
            //Other stuff below.

See if that works.

0gravity
  • 2,682
  • 4
  • 24
  • 33
  • do I need to have mp = MediaPlayer.create(this, R.raw.yourSongFileName); twice ? – Achilles Aug 01 '12 at 06:35
  • I think so, because when you first launch your activity the onCreate gets called and that is when you set the player for the first time. Then every time you click the button I am stopping the sound and making the player be null, then I create another one instance of the player. – 0gravity Aug 01 '12 at 06:37