从URL播放mp3花费了太多时间

时间:2017-12-20 10:02:21

标签: android media-player

我正在尝试从URL播放mp3。但它花了太多时间在线播放文件,由于这个用户必须等到进度对话被解雇这对我来说是一个严重的问题。我怎样才能减少播放音乐的时间。我的URL和代码是:

网址:[http://ice31.securenetsystems.net/STUDIO1?type=.mp3][1]

public class Player extends AsyncTask<String, Void, Boolean> {
    private Context context;
    private String stationUrl;
    public static MediaPlayer mediaPlayer = new MediaPlayer();
    private boolean initialStage = true;
    private boolean playPause;
    public Boolean prepared = false;
    public ProgressDialog progressDialog = null;


    public Player(String stationUrl,Context context) {
        this.stationUrl =  stationUrl;
        this.context = context;
    }
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        CommonUtils.showProgress(context);
    }

    @Override
    protected Boolean doInBackground(String... strings) {

        try {

            mediaPlayer.setDataSource(stationUrl);
            mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mediaPlayer) {
                    initialStage = true;
                    playPause = false;
                    mediaPlayer.stop();
                    mediaPlayer.reset();
                }
            });

            mediaPlayer.prepare();
            prepared = true;

        } catch (Exception e) {
            prepared = false;
        }

        return prepared;
    }

    @Override
    protected void onPostExecute(Boolean aBoolean) {
        super.onPostExecute(aBoolean);
        CommonUtils.dismissProgress();
        mediaPlayer.start();
        mediaPlayer.pause();
        if (!CommonUtils.getPreferencesString(context,AppConstants.STATUS).equalsIgnoreCase(""))
        {
            if (CommonUtils.getPreferencesString(context,AppConstants.STATUS).equalsIgnoreCase("One"))
            {
                mediaPlayer.start();
            }
        }
        initialStage = false;
    }


}

1 个答案:

答案 0 :(得分:2)

MediaPlayer不适用于音频流。没有办法减少MediaPlayer缓冲的时间。从文档看起来似乎 https://developer.android.com/reference/android/media/AudioTrack.html 是更好的选择,甚至更https://developer.android.com/reference/android/media/MediaExtractor.html更复杂,但更适合流媒体。