Android媒体播放器寻求不起作用

时间:2017-02-10 07:19:27

标签: android android-studio media-player android-seekbar

我在对话框中使用搜索栏媒体播放器,但点击它时搜索栏不起作用

我正在制作录音机应用程序并使用搜索来播放音频文件。当我点击搜索栏时,它会向前移动,但是在1秒后它会被设置回以前的位置。

   @Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    final Holder holder = new Holder();
    View rowView;
    rowView = inflater.inflate(R.layout.audio_calls_list_row, null);
    holder.playpauseView = (LinearLayout) rowView.findViewById(R.id.play_pause_view);
    holder.playpausebtn = (ImageButton) rowView.findViewById(R.id.play_pause_btn);
    holder.callfileName = (TextView) rowView.findViewById(R.id.audiofileName);
    holder.totaltime = (TextView) rowView.findViewById(R.id.durationtiming);
    holder.callstatus1 = (ImageView) rowView.findViewById(R.id.callstatusid);
    holder.btnshare = (ImageView) rowView.findViewById(R.id.btn_share);
    holder.number = (TextView) rowView.findViewById(R.id.txt_number);

    holder.filetimecreated = (TextView) rowView.findViewById(R.id.myaudiofiletime);
    holder.filedatecreated = (TextView) rowView.findViewById(R.id.myaudiofiledate);

                   if (audioFileList.size() > 0) {
        try {


             mmr = new MediaMetadataRetriever();
                   mmr.setDataSource(audioFileList.get(position).getMyfilepath());
            String durationStr = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
            int millSecond = Integer.parseInt(durationStr);
            //setting listview totaltime

            holder.totaltime.setText(String.format("%02d:%02d:%02d",
                    TimeUnit.MILLISECONDS.toHours((long) millSecond),
                    TimeUnit.MILLISECONDS.toMinutes((long) millSecond),
                    TimeUnit.MILLISECONDS.toSeconds((long) millSecond) -
                            TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.
                                    toMinutes((long) millSecond))));

            String filename = audioFileList.get(position).filename;
            final String call_type = filename.substring(25, 33);
            int numlastpositionstr = filename.lastIndexOf("_");
            String call_number = filename.substring(34, numlastpositionstr);
            String call_time = filename.substring(16, 21);
            String call_date = filename.substring(5, 15);
            char[] call_timeArray = call_time.toCharArray();
            for (int x = 0; x < call_timeArray.length; x++) {
                if (call_timeArray[x] == '.') {
                    call_timeArray[x] = ':';
                }
            }
            call_time = String.valueOf(call_timeArray);
            holder.filetimecreated.setText(call_time);
            holder.filedatecreated.setText(call_date);
            String name = getContactDisplayNameByNumber(call_number, context);
            holder.number.setText(call_number);
            holder.callfileName.setText(name);

                    //status incomming or outgoing.

     if (filename.contains(CallStatus.INCOMING) || filename.contains            (CallStatus.incoming)) {
        //            if (call_type.equals(CallStatus.INCOMING) ||          call_type.equals(CallStatus.incoming)) {
                holder.callstatus1.setBackgroundResource(R.drawable.ic_call_received_black_24dp);
            } else {
                holder.callstatus1.setBackgroundResource(R.drawable.ic_call_made_black_24dp);
            }
           //            if (call_type.equals(CallStatus.OUTGOING) || call_type.equals(CallStatus.outgoing)) {
                    //                holder.callstatus1.setBackgroundResource(android.R.drawable.sym_call_outgoing);
         //            }
          } catch (Exception e) {
            System.out.println("Exaption is here " + e);
        }
    } else {
              Log.d("audiofilesize", "Audio file size is 0");
    }

    holder.playpausebtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            final AlertDialog.Builder audioPlayDialog = new AlertDialog.Builder(context, R.style.DialogTheme);
            LayoutInflater inflater1 = LayoutInflater.from(context.getApplicationContext());
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
            View layout1 = inflater1.inflate(R.layout.dialoguelayout, null);
            audioPlayDialog.setView(layout1)
                    .setNeutralButton("", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                            mediaPlayer.pause();
                            mediaPlayer.stop();
                            mediaPlayer.reset();
                            mediaPlayer.release();
                            myHandler.removeCallbacks(UpdateSongTime);
                            filecompleted = false;
                            running = false;
                        }
                    });
            audioPlayDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialog) {
                    try {
                        mediaPlayer.pause();
                        mediaPlayer.stop();
                        mediaPlayer.reset();
                        mediaPlayer.release();
                        myHandler.removeCallbacks(UpdateSongTime);
                        filecompleted = false;
                        running = false;
                    } catch (Exception e) {
                    }
                }
            });
            audioPlayDialog.setCancelable(true);
            final ImageButton audioPlayDialogButton = (ImageButton) layout1.findViewById(R.id.your_dialog_button);
            audioPlayDialogSeekBar = (SeekBar) layout1.findViewById(R.id.your_dialog_seekbar);
            dfilename = (TextView) layout1.findViewById(R.id.dcallnameid);
            remaingtime = (TextView) layout1.findViewById(R.id.dremaingtime);
            totaltime = (TextView) layout1.findViewById(R.id.dtotaltime);
            callstatus = (TextView) layout1.findViewById(R.id.dcallstatus);
            audioPlayDialog.create();
            audioPlayDialog.show();

            String[] call_type1 = getcallstatus(position);
            callstatus.setText(call_type1[0]);
            if (call_type1[1].equals("")) {
                dfilename.setText(call_type1[2]);
            } else {
                dfilename.setText(call_type1[1]);
            }
            audioPlayDialogButton.setOnClickListener(new View.OnClickListener() {
                @Override
                  public void onClick(View v) {


                    if (filecompleted == true) {
                        updateTime();
                        if (mediaPlayer.isPlaying()) {
                            mediaPlayer.pause();
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                                audioPlayDialogButton.setBackgroundResource(R.drawable.ic_play_circle_outline_black_48dp);
                            }
                        } else {
                            mediaPlayer.start();
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                                audioPlayDialogButton.setBackgroundResource(R.drawable.ic_pause_circle_outline_black_48dp);
                            }
                        }
                    } else {

                        try {
                            if (mediaPlayer.isPlaying()) {
                                mediaPlayer.pause();
                                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                                    audioPlayDialogButton.setBackgroundResource(R.drawable.ic_play_circle_outline_black_48dp);
                                }
                            } else {
                                mediaPlayer.start();
                                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                                    audioPlayDialogButton.setBackgroundResource(R.drawable.ic_pause_circle_outline_black_48dp);
                                }
                            }
                        } catch (Exception e) {
                        }
                    }
                }


            });

            audioPlayDialogSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                @Override
                public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

                    Log.d("Progress changed", "progress changed" + progress);
                    audioPlayDialogSeekBar.setMax((int) finalTime);
                }

                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {
                    Log.d("starttracking", "progress starTT");
                }

                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {
                }
            });

            final Uri data = Uri.parse(audioFileList.get(position).getMyfilepath());

            System.out.println("file Path is here" + data.getPath());
            mediaPlayer = new MediaPlayer();
            mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            try {
                mediaPlayer.setDataSource(context, data);
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {


                mediaPlayer.prepareAsync();
                mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                    @Override
                    public void onPrepared(MediaPlayer mp) {
                        finalTime = mp.getDuration();
                        running = true;
                        mp.start();
                        //      audioPlayDialogSeekBar = true;
                        audioPlayDialogSeekBar.setMax(mediaPlayer.getDuration());
                        if (finalTime != 0) {
                            totaltime.setText(String.format("%02d:%02d:%02d",
                                    TimeUnit.MILLISECONDS.toHours((long) finalTime),
                                    TimeUnit.MILLISECONDS.toMinutes((long) finalTime),
                                    TimeUnit.MILLISECONDS.toSeconds((long) finalTime) -
                                            TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.
                                                    toMinutes((long) finalTime))));
                        }
                    }

                });
            } catch (Exception e) {
            }
            myHandler = new Handler();
            totaltime.setText(String.format("%02d:%02d:%02d",
                    TimeUnit.MILLISECONDS.toHours((long) finalTime),
                    TimeUnit.MILLISECONDS.toMinutes((long) finalTime),
                    TimeUnit.MILLISECONDS.toSeconds((long) finalTime) -
                            TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.
                                    toMinutes((long) finalTime))));

            updateTime();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                audioPlayDialogButton.setBackgroundResource    (R.drawable.ic_pause_circle_outline_black_48dp);
            }
            firsttime = false;

            mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    audioPlayDialogSeekBar.setProgress(0);
                    audioPlayDialogButton.setBackgroundResource    (R.drawable.ic_play_circle_outline_black_48dp);
                    filecompleted = true;
                    startTime = 0;
                    mediaPlayer.pause();
                    checkpoint = true;
                    mp.seekTo(10);
                    mp.pause();
                }
            });

            mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
                @Override
                public boolean onError(MediaPlayer mp, int what, int extra) {
                    return false;
                }
            });
        }

        private void updateTime() {
            UpdateSongTime = new Runnable() {
                public void run() {
                    if (running == true) {
                        startTime = mediaPlayer.getCurrentPosition();
                    }
                    if (filecompleted == true) {
                        if (checkpoint) {
                            startTime = 0;
                            checkpoint = false;
                        }
                    }
                    if (startTime > (finalTime - 100)) {
                        remaingtime.setText("00:00:00");

                    } else {
                        remaingtime.setText(String.format("%02d:%02d:%02d",
                                TimeUnit.MILLISECONDS.toHours((long) startTime),
                                TimeUnit.MILLISECONDS.toMinutes((long) startTime),
                                TimeUnit.MILLISECONDS.toSeconds((long) startTime) -
                                        TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.
                                                toMinutes((long) startTime))));
                        audioPlayDialogSeekBar.setProgress((int) startTime);
                        myHandler.postDelayed(this, 100);
                    }

                }
            };

            myHandler1 = new Handler();
            UpdateSongTime1 = new Runnable() {
                public void run() {
                    myHandler.postDelayed(this, 100);
                }
            };
            startTime = mediaPlayer.getCurrentPosition();
            audioPlayDialogSeekBar.setProgress((int) startTime);
            myHandler.postDelayed(UpdateSongTime, 100);

        }
    });
    holder.btnshare.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //  final Uri data = Uri.parse(audioFileList.get(position).getMyfilepath());
                  Intent intent = new Intent();
            intent.setAction(Intent.ACTION_SEND);
            intent.setType("audio/*");
            intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File   (audioFileList.get(position).getMyfilepath())));
            context.startActivity(intent);


        }
    });
    return rowView;

}

2 个答案:

答案 0 :(得分:1)

你在哪里调用media.seekTo(位置)?据我所知,你没有在seekbarchangelistener上调用它。

因为你总是设置相对于你的歌曲进度的搜索栏位置..它总是会回到以前的状态..

解决方案: 在你的seekbarchangelistener中..使用onSeekbarchanged并使用(媒体播放器).seekTo(position)

答案 1 :(得分:1)

  try {
                        if (mediaPlayer.isPlaying() || mediaPlayer != null) {
                            if (fromUser)
                                mediaPlayer.seekTo(progress);
                        } else if (mediaPlayer == null) {
                            Toast.makeText(context.getApplicationContext(), "Media is not running",
                                    Toast.LENGTH_SHORT).show();
                            seekBar.setProgress(0);
                        }
                    } catch (Exception e) {
                        Log.e("seek bar", "" + e);
                        seekBar.setEnabled(false);

                    }

我使用此代码并发布已解决的