媒体播放器在模拟器上工作,但不在安卓的实际设备上工作



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

我已经搜索了它,发现尝试在 android 上的媒体播放器中没有有效媒体播放器的情况下调用 getDuration,但这是不确定的,因为我没有因为 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);
    }
}

我做错了什么,错误是什么,日志猫显示:

    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)

我不确定这是你的全部问题,但我看到你在 AsyncTask 的 doInBackground 方法中调用媒体播放器.prepare((,然后在 onPostExecute(( 中调用媒体播放器.start((,

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

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

最新更新