24

这是用于从其他类动态获取文件名(.mp3)的代码,因为我的资产文件夹中有许多 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();
                    }
                }




            }
        });


    }

原木猫:

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)

需要注意的点: 1. 我已经检查过堆栈溢出的类似线程,答案是使用 prepareAsync() 2. 我使用过 prepareAsync()

提前致谢。

4

4 回答 4

24

您也可以使用下面onPrepared()的方法来调用 start 方法,以便在准备好start()之后调用MediaPlayer

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

            mp.start();

        }
    });
于 2013-03-20T06:18:33.433 回答
2

除了使用这种preparedSync/prepare方法,MediaPlayer您只需将转换为 URI 的文件路径传递给MediaPlayer.create()

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

这将准备好并开始播放

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

于 2013-03-20T06:13:35.413 回答
1

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

我们称其他动作为

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

或任何其他事情。

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

你需要编写 onprepare 方法

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

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

只需删除

mp.start() 

mp.prepareAsync();

你会很高兴去的

于 2018-03-04T22:44:40.530 回答
0
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;
    }
}
于 2019-11-28T13:05:42.403 回答