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);
}
}*/
}
我想創建媒體播放器並播放所選擇的單擊媒體播放器上的項目點擊監聽器上的歌曲,我在音樂播放器活動中遇到問題,如何將選定歌曲名稱映射到歌曲,我必須將其保存在原始文件夾中?
當您單擊列表中的某首歌曲,你會得到你的代碼? 。歌曲名稱或歌曲網址?如果您有歌曲名稱,那麼從您的資源文件夾中獲取該歌曲並播放它。否則,如果您有歌曲網址,請取回並播放 – Signare
我們不知道您的歌曲位於何處。如果它在設備上,則用您在單擊列表字段時獲得的名稱進行搜索。 – Signare
我已存儲的所有歌曲列表中的原始文件夾,當我點擊所選項目列表就會去musicplayer活性,但對播放按鈕它給了空指針異常 –