tags:

views:

156

answers:

1

My code tries to play an MP3 file from res/raw.

Code:

FileDescriptor fd = appContext.getResources().openRawResourceFd(R.raw.ringtone)
                    .getFileDescriptor();
player = new MediaPlayer();
            try
            {
                player.setAudioStreamType(AudioManager.STREAM_RING);
                player.setDataSource(fd);
                player.prepare();           
            }
            catch (IllegalArgumentException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
                player = null;
                return;
            }
            catch (IllegalStateException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
                player = null;
                return;
            }
            catch (IOException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
                player = null;
                return;
            }

            player.setLooping(true);
            player.start();

The log shows:

02-21 15:18:18.360: ERROR/PlayerDriver(51): Command PLAYER_SET_DATA_SOURCE completed with an error or info PVMFErrNotSupported 02-21 15:18:18.380: ERROR/MediaPlayer(693): error (1, -4) 02-21 15:18:18.390: WARN/PlayerDriver(51): PVMFInfoErrorHandlingComplete

After player.prepare() is called.

I really don't have a hint. I won't use MediaPlayer.create() because I need player.setAudioStreamType(AudioManager.STREAM_RING );

Would appreciate any help on this...

+1  A: 

From the MediaPlayer API docs:

When a MediaPlayer object is just created using new or after reset() is called, it is in the Idle state; and after release() is called, it is in the End state. Between these two states is the life cycle of the MediaPlayer object.

It is a programming error to invoke methods such as getCurrentPosition(), ... , setAudioStreamType(int) in the Idle state.

You should study the MediaPlayer lifecycle diagram and provided examples and rewrite your code with respect to them. In this case, you see you need to call setDataSource() before setAudioStreamType().

alt text


Side note: In Android, you really need to follow the lifecycle events for everything you do, or you'll get bitten. You can write incorrect code and you'll never know until runtime, or worse you might write code you think works and you'll only discover in weird circumstances that it doesn't... e.g. the screen orientation changes and the callback method is not the same in this case, or other similar situations (Home button...etc).

JRL
Thanks, but:1. There's no documentation about the correct lifecycle state for calling setAudioStreamType().2. I tried what you said already and it gave the same error...
Omer Gilad
Sorry, my first sentence isn't true, but it still doesn't work :(
Omer Gilad