2015-04-08 80 views
1
public class MarathiActivity extends Activity 
{ 
    ListView list; 

    String[] web = { 
      "song name 1 ", 
      "song name 2", 
      "song name 3", 
      "song name 4", 
      "song name 5", 
      "song name 6", 
      "song name 7", 
      "song name 8" 

    } ; 

    Integer[] imageId = { 
      R.mipmap.ic_launcher, 
      R.mipmap.ic_launcher, 
      R.mipmap.ic_launcher, 
      R.mipmap.ic_launcher, 
      R.mipmap.ic_launcher, 
      R.mipmap.ic_launcher, 
      R.mipmap.ic_launcher 
    }; 

    public int[] resID = { 
      R.raw.song1, 
      R.raw.song2, 
      R.raw.song3, 
      R.raw.song4, 
      R.raw.song5, 
      R.raw.song6, 
      R.raw.song7, 
      R.raw.song8 
    }; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.marathi); 
     CustomList adapter = new 
       CustomList(MarathiActivity.this, web); 
     list=(ListView)findViewById(R.id.list); 
     list.setAdapter(adapter); 
     list.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

       Intent i = new Intent(MarathiActivity.this,MusicPlayer.class); 
       startActivity(i); 
       // playSong(position); 

      } 
     }); 

其歌曲馬拉名單,從列表中選擇歌曲,瀏覽到媒體播放器上的項目點擊監聽器和媒體播放器播放歌曲

public class MusicPlayer extends Activity 
{ 
    private MediaPlayer mediaPlayer; 
    public TextView songName, duration; 
    private double timeElapsed = 0, finalTime = 0; 
    private int forwardTime = 2000, backwardTime = 2000; 
    private Handler durationHandler = new Handler(); 
    private SeekBar seekbar; 
    MarathiActivity ma; 
    int position0; 



    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     //set the layout of the Activity 
     setContentView(R.layout.musicplayer); 

     //initialize views 
     initializeViews(); 
    } 

    public void initializeViews(){ 
     songName = (TextView) findViewById(R.id.songName); 

     mediaPlayer = MediaPlayer.create(this,R.raw.songname); 
     finalTime = mediaPlayer.getDuration(); 
     duration = (TextView) findViewById(R.id.songDuration); 
     seekbar = (SeekBar) findViewById(R.id.seekBar); 
     songName.setText("Sample_Song.mp3"); 

     seekbar.setMax((int) finalTime); 
     seekbar.setClickable(false); 
    } 

    // play mp3 song 
    public void play(View view) { 
     mediaPlayer.start(); 
     timeElapsed = mediaPlayer.getCurrentPosition(); 
     seekbar.setProgress((int) timeElapsed); 
     durationHandler.postDelayed(updateSeekBarTime, 100); 
    } 

    //handler to change seekBarTime 
    private Runnable updateSeekBarTime = new Runnable() { 
     public void run() { 
      //get current position 
      timeElapsed = mediaPlayer.getCurrentPosition(); 
      //set seek bar progress 
      seekbar.setProgress((int) timeElapsed); 
      //set time remaining 
      double timeRemaining = finalTime - timeElapsed; 
      duration.setText(String.format("%d min, %d sec", TimeUnit.MILLISECONDS.toMinutes((long) timeRemaining), TimeUnit.MILLISECONDS.toSeconds((long) timeRemaining) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes((long) timeRemaining)))); 

      //repeat yourself that again in 100 milliseconds 
      durationHandler.postDelayed(this, 100); 
     } 
    }; 

    // pause mp3 song 
    public void pause(View view) { 
     mediaPlayer.pause(); 
    } 

    /* // go forward at forwardTime seconds 
    public void forward(View view) { 
     //check if we can go forward at forwardTime seconds before song endes 
     if ((timeElapsed + forwardTime) 0) { 
      timeElapsed = timeElapsed - backwardTime; 

      //seek to the exact second of the track 
      mediaPlayer.seekTo((int) timeElapsed); 
     } 
    }*/ 

} 

我想創建媒體播放器並播放所選擇的單擊媒體播放器上的項目點擊監聽器上的歌曲,我在音樂播放器活動中遇到問題,如何將選定歌曲名稱映射到歌曲,我必須將其保存在原始文件夾中?

+0

當您單擊列表中的某首歌曲,你會得到你的代碼? 。歌曲名稱或歌曲網址?如果您有歌曲名稱,那麼從您的資源文件夾中獲取該歌曲並播放它。否則,如果您有歌曲網址,請取回並播放 – Signare

+0

我們不知道您的歌曲位於何處。如果它在設備上,則用您在單擊列表字段時獲得的名稱進行搜索。 – Signare

+0

我已存儲的所有歌曲列表中的原始文件夾,當我點擊所選項目列表就會去musicplayer活性,但對播放按鈕它給了空指針異常 –

回答

0

請修改如下面的代碼

list.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

       Intent i = new Intent(MarathiActivity.this,MusicPlayer.class); 
       i.putExtra("SongName", web[position]); 
       i.putExtra("Song", resID[position]); 
       startActivity(i); 
       // playSong(position); 

      } 
     }); 

MusicPlayer.class

String m_songName; 
    int m_songRes; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     //set the layout of the Activity 
     setContentView(R.layout.musicplayer); 


     if(getIntent().getExtras()!= null) 
     { 
      m_songName = getIntent().getExtras().getString("SongName"); 
      m_songRes = getIntent().getExtras().getInt("Song"); 
     } 

     //initialize views 
     initializeViews(); 
    } 

    public void initializeViews(){ 
     songName = (TextView) findViewById(R.id.songName); 

     mediaPlayer = MediaPlayer.create(this,m_songRes); 
     finalTime = mediaPlayer.getDuration(); 
     duration = (TextView) findViewById(R.id.songDuration); 
     seekbar = (SeekBar) findViewById(R.id.seekBar); 
     songName.setText(m_songName); 

     seekbar.setMax((int) finalTime); 
     seekbar.setClickable(false); 
    } 
+0

感謝它對我有用.. –

0

我真的不明白,你想要什麼,但儘量要做到這一點:

public class MySongs { 
public String name; 
public int imageId; 
public int songRawId; 

public MySongs(String name, int imageId, int songRawId) { 
    this.name = name; 
    this.imageId = imageId; 
    this.songRawId = songRawId; 
}  

}

然後你可以使用你的歌曲列表:

List<MySongs> songs = new ArrayList<MySongs>(); 

在OnCreate你可以添加你想擁有的所有歌曲,:

songs.add(new MySongs("name1", R.mipmap.ic_launcher, R.raw.song1)); 
songs.add(new MySongs("name2", R.mipmap.ic_launcher, R.raw.song2)); 
... 

之後,你可以把這個列表放入你的listAdapter。在OnItemClick只是做MySongs selectedSong = songs.get(position);

然後,你必須對所選歌曲所有信息。你可以用selectedSong.name讓他們作爲例子

+0

我只想玩媒體播放器中保存在原始文件夾中的所選歌曲 –

相關問題