2016-08-12 8 views
0

**Androidのシークバーエラー。音楽を演奏しながら。

シークバーが歌に応じて動いている曲を再生しながら、私は..メディアプレーヤーのストリーミングアプリを開発しています。しかし、助けてください私のJavaコードがFolows ...

public class Player extends Activity implements View.OnTouchListener,MediaPlayer.OnCompletionListener, MediaPlayer.OnBufferingUpdateListener { 

    private ImageButton btnPlay; 

    private ImageButton btnNext; 
    private ImageButton btnPrevious; 

    private ImageButton btnRepeat; 
    private ImageButton btnShuffle; 

    private TextView songTitleLabel; 
    private TextView songCurrentDurationLabel; 
    private TextView songTotalDurationLabel; 


    private int seekForwardTime = 5000; // 5000 milliseconds 
    private int seekBackwardTime = 5000; // 5000 milliseconds 
    private int currentSongIndex = 0; 
    private boolean isShuffle = false; 
    private boolean isRepeat = false; 
    private SeekBar songProgressBar; 
    private int mediaFileLengthInMilliseconds; 
    private final Handler handler = new Handler(); 

    String AudioURL = "http://f23.wapka-files.com/download/9/e/9/1408248_9e9b050b4bea1c48ba6b7329.mp3/bed5656a469655b9f12e/08-Chundari.Penne-Dulquer.Salmaan.mp3"; 


    MediaPlayer mp; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.player); 
//-=------------------------------------------------------------ 
     final ImageView iv = (ImageView) findViewById(R.id.image); 

     final String imgURL = "https://i.ytimg.com/vi/NLQHBv6lgXE/maxresdefault.jpg"; 
     new DownLoadImageTask(iv).execute(imgURL); 
     //--------------------------------------------------- 
     // All player buttons 
     btnPlay = (ImageButton) findViewById(R.id.btnPlay); 
     //btnForward = (ImageButton) findViewById(R.id.btnForward); 
     //btnBackward = (ImageButton) findViewById(R.id.btnBackward); 
     btnNext = (ImageButton) findViewById(R.id.btnNext); 
     btnPrevious = (ImageButton) findViewById(R.id.btnPrevious); 

     btnRepeat = (ImageButton) findViewById(R.id.btnRepeat); 
     btnShuffle = (ImageButton) findViewById(R.id.btnShuffle); 

     songTitleLabel = (TextView) findViewById(R.id.songTitle); 
     songCurrentDurationLabel = (TextView) findViewById(R.id.songCurrentDurationLabel); 
     songTotalDurationLabel = (TextView) findViewById(R.id.songTotalDurationLabel); 

     songProgressBar = (SeekBar) findViewById(R.id.songProgressBar); 
     songProgressBar.setMax(99); // It means 100% .0-99 
     songProgressBar.setOnTouchListener(null); 

     mp = new MediaPlayer(); 

     // Mediaplayer 

     mp.setAudioStreamType(AudioManager.STREAM_MUSIC); 
     mp.setOnBufferingUpdateListener(null); 
     mp.setOnCompletionListener(null); 

     try { 




     } catch (Exception h) { 
      Toast.makeText(Player.this, "NO MUSIC HERE...........", Toast.LENGTH_SHORT).show(); 
     } 

    } 


     private void primarySeekBarProgressUpdater() { 
      songProgressBar.setProgress((int) (((float) mp.getCurrentPosition()/mediaFileLengthInMilliseconds) * 100)); // This math construction give a percentage of "was playing"/"song length" 
      if (mp.isPlaying()) { 
       Runnable notification = new Runnable() { 
        public void run() { 
         primarySeekBarProgressUpdater(); 
        } 
       }; 
       handler.postDelayed(notification, 1000); 
      } 
     } 
     public void clik(View view){ 
      try { 
       mp.setDataSource(AudioURL); // setup song from http://www.hrupin.com/wp-content/uploads/mp3/testsong_20_sec.mp3 URL to mediaplayer data source 
       mp.prepare(); // you must call this method after setup the datasource in setDataSource method. After calling prepare() the instance of MediaPlayer starts load data from URL to internal buffer. 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 

      mediaFileLengthInMilliseconds = mp.getDuration(); // gets the song length in milliseconds from URL 

      if (!mp.isPlaying()) { 
       mp.start(); 
       btnPlay.setImageResource(R.drawable.btn_pause); 

      } else { 
       mp.pause(); 
       btnPlay.setImageResource(R.drawable.btn_play); 

      } 

      primarySeekBarProgressUpdater(); 



     } 

    @Override 
    public void onBufferingUpdate(MediaPlayer mediaPlayer, int i) { 
     songProgressBar.setSecondaryProgress(i); 
    } 

    @Override 
    public void onCompletion(MediaPlayer mediaPlayer) { 
     mp.stop(); 
    } 

    @Override 
    public boolean onTouch(View view, MotionEvent motionEvent) { 
     if(motionEvent.getAction() == MotionEvent.ACTION_MOVE){ 
      // Log.d(TAG, "Moved , process data, Moved to :" + seekBarProgress.getProgress()); 
      songProgressBar.setProgress(songProgressBar.getProgress()); 
      return false; 
     } 
     // Log.d(TAG, "Touched , Progress :" + seekBarProgress.getProgress()); 
     return true; 

    } 
} 

たようです

**

...それは タッチした位置から再生されていないバーをonTouching求める.....

+0

に示すよう

は、この行を追加しますか? – Opiatefuchs

+0

私は、そのメソッドを使用していません。どのように使用するか教えていただけますか? –

+0

はちょうど – Opiatefuchs

答えて

0

シークバーが移動した後、たぶん、あなたはseekTo(int)方法でMediaPlayer上の位置を設定し忘れてしまいました。たとえば:

@Override 
public boolean onTouch(View view, MotionEvent motionEvent) { 
    if(motionEvent.getAction() == MotionEvent.ACTION_MOVE){ 
     songProgressBar.setProgress(songProgressBar.getProgress()); 
     //seek to the position toucehd 
     mp.seekTo(songProgressBar.getProgress()); 
     return false; 
    } 
    // Log.d(TAG, "Touched , Progress :" + seekBarProgress.getProgress()); 
    return true; 

} 
+0

が機能していない...それは前と同じです....ここでは、この答えに従ってください? ()は、あなたのログに示したseekbar.getProgressの値は何ですか? memo.seekに署名しますか? –

+0

がonTouchイベントを右発火している..... – jos

0

あなたは99にsongProgressBar.setMax(99);最大に設定され、その後シークバーがメディアプレーヤーを目指します合計時間を融点に設定します。コードあなた `seekTo()`メソッドがある songProgressBar.setMax(mediaFileLengthInMilliseconds);

 public void clik(View view){ 
      try { 
       mp.setDataSource(AudioURL); // setup song from http://www.hrupin.com/wp-content/uploads/mp3/testsong_20_sec.mp3 URL to mediaplayer data source 
       mp.prepare(); // you must call this method after setup the datasource in setDataSource method. After calling prepare() the instance of MediaPlayer starts load data from URL to internal buffer. 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
      mediaFileLengthInMilliseconds = mp.getDuration(); // gets the song length in milliseconds from URL 

      // Add this line here 
      songProgressBar.setMax(mediaFileLengthInMilliseconds); 

      if (!mp.isPlaying()) { 
       mp.start(); 
       btnPlay.setImageResource(R.drawable.btn_pause); 
      } else { 
       mp.pause(); 
       btnPlay.setImageResource(R.drawable.btn_play); 
      } 
      primarySeekBarProgressUpdater(); 

     } 
+0

が機能していない...それは前と同じです.... –

関連する問題