2016-09-27 52 views
0

这是我的代码,我几乎可以肯定问题是与while循环。音乐播放器的进度条不工作和崩溃

 public void start(View view) { 

     int currentPosition = 0; 


     player = MediaPlayer.create(this, R.raw.sound); 

     player.start(); 

     int total = player.getDuration(); 
     progressBar.setProgress(0); 
     progressBar.setMax(player.getDuration()); 

     while (player != null && currentPosition < total) { 
//   try { 
//    Thread.sleep(500); 

       currentPosition = player.getCurrentPosition(); 

//   } catch (InterruptedException e) { 
//    return; 
//   } catch (Exception e) { 
//    return; 
//   } 

      progressBar.setProgress(currentPosition); 
     } 
    } 


    public void stop(View view) { 

     player.stop(); 

    } 

是否我有睡眠间隔我的结果是相同的;声音开始播放,但我无法停止它,并且进度条不动。 我将不胜感激

+0

是什么'getCurrentPosition()'做什么?它实际上是否返回一个值?你的意思是进度条不动?有没有用户界面? – lux

回答

0

我在我的应用程序中使用此代码与MediaPlayer使用seekBar。它实现了Runnable以在音乐播放时保持seekBar更新。看看代码:

public class MainActivity extends Activity implements Runnable, 
    OnClickListener, OnSeekBarChangeListener { 
private SeekBar seekBar; 
private Button startMedia; 
private Button stopMedia; 
private MediaPlayer mp; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    seekBar = (SeekBar) findViewById(R.id.seekBar1); 
    startMedia = (Button) findViewById(R.id.button1); 
    stopMedia = (Button) findViewById(R.id.button2); 
    startMedia.setOnClickListener(this); 
    stopMedia.setOnClickListener(this); 
    seekBar.setOnSeekBarChangeListener(this); 
    seekBar.setEnabled(false); 
} 

public void run() { 
    int currentPosition = mp.getCurrentPosition(); 
    int total = mp.getDuration(); 

    while (mp != null && currentPosition < total) { 
    try { 
    Thread.sleep(1000); 
    currentPosition = mp.getCurrentPosition(); 
    } catch (InterruptedException e) { 
    return; 
    } catch (Exception e) { 
    return; 
    } 
    seekBar.setProgress(currentPosition); 
    } 
} 

public void onClick(View v) { 
    if (v.equals(startMedia)) { 
    if (mp == null) { 
    mp = MediaPlayer.create(getApplicationContext(), R.raw.song2); 
    seekBar.setEnabled(true); 
    } 
    if (mp.isPlaying()) { 
    mp.pause(); 
    startMedia.setText("play"); 
    } else { 
    mp.start(); 
    startMedia.setText("pause"); 
    seekBar.setMax(mp.getDuration()); 
    new Thread(this).start(); 
    } 
    } 

    if (v.equals(stopMedia) && mp != null) { 
    if (mp.isPlaying() || mp.getDuration() > 0) { 
    mp.stop(); 
    mp = null; 
    startMedia.setText("play"); 
    seekBar.setProgress(0); 
    } 
    } 

} 

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

    } 
} 

@Override 
public void onStartTrackingTouch(SeekBar seekBar) { 
    // TODO Auto-generated method stub 

} 

@Override 
public void onStopTrackingTouch(SeekBar seekBar) { 
    // TODO Auto-generated method stub 

} 
} 

感谢:Sridhar Kulkarni

+0

我试过使用你的解决方案,但它没有工作 –

+0

其实我已经得到它与您的代码工作!但我仍然不明白为什么我需要使其可运行 –

+0

William Morrison解释说,Runnable将需要异步运行的代码与代码的运行方式分开。这使您的代码更加灵活。例如,可运行的异步代码可以运行在线程池或专用线程上。这里使用while循环不起作用,因为我们需要睡眠线程1000毫秒或任何持续时间你选择每秒更新搜索栏的进度,我们不需要睡眠主线程 –