该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

代码

public class videoFragment extends Fragment {

ListView lvMusic;

List musicBeanList;

MediaPlayer mp;

SeekBar sb;

int currentTime;

TextView tvTime,tvCurrent;

TextView tvName;

Thread thread;

Button btnPre,btnNext,btnPut;

int currentPos=-1;

boolean isPause=false;

Handler handler=new Handler(){

@Override

public void handleMessage(Message msg) {

super.handleMessage(msg);

sb.setProgress(currentTime);

tvCurrent.setText(getTime(currentTime));

}

};

public videoFragment() {

// Required empty public constructor

}

public void initMusic() throws IllegalAccessException {

Field[] fields=R.raw.class.getDeclaredFields();

musicBeanList=null;

musicBeanList=new ArrayList();

for(int i=0;i

MusicBean current=new MusicBean(fields[i].getInt(R.raw.class),fields[i].getName());

musicBeanList.add(current);

}

}

public void playMusic(){

MusicBean current=musicBeanList.get(currentPos);

int rawId=current.getId();

String name=current.getName();

if(thread!=null){

thread.interrupt();

thread=null;

}

if(mp!=null){

mp.stop();

mp.release();

mp=null;

}

try {

Uri uri = Uri.parse("android.resource://" + getContext().getPackageName() + "/" + rawId);

mp = new MediaPlayer();

mp.setDataSource(getActivity(), uri);

mp.setAudioStreamType(AudioManager.STREAM_MUSIC);

mp.prepare();

final int totalTime=mp.getDuration();

sb.setMax(totalTime);

tvTime.setText(getTime(totalTime));

tvName.setText(name);

thread=new Thread(){

@Override

public void run() {

super.run();

while (currentTime<=totalTime && thread!=null) {

currentTime = mp.getCurrentPosition();

handler.sendEmptyMessage(0);

try {

sleep(1000);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

};

thread.start();

mp.start();

}

catch (Exception e){

e.printStackTrace();

}

}

public String getTime(int ms){

int seconds=ms/1000;

int m=seconds/60;

int s=seconds%60;

String srtMin="";

String srtSec="";

if(m<10)

srtMin="0"+m;

else

srtMin=""+m;

if(s<10)

srtSec="0"+s;

else

srtSec=""+s;

String srt=srtMin+":"+srtSec;

return srt;

}

@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container,

Bundle savedInstanceState) {

// Inflate the layout for this fragment

View view = inflater.inflate(R.layout.fragment_video, container, false);

try {

initMusic();

} catch (IllegalAccessException e) {

e.printStackTrace();

}

lvMusic = view.findViewById(R.id.music_list);

sb = view.findViewById(R.id.pb_music);

tvTime = view.findViewById(R.id.tv_time);

tvCurrent = view.findViewById(R.id.tv_current);

btnPre = view.findViewById(R.id.btn_pre);

btnNext = view.findViewById(R.id.btn_next);

btnPut = view.findViewById(R.id.btn_put);

tvName = view.findViewById(R.id.tv_name2);

MusicAdapter adapter = new MusicAdapter(musicBeanList,getActivity());

lvMusic.setAdapter(adapter);

currentPos=0;

playMusic();

lvMusic.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override

public void onItemClick(AdapterView> parent, View view, int position, long id) {

currentPos=position;

playMusic();

}

});

btnPre.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

if(currentPos==0){

currentPos=musicBeanList.size()-1;

}else{

currentPos=currentPos-1;

}

playMusic();

}

});

btnNext.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

if(currentPos==musicBeanList.size()-1){

currentPos=0;

}else {

currentPos=currentPos+1;

}

playMusic();

}

});

btnPut.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

if(mp.isPlaying() &&! isPause){

mp.pause();

isPause=true;

}

else{

mp.start();

isPause=false;

}

}

});

sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

@Override

public void onProgressChanged(SeekBar seekBar, int i, boolean b) {

}

@Override

public void onStartTrackingTouch(SeekBar seekBar) {

}

@Override

public void onStopTrackingTouch(SeekBar seekBar) {

int sb1=sb.getProgress();

mp.seekTo(sb1);

}

});

return view;

}

}

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐