Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 解析preparedAsync()MediaPlayer_Android_Media Player_Prepare - Fatal编程技术网

Android 解析preparedAsync()MediaPlayer

Android 解析preparedAsync()MediaPlayer,android,media-player,prepare,Android,Media Player,Prepare,我正在编写一个音乐应用程序,使用从网站获取的url播放音乐。在我的代码中,我使用方法准备和应用程序工作。但是这个方法让我很不舒服。。。我想使用方法prepareAsync。。。但我不知道用它:。我是Android编程的新手。请编辑我的代码 我英语说得不好。很抱歉给您带来不便 代码如下: 函数播放歌曲 private void playSong(String urlData) { btnPlay = (ImageButton) findViewById(R.id.btnPlay);

我正在编写一个音乐应用程序,使用从网站获取的url播放音乐。在我的代码中,我使用方法准备和应用程序工作。但是这个方法让我很不舒服。。。我想使用方法prepareAsync。。。但我不知道用它:。我是Android编程的新手。请编辑我的代码

我英语说得不好。很抱歉给您带来不便

代码如下:

函数播放歌曲

private void playSong(String urlData) {
        btnPlay = (ImageButton) findViewById(R.id.btnPlay);
        seekBar = (SeekBar) findViewById(R.id.seekBar);
        btnPlay.setOnClickListener(this);
        seekBar.setMax(99);
        seekBar.setOnTouchListener(this);
        mPlay = new MediaPlayer();
        mPlay.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mPlay.setOnBufferingUpdateListener(this);
        mPlay.setOnCompletionListener(this);
        try {
            mPlay.setDataSource(urlData);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

@Override
    public void onPrepared(MediaPlayer mp) {
        if (mPlay != null) {
            mPlay.start();
            btnPlay.setImageResource(R.drawable.pause_icon);
        } else {
            mPlay.pause();
            btnPlay.setImageResource(R.drawable.play);
        }
        mPlay.start();
        primaryUpdateSeekBar();
    }
函数onClick使用play

@Override
    public void onClick(View view) {
        if (view.getId() == R.id.btnPlay) {
            try {
                mPlay.prepare();
            } catch (Exception e) {
                e.printStackTrace();
            }
            LenghData = mPlay.getDuration();
            if (!mPlay.isPlaying()) {
                mPlay.start();
                btnPlay.setImageResource(R.drawable.pause_icon);
            } else {
                mPlay.pause();
                btnPlay.setImageResource(R.drawable.play);
            }
            primaryUpdateSeekBar();
        }
    }
Seekbar的功能

private void primaryUpdateSeekBar() {
        seekBar.setProgress((int) (((float) mPlay.getCurrentPosition() / LenghData) * 100));
        if (mPlay.isPlaying()) {
            Runnable notification = new Runnable() {
                @Override
                public void run() {
                    long totalDuration = mPlay.getDuration();
                    long currentDuration = mPlay.getCurrentPosition();
                    tvTotalTime.setText(Utils.getTimeString(totalDuration));
                    tvCurrentTime.setText(Utils.getTimeString(currentDuration));
                    primaryUpdateSeekBar();
                }
            };
            handler.postDelayed(notification, 1000);
        }

    }

    @Override
    public void onBackPressed() {
        if (mPlay.isPlaying()) {
            mPlay.stop();
        }
        super.onBackPressed();
    }

    @Override
    public void onBufferingUpdate(MediaPlayer mp, int percent) {
        seekBar.setSecondaryProgress(percent);
    }

    @Override
    public void onCompletion(MediaPlayer mediaPlayer) {
        btnPlay.setImageResource(R.drawable.play);
        seekBar.setProgress(0);
        seekBar.setSecondaryProgress(0);
        tvTotalTime.setText("00:00");
        tvCurrentTime.setText("");
        try {
            playSong(ifChangeCheck);
            mPlay.prepareAsync();
            mPlay.setOnPreparedListener(this);
            mPlay.setLooping(true);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean onTouch(View view, MotionEvent event) {
        if (view.getId() == R.id.seekBar) {
            SeekBar seekbar = (SeekBar) view;
            int playPositioninMiliseconds = (LenghData / 100)
                    * seekbar.getProgress();
            mPlay.seekTo(playPositioninMiliseconds);
        }
        return false;
    }
决赛

@Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (group.getCheckedRadioButtonId()) {
        case R.id.rad32Kb:
            String aftercheck32 = "";
            int checkLinkIsPlay32 = urlDataSource.lastIndexOf("/") - 3;
            String subString32 = urlDataSource.substring(checkLinkIsPlay32);
            if (subString32.contains("128")) {
                aftercheck32 = urlDataSource.replace("/128/", "/32/").replace(
                        ".mp3", ".m4a");
            } else if (subString32.contains("320")) {
                aftercheck32 = urlDataSource.replace("/320/", "/32/").replace(
                        ".mp3", ".m4a");
            } else if (subString32.contains("m4a")) {
                aftercheck32 = urlDataSource.replace("/m4a/", "/32/");
            }
            ifChangeCheck = aftercheck32;
            try {
                if (mPlay != null) {
                    mPlay.stop();
                    mPlay.reset();
                }
                playSong(ifChangeCheck);
                mPlay.setLooping(true);
            } catch (IllegalStateException e) {
                e.printStackTrace();
            }
            break;
        case R.id.rad128Kb:
            try {
                if (mPlay != null) {
                    ifChangeCheck = urlDataSource;
                    mPlay.stop();
                    mPlay.reset();
                }
                playSong(urlDataSource);
                mPlay.setLooping(true);
            } catch (IllegalStateException e) {
                e.printStackTrace();
            }
            break;
        case R.id.rad320Kb:
            String aftercheck320 = "";
            int checkLinkIsPlay320 = urlDataSource.lastIndexOf("/") - 3;
            String subLink320 = urlDataSource.substring(checkLinkIsPlay320);
            if (subLink320.contains("/32")) {
                aftercheck320 = urlDataSource.replace("/32/", "/320/").replace(
                        ".m4a", ".mp3");
            } else if (subLink320.contains("128")) {
                aftercheck320 = urlDataSource.replace("/128/", "/320/");
            } else if (subLink320.contains("m4a")) {
                aftercheck320 = urlDataSource.replace("/m4a/", "/320/")
                        .replace(".m4a", ".mp3");
            }
            ifChangeCheck = aftercheck320;
            try {
                if (mPlay != null) {
                    mPlay.stop();
                    mPlay.reset();
                }
                playSong(ifChangeCheck);
                mPlay.setLooping(true);
            } catch (IllegalStateException e) {
                e.printStackTrace();
            }
            break;
        case R.id.rad500:
            String aftercheck500 = "";
            int checkLinkIsPlay500 = urlDataSource.lastIndexOf("/") - 3;
            String subLink500 = urlDataSource.substring(checkLinkIsPlay500);
            if (subLink500.contains("/32")) {
                aftercheck500 = urlDataSource.replace("/32/", "/m4a/");

            } else if (subLink500.contains("128")) {
                aftercheck500 = urlDataSource.replace("/128/", "/m4a/")
                        .replace(".mp3", ".m4a");
            } else if (subLink500.contains("320")) {
                aftercheck500 = urlDataSource.replace("/320/", "/m4a/")
                        .replace(".mp3", ".m4a");
            }
            ifChangeCheck = aftercheck500;
            try {
                if (mPlay != null) {
                    mPlay.stop();
                    mPlay.reset();
                }
                playSong(ifChangeCheck);
                mPlay.setLooping(true);
            } catch (IllegalStateException e) {
                e.printStackTrace();
            }
            break;
        default:
            break;
        }

    }

以上都是我在课堂上的音乐活动代码。。。感谢您帮助我从playsong方法调用prepareAsync方法,因为它将立即开始缓冲音频。当足够的音频缓冲后,将调用onPrepared方法并播放音频

private void playSong(String urlData) {
    btnPlay = (ImageButton) findViewById(R.id.btnPlay);
    seekBar = (SeekBar) findViewById(R.id.seekBar);
    btnPlay.setOnClickListener(this);
    seekBar.setMax(99); 
    seekBar.setOnTouchListener(this);
    mPlay = new MediaPlayer();
    mPlay.setAudioStreamType(AudioManager.STREAM_MUSIC);
    mPlay.setOnBufferingUpdateListener(this);
    mPlay.setOnCompletionListener(this);
    try { 
        mPlay.setDataSource(urlData);
        mPlay.prepareAsync();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } 
} 

我编辑我的代码。但是Logcat告诉我错误07-2601:55:27.326:E/MediaPlayer1291:stop在状态407-2601:55:27.326:E/MediaPlayer1291:error-38,0 07-2601:55:27.330:E/MediaPlayer1291:error 1,-2147483648 07-2601:55:27.642:E/MediaPlayer1291:error 1,-2147483648 07-26 01:55:27.646:E/MediaPlayer1291:prepareAsync在状态4中被调用您正在不应该调用的状态中调用player方法。您必须遵守MediaPlayer状态图。请参阅@joao2fast4u问题已解决:D谢谢: