Android Media Player: Start called in state 4 error(-38,0)

心不动则不痛 提交于 2019-12-12 07:25:35

问题


This is the code am using to fetch a file name(.mp3) dynamically from some other class as am having many mp3 files in my assets folder:

playAudioButton.setOnClickListener(new OnClickListener() {


            @Override
            public void onClick(View v) 
            {
                FileName audio=new FileName();
                String audioName=audio.getAudioName(count).toString();
                if(audioName=="NO Audio")
                {

                    Toast.makeText(getApplicationContext(), "No Audio for this page", Toast.LENGTH_SHORT).show();

                }
                else
                {

                    try {
                        afd=getAssets().openFd(audioName + ".mp3");
                        mp = new MediaPlayer();
                        mp.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
                        mp.prepareAsync();
                        mp.start();

                    } catch (IllegalArgumentException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IllegalStateException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }




            }
        });


    }

Log Cat :

03-20 11:21:28.726: E/SpannableStringBuilder(4418): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
03-20 11:21:28.726: E/SpannableStringBuilder(4418): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
03-20 11:22:24.968: D/libEGL(4579): loaded /vendor/lib/egl/libEGL_POWERVR_SGX540_120.so
03-20 11:22:24.976: D/libEGL(4579): loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX540_120.so
03-20 11:22:24.984: D/libEGL(4579): loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so
03-20 11:22:25.070: D/OpenGLRenderer(4579): Enabling debug mode 0
03-20 11:22:38.773: E/MediaPlayer(4579): start called in state 4
03-20 11:22:38.773: E/MediaPlayer(4579): error (-38, 0)
03-20 11:22:38.773: E/MediaPlayer(4579): Error (-38,0)

Points to be noted: 1. I have checked similar threads on stack overflow the answer is using prepareAsync() 2. I have used prepareAsync()

Thanks in advance.


回答1:


You can also use below onPrepared() method to call start method so that start() is called after MediaPlayer is prepared.

mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            // TODO Auto-generated method stub

            mp.start();

        }
    });



回答2:


Instead of using this preparedSync/prepare method of MediaPlayer you can just pass the file path converting into URI to the MediaPlayer.create()

MediaPlayer mediaPlayer = MediaPlayer.create(context, URI.parse("file://"+filePath));

this will prepare it and start playing itself

Or another way is use onPrepareListener() of MediaPlayer and then from this on prepared start playing




回答3:


state 4 means Mediaplayer is in preparing state

and we call other actions like

Mediaplayer.start() 
Mediaplayer.stop() 
Mediaplayer.pause() 

or any other thing .

As per your code it is Mediaplayer.start();

you need to write onprepare method

MediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            // TODO Auto-generated method stub
            mp.start();
        }
    });

Once Onprepared() method called you can do further process

just remove

mp.start() 

after

mp.prepareAsync();

And you will good to go




回答4:


public class BackgroundSound extends AsyncTask<String, Void, Void> {
    MediaPlayer mediaPlayerChatAudio;

    @Override
    protected Void doInBackground(String... params) {
        String model = params[0];
        final Uri uri = Uri.parse(model);
        LogEm.e("EM", "::::::::::URI::::::::::" + model);

        if (uri == null || uri == Uri.EMPTY) return null;
        if (mediaPlayerChatAudio != null) mediaPlayerChatAudio.stop();

        try {
            mediaPlayerChatAudio = MediaPlayer.create(TextActivity.this, uri);
            mediaPlayerChatAudio.setAudioStreamType(AudioManager.STREAM_MUSIC);
        } catch (Exception e) {
            // do nothing.
        }
        if (mediaPlayerChatAudio == null) return null;

        mediaPlayerChatAudio.setVolume(1.0f, 1.0f);
        mediaPlayerChatAudio.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                mediaPlayerChatAudio.reset();
                mediaPlayerChatAudio.release();
                mediaPlayerChatAudio = null;
            }
        });
        mediaPlayerChatAudio.start();
        return null;
    }
}


来源:https://stackoverflow.com/questions/15516469/android-media-player-start-called-in-state-4-error-38-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!