2012-07-07 72 views
0

我正在製作一個android應用程序,我在這裏找到了一些音樂的seekbar。我得到了搜索欄來顯示音樂播放狀態,我現在在播放音樂。但是,我如何設置一個監聽器,以便我可以說mediaplayer會在軌道上播放特定的地方?我知道在mediaplayer中設置播放時間的代碼,但是如何處理來自seekbar的點擊事件?像onClick()或類似的東西?在android中使用seekbar

FIXED: 我的代碼:

package com.mycompany.appname; 

import java.io.File; 
import java.io.IOException; 
import java.util.ArrayList; 
import java.util.Arrays; 
import java.util.List; 

import android.app.ListActivity; 
import android.media.MediaPlayer; 
import android.os.Bundle; 
import android.os.CountDownTimer; 
import android.os.Environment; 
import android.view.KeyEvent; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.ListAdapter; 
import android.widget.ListView; 
import android.widget.SeekBar; 
import android.widget.SeekBar.OnSeekBarChangeListener; 
import android.widget.SlidingDrawer; 

public class MusicActivity extends ListActivity implements OnClickListener, OnSeekBarChangeListener { 
//Called when the activity is first created 
List<String> myList = new ArrayList<String>(); 
EditText AddItemToListViewEditText; 
    static final String[] COUNTRIES = new String[] { 
     "Movies" 
     }; 
    MediaPlayer mp1; 
    String musicUri; 
    Button PausePlay; 
    int positionInt; 
    SlidingDrawer slidingDrawer2; 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.musicscreen); 

    //Import views 
    PausePlay = (Button)findViewById(R.id.PausePlay); 
    slidingDrawer2 = (SlidingDrawer)findViewById(R.id.slidingDrawer2); 

    //Setup onClickListener for the buttons 
    PausePlay.setOnClickListener(this); 

    //Setup mediaPlayer 
    mp1 = new MediaPlayer(); 

    //Setup ListView 
    setListAdapter((ListAdapter) new ArrayAdapter<String>(this, R.layout.list_item, COUNTRIES)); 
    setListAdapter((ListAdapter) new ArrayAdapter<String>(this, R.layout.list_item, myList)); 

    //Setup seekBar 
    final SeekBar progress = (SeekBar)findViewById(R.id.musicProgressBar); 
    progress.setOnSeekBarChangeListener(this); 

    ListView lv = getListView(); 
    lv.setTextFilterEnabled(true); 

    File mFile = new File(Environment.getExternalStorageDirectory() + "/Music/"); 
    myList.addAll(Arrays.asList(mFile.list())); 
    setListAdapter((ListAdapter) new ArrayAdapter<String>(this, R.layout.list_item, myList)); 

    lv.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View view, 
      int position, long id) { 
     //When an item is clicked, play it on a MediaPlayer 
      //Play song 
      positionInt = position; 
      if (mp1.isPlaying()) { 
       //Reset mediaPlayer 
       mp1.reset(); 
       try { 
        musicUri = Environment.getExternalStorageDirectory() + "/Music/" + myList.get(positionInt); 
       mp1.setDataSource(musicUri); 
       mp1.prepare(); 
       mp1.start(); 
       slidingDrawer2.animateOpen(); 
      } catch (IllegalArgumentException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IllegalStateException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      } else { 
      try { 
       musicUri = Environment.getExternalStorageDirectory() + "/Music/" + myList.get(position); 
      mp1.setDataSource(musicUri); 
      mp1.prepare(); 
      mp1.start(); 
      slidingDrawer2.animateOpen(); 
     } catch (IllegalArgumentException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IllegalStateException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

      //Setup seekBar 
      final SeekBar progress = (SeekBar)findViewById(R.id.musicProgressBar); 
      progress.setProgress(0); 
      progress.setMax(mp1.getDuration()); 
      new CountDownTimer(mp1.getDuration(), 250) { 
      public void onTick(long millisUntilFinished) { 
       if (progress.getProgress() == mp1.getDuration()) { 
        mp1.reset(); 
        progress.setProgress(0); 
       } else { 
        progress.setProgress(mp1.getCurrentPosition()); 
       } 
      } 
      public void onFinish() {} 
      }.start(); 
     } 
    } 
    }); 
} 

@Override 
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { 
    if (fromUser == true) { 
     mp1.seekTo(seekBar.getProgress()); 
    } else { 
     //Do nothing 
    } 
} 

@Override 
public boolean onKeyDown(int KeyCode, KeyEvent event) { 
    if ((KeyCode == KeyEvent.KEYCODE_BACK)) { 
     if (mp1.isPlaying()) { 
      mp1.reset(); 
      System.exit(0); 
     } else { 
      finish(); 
     } 
     return true; 
    } 
    return super.onKeyDown(KeyCode, event); 
} 

public void onClick(View src) { 
    switch(src.getId()) { 
    case R.id.PausePlay: 
     if (mp1.isPlaying()) { 
      mp1.pause(); 
      PausePlay.setText("Play"); 
     } else { 
      mp1.start(); 
      PausePlay.setText("Pause"); 
     } 
     break; 
    } 
} 

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

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

我的XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 


<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Musikk" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 

<FrameLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" > 




    <ListView 
     android:id="@+android:id/list" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" > 

    </ListView> 


    <SlidingDrawer 
     android:id="@+id/slidingDrawer2" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:content="@+id/content" 
     android:handle="@+id/handle" > 


     <Button 
      android:id="@+id/handle" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Spiller nå" /> 


     <LinearLayout 
      android:id="@+id/content" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:orientation="vertical" > 


      <SeekBar 
       android:id="@+id/musicProgressBar" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" /> 

      <LinearLayout 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" > 













       <Button 
        android:id="@+id/PausePlay" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="Pause" /> 

      </LinearLayout> 

     </LinearLayout> 
    </SlidingDrawer> 

</FrameLayout> 

</LinearLayout> 
+0

註冊OnSeekBarChanged監聽你也應該看看[Java變量的命名規則(http://docs.oracle的.com/JavaSE的/教程/ JAVA/nutsandbolts/variables.html)。特別是該頁上的最後一個項目符號。非最終變量(例如您的PausePlay變量)應該以小寫字母開頭。以大寫字母開頭的Camelcase應該保留給類。 – 2012-07-07 18:52:02

回答

0

你想實現SeekBar.OnSeekBarChangeListener,然後實現onProgressChanged方法。

當進度發生變化時(例如,用戶點擊搜索欄上的某個地方),將調用此函數,併爲您提供新位置。

例子:

public void onProgressChanged (SeekBar seekBar, int progress, boolean fromUser) { 
    // Do something 
} 

不要忘了seekBar.setOnseekBarChangeListener

+1

你能寫出這個方法嗎? – user1446632 2012-07-07 16:01:29

+0

你看過那個鏈接嗎?它應該有你需要的一切。 我用一個示例方法更新了我的答案。儘管如此,我不確定你希望從開發人員文檔中複製該方法。 – 2012-07-07 16:11:37

+0

我不應該重寫該方法嗎? – user1446632 2012-07-07 16:15:44

0
public void onProgressChanged(SeekBar seekBar, int progress, 
        boolean fromUser) { 
       // TODO Auto-generated method stub 
       if (fromUser) { 
        mp1.seekTo(progress); 
        seekBar.setProgress(progress); 
       } 

      }