MediaPlayer在模拟器上工作,但在android中的实际设备上没有

时间:2018-02-03 14:17:36

标签: java android android-mediaplayer

我正在创建一个音乐从直接链接流式传输的应用程序。它正在使用模拟器,但不适用于实际设备。我创建了一个名为HomeFragment的类和一个嵌套的类Player,它扩展了AsyncTask并启动了MediaPlayer。看起来在解释MediaPlayer时出错。在logcat中,它显示prepare()失败。

我搜索过它并发现了Attempt to call getDuration without a valid mediaplayer in media player on android,但这是不同的,因为我没有因为getDuration()方法而出错

    public class HomeFragment extends Fragment {
View view;

private ImageButton btn_play_pause;
private TextView tv_time;
private SeekBar seekBar;
private MediaPlayer mediaPlayer;
private int mediaFileLength;
private int realTimeLength;
private boolean playPause;
private boolean initialStage = true;
private ProgressDialog progressDialog;
final Handler handler = new Handler();
private Runnable updater;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.home_fragment,container,false);
    initialize();
    return view;
}

private void initialize() {
    btn_play_pause = (ImageButton) view.findViewById(R.id.btn_play_pause);
    tv_time = (TextView) view.findViewById(R.id.tv_timer);
    seekBar = (SeekBar) view.findViewById(R.id.seekBar);
    seekBar.setMax(99);
    seekBar.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if(mediaPlayer.isPlaying()){
                SeekBar seekbar = (SeekBar) v;
                int playPosition = (mediaFileLength / 100) * seekbar.getProgress();
                mediaPlayer.seekTo(playPosition);
            }
            return false;
        }
    });

    mediaPlayer = new MediaPlayer();
    progressDialog = new ProgressDialog(view.getContext());
    btn_play_pause.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            if(!playPause){
                btn_play_pause.setImageResource(R.drawable.ic_pause);
                if(initialStage){
                    new Player().execute("http://www.url_of_song.mp3");
                }
                else {
                    if (mediaPlayer != null) {
                        if (!mediaPlayer.isPlaying()) {
                            mediaPlayer.start();
                        }
                    }
                }
                playPause = true;
            }
            else{
                btn_play_pause.setImageResource(R.drawable.ic_play);
                if(mediaPlayer != null) {
                    if (mediaPlayer.isPlaying()) {
                        mediaPlayer.pause();
                    }
                }
                playPause = false;
            }
        }
    });
}

class Player extends AsyncTask<String, Void, Boolean>{

    @Override
    protected Boolean doInBackground(String... params) {
        boolean prepared = false;

        try{
            mediaPlayer.setDataSource(params[0]);
            mediaPlayer.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {
                @Override
                public void onBufferingUpdate(MediaPlayer mp, int percent) {
                    seekBar.setSecondaryProgress(percent);
                }
            });

            mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener(){

                @Override
                public void onCompletion(MediaPlayer mp) {
                    initialStage = true;
                    playPause = false;
                    btn_play_pause.setImageResource(R.drawable.ic_pause);
                    mp.stop();
                    mp.reset();
                }
            });
            mediaPlayer.prepare();
            prepared = true;
        }
        catch (Exception e){
            Log.e("AudioStreaming", e.getMessage());
            prepared = false;
        }
        return prepared;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        progressDialog.setMessage("Please Wait...");
        progressDialog.show();
    }

    @Override
    protected void onPostExecute(Boolean aBoolean) {
        super.onPostExecute(aBoolean);
        if(progressDialog.isShowing()){
            progressDialog.cancel();
        }
   //     mediaFileLength = mediaPlayer.getDuration();
        realTimeLength = mediaFileLength;
        mediaPlayer.start();
        initialStage = false;
        updateSeekbar();
    }
}

private void updateSeekbar(){
    seekBar.setProgress((int)(((float)mediaPlayer.getCurrentPosition() / mediaFileLength)*100));
    if(mediaPlayer.isPlaying()){
        updater = new Runnable() {
            @Override
            public void run() {
                updateSeekbar();
                realTimeLength -= 1000;
                tv_time.setText(String.format("%d:%d", TimeUnit.MILLISECONDS.toMinutes(realTimeLength), TimeUnit.MILLISECONDS.toSeconds(realTimeLength) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(realTimeLength))));
            }
        };
        handler.postDelayed(updater, 1000);
    }
}

我做错了什么,错误是什么,Logcat显示:

    02-03 15:54:27.226 15845-15845/com.example.sk.voiceapplication I/ViewRootImpl: CPU Rendering VSync enable = true
02-03 15:54:27.266 15845-15864/com.example.sk.voiceapplication D/MediaHTTPConnection: filterOutInternalHeaders: key=User-Agent, val= stagefright/1.2 (Linux;Android 5.1.1)
02-03 15:54:27.266 15845-15991/com.example.sk.voiceapplication D/MediaHTTPConnection: proxy null port 0
02-03 15:54:29.496 15845-15864/com.example.sk.voiceapplication D/MediaHTTPConnection: proxy null port 0
02-03 15:54:33.126 15845-15863/com.example.sk.voiceapplication D/MediaHTTPConnection: filterOutInternalHeaders: key=User-Agent, val= stagefright/1.2 (Linux;Android 5.1.1)
02-03 15:54:33.126 15845-15991/com.example.sk.voiceapplication D/MediaHTTPConnection: proxy null port 0
02-03 15:54:36.926 15845-15864/com.example.sk.voiceapplication D/MediaHTTPConnection: filterOutInternalHeaders: key=User-Agent, val= stagefright/1.2 (Linux;Android 5.1.1)
02-03 15:54:36.926 15845-15863/com.example.sk.voiceapplication D/MediaHTTPConnection: proxy null port 0
02-03 15:54:40.486 15845-15864/com.example.sk.voiceapplication D/MediaHTTPConnection: filterOutInternalHeaders: key=User-Agent, val= stagefright/1.2 (Linux;Android 5.1.1)
02-03 15:54:40.486 15845-15863/com.example.sk.voiceapplication D/MediaHTTPConnection: proxy null port 0
02-03 15:54:44.326 15845-15991/com.example.sk.voiceapplication D/MediaHTTPConnection: filterOutInternalHeaders: key=User-Agent, val= stagefright/1.2 (Linux;Android 5.1.1)
02-03 15:54:44.336 15845-15864/com.example.sk.voiceapplication D/MediaHTTPConnection: proxy null port 0
02-03 15:54:48.176 15845-15864/com.example.sk.voiceapplication D/MediaHTTPConnection: filterOutInternalHeaders: key=User-Agent, val= stagefright/1.2 (Linux;Android 5.1.1)
02-03 15:54:48.176 15845-15863/com.example.sk.voiceapplication D/MediaHTTPConnection: proxy null port 0
02-03 15:54:51.696 15845-15863/com.example.sk.voiceapplication D/MediaHTTPConnection: filterOutInternalHeaders: key=User-Agent, val= stagefright/1.2 (Linux;Android 5.1.1)
02-03 15:54:51.696 15845-15991/com.example.sk.voiceapplication D/MediaHTTPConnection: proxy null port 0
02-03 15:54:55.526 15845-15863/com.example.sk.voiceapplication D/MediaHTTPConnection: filterOutInternalHeaders: key=User-Agent, val= stagefright/1.2 (Linux;Android 5.1.1)
02-03 15:54:55.526 15845-15991/com.example.sk.voiceapplication D/MediaHTTPConnection: proxy null port 0
02-03 15:54:59.366 15845-15863/com.example.sk.voiceapplication D/MediaHTTPConnection: filterOutInternalHeaders: key=User-Agent, val= stagefright/1.2 (Linux;Android 5.1.1)
02-03 15:54:59.366 15845-15991/com.example.sk.voiceapplication D/MediaHTTPConnection: proxy null port 0
02-03 15:55:03.206 15845-15864/com.example.sk.voiceapplication D/MediaHTTPConnection: filterOutInternalHeaders: key=User-Agent, val= stagefright/1.2 (Linux;Android 5.1.1)
02-03 15:55:03.206 15845-15863/com.example.sk.voiceapplication D/MediaHTTPConnection: proxy null port 0
02-03 15:55:07.046 15845-15991/com.example.sk.voiceapplication D/MediaHTTPConnection: filterOutInternalHeaders: key=User-Agent, val= stagefright/1.2 (Linux;Android 5.1.1)
02-03 15:55:07.046 15845-15864/com.example.sk.voiceapplication D/MediaHTTPConnection: proxy null port 0
02-03 15:55:07.886 15845-15991/com.example.sk.voiceapplication D/MediaHTTPConnection: proxy null port 0
02-03 15:55:08.536 15845-15991/com.example.sk.voiceapplication E/MediaPlayer: error (1, -2147483648)
02-03 15:55:08.536 15845-15878/com.example.sk.voiceapplication E/AudioStreaming: Prepare failed.: status=0x1

02-03 15:55:08.566 15845-15845/com.example.sk.voiceapplication E/MediaPlayer: error (-38, 0)
02-03 15:55:08.566 15845-15845/com.example.sk.voiceapplication E/MediaPlayer: start called in state 0
02-03 15:55:08.566 15845-15845/com.example.sk.voiceapplication E/MediaPlayer: error (-38, 0)
02-03 15:55:08.566 15845-15845/com.example.sk.voiceapplication E/MediaPlayer: Error (-38,0)
02-03 15:55:08.566 15845-15845/com.example.sk.voiceapplication E/MediaPlayer: stop called in state 0
02-03 15:55:08.566 15845-15845/com.example.sk.voiceapplication E/MediaPlayer: error (-38, 0)

2 个答案:

答案 0 :(得分:0)

我不确定这是你的整个问题,但我看到你在AsyncTask的doInBackground方法中调用了media player.prepare(),然后在onPostExecute中调用了media player.start() ),

但是,在调用prepare()之后,你还没有等待玩家完成初始化。您需要使用OnPreparedListener,并在与播放器交互之前等待该通知。

答案 1 :(得分:0)

我正在犯这个错误,new Player().execute("http://www.url_of_song.mp3");里面onClick方法中的网址流式mp3歌曲的文件名包含空格和大写字母,因此歌曲无法流式传输。所以要注意这些愚蠢的错误。