简体   繁体   中英

Android MediaPlayer does not work every time

I've started to learn how to develop applications for Android. Now I would like to create a MediaPlayer that can play a radio stream. I've tried my code with several radio stations. In some cases it is working properly but in some cases it stucks with the following error message:

E/MediaPlayerNative: error (1, -2147483648)

E/MediaPlayer: Error (1,-2147483648)

My code:

MediaPlayer player;
public void onViewCreated(View view, @Nullable Bundle savedInstanceState){
        player = new MediaPlayer();
        player.setAudioStreamType(AudioManager.STREAM_MUSIC);
        try{
            player.setDataSource("http://MY_LINK_GOES_HERE");
            player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    play.setEnabled(true); //this is my play button
                    mp.start();
                }
            });
            player.prepareAsync();
        } catch (IOException e){
            e.printStackTrace();
        }
        play.setOnClickListener(this);
}

 @Override
    public void onClick(View v) {
        if (player.isPlaying()){
            player.pause();
        } else {
            player.start();
        }
    }

I hope you can help me with this issue, thanks in advance!

I just had to use a https:// link as Android does not allow the simple http:// connection by default.

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