2013-05-27 1 views
0

나는 임의의 소리로 onCreate를 설정하고 오디오를 추적하기 위해 탐색 바를 추가해야합니다. 트랙과 함께 이동하지만 seekbar가 다시 당겨지면 되돌아 가지 않습니다. 오디오의 또 다른 부분. 어떤 도움도 좋을 것입니다, 오직 초보자, 멍청한 것에 대해 유감스럽게 생각합니다 :).안드로이드 MediaPlayer Seekbar는 추적 만 변경하지 않습니다.

public class player1 extends Activity implements Runnable { 


private MediaPlayer mp; 
// Handler to update UI timer, progress bar etc,. 
private Handler mHandler = new Handler();; 
private Utilities utils; 
private int seekForwardTime = 5000; // 5000 milliseconds 
private int seekBackwardTime = 5000; // 5000 milliseconds 
private int currentSongIndex = 0; 
private SeekBar songProgressBar; 
private ImageButton playicon; 
private ImageButton pauseicon; 
private TextView songCurrentDurationLabel; 
private TextView songTotalDurationLabel; 
private final int NUM_SOUND_FILES = 3; //*****REPLACE THIS WITH THE ACTUAL NUMBER OF SOUND FILES YOU HAVE***** 
private SeekBar seek; 
private int mfile[] = new int[NUM_SOUND_FILES]; 
private Random rnd = new Random(); 


    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.player_1); 
     songProgressBar = (SeekBar) findViewById(R.id.songProgressBar); 
     songTotalDurationLabel = (TextView) findViewById(R.id.songTotalDurationLabel); 
     songCurrentDurationLabel = (TextView) findViewById(R.id.songCurrentDurationLabel); 
     pauseicon = (ImageButton) findViewById(R.id.pauseicon); 
     getActionBar().setDisplayHomeAsUpEnabled(true); 


     mfile[0] = R.raw.sound01; //****REPLACE THESE WITH THE PROPER NAMES OF YOUR SOUND FILES 
     mfile[1] = R.raw.sound02; //PLACE THE SOUND FILES IN THE /res/raw/ FOLDER IN YOUR PROJECT***** 
     mfile[2] = R.raw.sound03; 
     // Listeners 
     /** 
     * Play button click event 
     * plays a song and changes button to pause image 
     * pauses a song and changes button to play image 
     * */ 


     try{ 
      mp = MediaPlayer.create(player1.this, mfile[rnd.nextInt(NUM_SOUND_FILES)]); 
      mp.seekTo(0); 
      mp.start(); 
      // set Progress bar values 
       songProgressBar.setProgress(0); 
       songProgressBar.setMax(mp.getDuration()); 
       new Thread(this).start(); 


     } catch (IllegalArgumentException e) { 
      e.printStackTrace(); 
     } catch (IllegalStateException e) { 
      e.printStackTrace(); 
     } 


     pauseicon.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       if (v.getId() == R.id.pauseicon) 
     if(mp.isPlaying()){ 
      mp.pause(); 
      ImageButton pauseicon =(ImageButton) findViewById(R.id.pauseicon); 

      pauseicon.setImageResource(R.drawable.playicon); 
     } else { 
      mp.start(); 

       ImageButton pauseicon =(ImageButton) findViewById(R.id.pauseicon); 

       pauseicon.setImageResource(R.drawable.pauseicon); 


      }}}); 
    } 


    public void run() { 
     int currentPosition= 0; 
     int total = mp.getDuration(); 
     while (mp!=null && currentPosition<total) { 
      try { 
       Thread.sleep(1000); 
       currentPosition= mp.getCurrentPosition(); 
      } catch (InterruptedException e) { 
       return; 
      } catch (Exception e) { 
       return; 
      }    
      songProgressBar.setProgress(currentPosition); 
     } 
    } 



    public void onStartTrackingTouch(SeekBar seekBar) { 
    } 

    public void onStopTrackingTouch(SeekBar seekBar) { 
    } 

    public void onProgressChanged(SeekBar seekBar, int progress, 
      boolean fromUser) { 
     if(fromUser) mp.seekTo(progress); 

    } 



    public boolean onOptionsItemSelected(MenuItem item){ 
     Intent myIntent = new Intent(getApplicationContext(), MainActivity.class); 
     startActivityForResult(myIntent, 0); 
     return true; 
     } 
    } 

답변

0

run() 메서드는 호출되지 않습니다. 대신 메인 스레드에 Handler 오브젝트를 작성하십시오 (이미했으나 사용되지 않았습니다). Thread.sleep()을 실행 메소드에서 제거하지만 run() 끝에있는 핸들러의 postDelayed() 메소드에 대한 호출을 추가하십시오 (어쩌면 호출 조건 재생하는 동안).

재생을 시작한 후 주 스레드에서 run() 메서드를 한 번 호출하십시오. 그런 다음 postDelayed()을 사용하여 자신을 호출하는 데주의를 기울입니다.

+0

안녕하세요, 자기 자신의 문제를 해결했지만 지금은 트랙을 스크롤 할 수 있도록하려고했지만 다시 스크롤 할 때 되돌아 가지 않으므로 코드를 업데이트했습니다. – user2407147

+0

@ user2407147 pauseicon 용 OnClickListener를 설정하는 것과 마찬가지로 SeekBar 용 OnSeekBarChangeListener를 설정해야합니다. 그건 그렇고 : setProgress()와 같은 GUI 요소의 메서드 호출은 주 스레드에서만 수행되어야하므로 (이 특정 메서드의 경우 작동) 다른 솔루션에서 솔루션이 실패 할 수 있습니다. –

관련 문제