Android第三次作业

播放器页面:

 

 

部分代码:

  获取权限:

 
if(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(MainActivity.this,new String[]{
Manifest.permission.WRITE_EXTERNAL_STORAGE},1);
}else{
init();//初始化MediaPlayer
}

 

  切换歌曲:

 
public void previousMusic()throws IOException {
try {
currentListItme--;
if(currentListItme<0)
{
currentListItme=arrayList.size()-1;
}
playMusic(currentListItme);
}catch(IOException e){}

}
public void nextMusic()throws IOException {
try {
currentListItme++;
if(currentListItme>=arrayList.size())
{
currentListItme=0;
}
playMusic(currentListItme);
}catch(IOException e){}

}

 

  主要代码:

 
public void init()
{


File file = new File(Environment.getExternalStorageDirectory(),"music.mp3");
try {
mediaPlayer.setDataSource(file.getPath());//指定音频文件路径
mediaPlayer.prepare();//让mediaPlayer进入准备状态
} catch (IOException e) {
e.printStackTrace();
}


mListView = (ListView) findViewById(R.id.list_view);
adapter = new BaseAdapter() {
@Override
public int getCount() {
return arrayList.size();
}

@Override
public Object getItem(int position) {
return null;
}

@Override
public long getItemId(int position) {
return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView MusicItem = new TextView(MainActivity.this);

MusicItem.setText(arrayList.get(position).Name);
MusicItem.setTextSize(30);
return MusicItem;
}
};
mListView.setAdapter(adapter);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, "开始播放:"+arrayList.get(position).Name, Toast.LENGTH_LONG).show();
try {
playMusic(position);
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
public void playMusic(int position)throws IOException
{
try {
this.init();
mediaPlayer.reset();
mediaPlayer.setDataSource(arrayList.get(position).Url);
mediaPlayer.prepare();
mediaPlayer.start();
}catch (IOException e){}
}
MediaPlayer mediaPlayer = new MediaPlayer();
public void onClick(View v) {

switch (v.getId()) {
case R.id.id_previous: {
try {
previousMusic();
} catch (IOException e) {
e.printStackTrace();
}

}
break;
case R.id.id_stop_play: {

if (mediaPlayer == null ||mediaPlayer.isPlaying()) {
mediaPlayer.pause();
} else {
mediaPlayer.start();
}
}
break;
case R.id.next: {

try {
nextMusic();
} catch (IOException e) {
e.printStackTrace();
}
}
break;
case R.id.seekbar:
{
this.onStopTrackingTouch(seekBar);
}
default:
break;


}


}
public void previousMusic()throws IOException {
try {
currentListItme--;
if(currentListItme<0)
{
currentListItme=arrayList.size()-1;
}
playMusic(currentListItme);
}catch(IOException e){}

}
public void nextMusic()throws IOException {
try {
currentListItme++;
if(currentListItme>=arrayList.size())
{
currentListItme=0;
}
playMusic(currentListItme);
}catch(IOException e){}

}
public boolean onKeyDown(int keyCode,KeyEvent event)
{
if(keyCode==KeyEvent.KEYCODE_BACK)
{
mediaPlayer.stop();
mediaPlayer.release();
this.finish();
return true;
}
return super.onKeyDown(keyCode,event);


}

 

代码地址:https://coding.net/u/lwb1234/p/Music/git/tree/master/MyApplication3

apk下载地址:https://coding.net/u/lwb1234/p/Music/git/raw/master/1600802116.apk

posted @ 2018-12-11 21:20  1600802116喇文斌  阅读(149)  评论(0编辑  收藏  举报