Tuesday, August 14, 2012

[android-developers] media player error (1, -4)

I'm trying to crate an app that will play a number of sound file saved within the app. I'm having trouble getting the media player to start however.
Here is the 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");
        playMusic();
    }
   
    public void playMusic()
    {
        try {
            mp.setDataSource("R.raw.music");
            Log.v(LOG_TAG, "set data source");
            mp.setOnPreparedListener(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");
        //if (!mp.isPlaying())
            mp.start();
        Log.v(LOG_TAG, "started music");
    }

It runs smoothly until the logtag "preparing" but "finished preparing; starting" never prints.
Not sure what's going wrong in this segment. The media file is type .mp3 and the api is level 8 (2.1)

thanks

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment