简体   繁体   中英

Play wav with mediaplayer on android

I try to play a wav file streaming from the web.

                    MediaPlayer mediaPlayer = new MediaPlayer();
                    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

                    try {
                        mediaPlayer.setDataSource(url);
                        mediaPlayer.prepare();
                    } catch (IOException e) {
                        e.printStackTrace();
                        Log.e("SoundPlayerService", "Error preparing music");
                    }

                    mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                        @Override
                        public void onPrepared(MediaPlayer mp) {
                            Log.d(Tag, "Audio is playing from: " + url);
                            mp.start();
                        }
                    });

But it does not playing anything. What's the problem with the code?

您应该使用mediaPlayer.prepareAsync()代替mediaPlayer.prepare()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM