当我的网址不起作用时,我需要调整一个错误侦听器,该侦听器在textview中设置离线状态。我不知道该如何精确调整。或返回假的意思是什么?任何帮助将不胜感激。
这是我的媒体播放器:

lv_tracks.setOnItemClickListener(new AdapterView.OnItemClickListener(){

    public void onItemClick(AdapterView<?> parent, View view,
                            int position, long id) {


        if (isNetworkAvailable()) {
            mSelectedTrackTitle.setText(stations[position]);
            // mSelectedTrackImage.setImageResource(images[position]);
            nowplaying.setText("Now Playing");
            mPlayerControl.setImageResource(R.drawable.ic_pause_circle_filled);
            Resources res = getResources();
            String[] links = res.getStringArray(R.array.links);
            String url = links[position];
            // Toast.makeText(MainActivity.this,url,Toast.LENGTH_SHORT).show();

            if (mMediaPlayer.isPlaying()) {
                Log.d("abc", "playing and now going to reset : " + url);
                try {
                    mMediaPlayer.pause();
                    mMediaPlayer.stop();
                    mMediaPlayer.release();
                    mMediaPlayer = new MediaPlayer();
                    mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                    mMediaPlayer.setDataSource(url);
                    mMediaPlayer.prepare();
                    mMediaPlayer.start();
                } catch (IOException e) {
                    nowplaying.setText("Offline");
                    Toast.makeText(MainActivity.this,"SERVER IS DOWN",Toast.LENGTH_SHORT).show();
                    //mMediaPlayer.release();
                    e.printStackTrace();
                }

            } else {
                try {
                    Log.d("abc", "in else part :" + url);
                    mMediaPlayer = new MediaPlayer();
                    mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                    mMediaPlayer.setDataSource(url);
                    mMediaPlayer.prepare();
                    mMediaPlayer.start();

                } catch (IOException e) {
                    nowplaying.setText("Offline");
                    Toast.makeText(MainActivity.this,"SERVER IS DOWN",Toast.LENGTH_SHORT).show();
                    //mMediaPlayer.release();
                    e.printStackTrace();
                }
                return;
            }
        } else {
            Toast.makeText(MainActivity.this, "No Internet Connection", Toast.LENGTH_SHORT).show();
        }

    }
});

Logcat(首次选择时):

10/12 17:07:54.426 20080-20091 / indusradio.pk W / MediaPlayer-JNI:
MediaPlayer最终定稿未发布10-12 17:07:54.426
20080-20091 / indusradio.pk I / MediaPlayer本机:Pid:20080 MediaPlayer
破坏者10-12 17:08:29.478 20080-20096 / indusradio.pk
D / MediaPlayerNative:消息:MEDIA_ERROR(100),
ext1 = MEDIA_ERROR_UNKNOWN(1),ext2 = 0x80000000 10-12 17:08:29.480
20080-20096 / indusradio.pk E / MediaPlayerNative:错误(1,-2147483648)
10-12 17:08:29.480 20080-20096 / indusradio.pk D / MediaPlayer原生
[notify]:[1204]回调应用listenerNotNull = 1,发送= 0 10-12
17:08:29.487 20080-20080 / indusradio.pk D / HwRTBlurUtils:检查模糊
HwToast-Toast的样式,themeResId:0x7f0900b4,上下文:
com.example.maham.indusradio.MainActivity @ 77db462,Nhwext:6,获取
模糊:使用禁用
android.graphics.drawable.NinePatchDrawable@c363c7 10-12 17:08:29.505
20080-20080 / indusradio.pk W / System.err:java.io.IOException:准备
失败。:status = 0x1 10-12 17:08:29.506 20080-20080 / indusradio.pk
W / System.err:位于android.media.MediaPlayer._prepare(本机方法)
10-12 17:08:29.507 20080-20080 / indusradio.pk W / System.err:at
android.media.MediaPlayer.prepare(MediaPlayer.java:1313)10-12
17:08:29.507 20080-20080 / indusradio.pk W / System.err:at
com.example.maham.indusradio.MainActivity $ 3.onItemClick(MainActivity.java:174)

有时它会崩溃,并且花费大约20秒的时间才能显示脱机状态。

最佳答案

您可以将文本设置为

    mMediaPlayer = new MediaPlayer();

    mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                                        mMediaPlayer.setDataSource(url);

    mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                     mMediaPlayer.start();
                }
            });

    mMediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
                                            @Override
                                            public boolean
    onError(MediaPlayer mp, int what, int extra) {

                                                return false;
                                            }
                                        });

        mMediaPlayer.prepareAsync();
                                    } catch (IOException e) {
                                      nowplaying.setText("OFFLINE")
                                        e.printStackTrace();
                                    }

关于java - 如何在媒体播放器中调整setOnErrorListener并将文本 View 中的状态设置为“脱机”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52773449/

10-11 22:35