开发者

Create() fails in MediaPlayer - android

I am trying to play a few music (wav format) files based on button clicks. I am unable to get MediaPlayer to work properly. Every time I try to instantiate an object of MediaPlayer class, it fails on create(). Before I post the code, here is a run-through of what I am doing:

1. A layout file that contains 2 buttons.

2. Those buttons are read and onClickListeners defined for them.

3. Depending on the button clicked, a function called playAudioFile(View) called which creates the MediaPlayer instance and plays the file. The code for playAudioFile(View) is as follows:

   public void playAudioFile(View v) {
        /*Steps:
         * 1. Take in id of the button.
         * 2. Using id, identify what file needs to be played.
         * 3. Play file.
         */

        String path_to_file = null;   //this is the path to the file.

        if(b01.getId() == ((Button)v).getId()) {
            Log.v(this.toString(), "Button 01 pressed.");
            path_to_file = "/sdcard/audio/temp1.wav";
        } else if(b02.getId() == ((Button)v).getId()) {
            Log.v(this.toString(), "Button02 pressed.");
            path_to_file = "/sdcard/audio/temp2.wav";
        }
        path_to_file = "file://" + path_to_file;
        Uri streamUri = Uri.parse(path_to_file);
        Log.v(this.toString(), "Path of file = " + path_to_file);
        MediaPlayer mp = MediaPlayer.create(this, streamUri);

        if(mp == null) {
  开发者_运维技巧          Log.v(this.toString(), "Create() on MediaPlayer failed.");
        }

        mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                // TODO Auto-generated method stub
                try {
                    mp.start();
                } catch (IllegalStateException e) {
                    e.printStackTrace();
                    Log.v(this.toString(), "Illegal state exception thrown in start.");
                }
            }
        });

        mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                // TODO Auto-generated method stub
                mp.release();
            }
        });
    }

I have gone through a lot of posts here on stackoverflow that detail solutions to the above problem. I am encountering this problem after having incorporated all the suggestions made therein. Any help is most welcome.

Thanks,

Sriram


I dont understand why you are trying to do

MediaPlayer mp = MediaPlayer.create(this, streamUri);

This is my implementation of my Player, Hope this will help you in some way:

MediaPlayer mp = new MediaPlayer();

        setContentView(R.layout.player);

        Intent intent = getIntent();

        String path = "";

        System.out.println("My Scheme : "+intent.getData().toString().substring(8));

        if (intent.getData().getScheme().equals("video")) {

            path = intent.getData().toString().substring(8);
            System.out.println("Path : "+path);

            if(path.endsWith(".mp3")) {
            setContentView(R.layout.musicplayer);
            setContentView(R.layout.controls);

                Log.e("MusicPlayer", "Playing: " + path);
                try {

                    Notification notification = new Notification(
                            R.drawable.playbackstart, path, System.currentTimeMillis());
                    nm.notify(NOTIFY_ID, notification);

                    mp.reset();
                    mp.setDataSource(path);
                    mp.prepare();
                    mp.start(); 
                    mp.setOnCompletionListener(new OnCompletionListener() {

                        public void onCompletion(MediaPlayer arg0) {
                            System.out.println("Hi i am at the End");

                        }
                    });
                }catch {expression}


If I'm not wrong, you can't use onPrepare because mp.prepare() is called inside MediaPlayer.create(). The simpliest code can be something like this:

MediaPlayer mp = MediaPlayer.create(this, streamUri);
mp.start();

If you want something more elaborated, you can try this:

MediaPlayer mp = MediaPlayer.create(this, streamUri);        
if(mp == null) {            
    Log.v(this.toString(), "Create() on MediaPlayer failed.");       
} else {
    mp.setOnCompletionListener(new OnCompletionListener() {

    @Override
      public void onCompletion(MediaPlayer mp) {
          //here you should call the methods to release memory
          mp.stop();
          mp.release();
      }
    });
    mp.start();

}

Tell me if it helped you.

PD: Is better if you use a device for testing rather than a simulator. Some classes don't work properly there (MediaPlayer can't reproduce video in simulators) and I don't remember if it supports audio.


As far as I understand your problem: You are calling the MediaPlayer.create(...) method, and it returns null, meaning the creation of a MediaPlayer instance failed. According to the accepted answer of this post MediaPlayer.create() always returns null it is due to a corrupted audio file.

I also have an issue, where the sound gets played but on rare occasions the create() method also returns null. I use sounds in a turn based game, after each turn a sound is played. I always call the create method, start the player and release it when it's done. That happens every turn and like once in 300 turns the create method returns null... So in my case it must be something else. In yours it could probably have something to do with the corruption of the mp3 file.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜