zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    bzoj1015星球大战(并查集+离线)
    bzoj1085骑士精神(搜索)
    bzoj1051受欢迎的牛(Tarjan)
    左偏树学习
    hdu1512 Monkey King(并查集,左偏堆)
    左偏树(模板)
    PAT (Basic Level) Practice (中文) 1079 延迟的回文数 (20分) (大数加法)
    PAT (Basic Level) Practice (中文) 1078 字符串压缩与解压 (20分) (字符转数字——栈存放)
    PAT (Basic Level) Practice (中文) 1077 互评成绩计算 (20分) (四舍五入保留整数)
    PAT (Basic Level) Practice (中文) 1076 Wifi密码 (15分)
  • 原文地址:https://www.cnblogs.com/qwer-lwb/p/10105015.html
Copyright © 2011-2022 走看看