Android Media Player:在状态4中调用(-38,0)

时间:2022-01-06 18:58:40

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:

这是我用来从其他类动态获取文件名(.mp3)的代码,因为我的assets文件夹中有很多mp3文件:

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()

注意事项:我在stack overflow上检查了类似的线程,答案是使用prepareAsync() 2。我用prepareAsync()

Thanks in advance.

提前谢谢。

3 个解决方案

#1


19  

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

您还可以使用下面的onready()方法调用start方法,以便在准备MediaPlayer之后调用start()。

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

            mp.start();

        }
    });

#2


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的preparedSync/prepare方法,只需将文件路径转换为URI传递给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

或者另一种方法是使用MediaPlayer的onPrepareListener()然后从这里开始播放

#3


0  

state 4 means Mediaplayer is in preparing state

状态4表示Mediaplayer处于准备状态

and we call other actions like

我们称其他行为为

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

or any other thing .

或者别的什么。

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

根据您的代码,它是Mediaplayer.start();

you need to write onprepare method

您需要编写onprepare方法

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

一旦调用onprepare()方法,就可以进行进一步的处理

just remove

只是删除

mp.start() 

after

mp.prepareAsync();

And you will good to go

你也可以走了。

#1


19  

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

您还可以使用下面的onready()方法调用start方法,以便在准备MediaPlayer之后调用start()。

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

            mp.start();

        }
    });

#2


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的preparedSync/prepare方法,只需将文件路径转换为URI传递给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

或者另一种方法是使用MediaPlayer的onPrepareListener()然后从这里开始播放

#3


0  

state 4 means Mediaplayer is in preparing state

状态4表示Mediaplayer处于准备状态

and we call other actions like

我们称其他行为为

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

or any other thing .

或者别的什么。

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

根据您的代码,它是Mediaplayer.start();

you need to write onprepare method

您需要编写onprepare方法

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

一旦调用onprepare()方法,就可以进行进一步的处理

just remove

只是删除

mp.start() 

after

mp.prepareAsync();

And you will good to go

你也可以走了。