zoukankan      html  css  js  c++  java
  • 音乐播放

    1.扫描sdcard的mp3文件,点击能够播放

    2. 扫描到文件显示在listview中

    <1> 配置文件

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="myapplication.com.sdmp3">
    
        <uses-sdk
            android:minSdkVersion="14"
            android:targetSdkVersion="23" />
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
            <activity android:name=".MainActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    
    </manifest>
    View Code

    <2> 布局文件

     <ListView
            android:id="@+id/mListView"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
        </ListView>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <TextView
            android:id="@+id/tv_file"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:textColor="#000000"
            android:gravity="center_vertical"
            android:text="" />
    
        <TextView
            android:id="@+id/tc_fileName"
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:textColor="#000000"
             />
        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="#ff0000"/>
    
    </LinearLayout>

    <3> .adapter

    package myapplication.com.sdmp3;
    
    import android.content.Context;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.TextView;
    
    import java.util.List;
    import java.util.zip.Inflater;
    
    /**
     * Created by Administrator on 2016/10/11.
     */
    public class adapter extends BaseAdapter {
        Context context;
        List<MuiDao> datas;
    
        public adapter(Context context, List<MuiDao> datas) {
            this.context = context;
            this.datas = datas;
        }
    
        @Override
        public int getCount() {
            return datas.size();
        }
    
        @Override
        public Object getItem(int position) {
            return datas.get(position);
        }
    
        @Override
        public long getItemId(int position) {
            return position;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder=null;
            if(convertView==null){
                convertView = View.inflate(context, R.layout.sd_list, null);
                holder = new ViewHolder();
                holder.textView1 = (TextView) convertView.findViewById(R.id.tv_file);
                holder.textView2 = (TextView) convertView.findViewById(R.id.tc_fileName);
                convertView.setTag(holder);
            }
    
            else{
            holder= (ViewHolder) convertView.getTag();
            }
            MuiDao bean=datas.get(position);
            String str=bean.getAddress();
            String name1[]=str.split("-");
            holder.textView1.setText(name1[0]);
    
            holder.textView2.setText(bean.getAddress());
            return convertView;
        }
        class ViewHolder{
            TextView textView1,textView2;
        }
    }
    View Code

      .bean

    package myapplication.com.sdmp3;
    
    /**
     * Created by Administrator on 2016/10/11.
     */
    public class MuiDao {
        public String name;
        public String address;
    
        public MuiDao(String address, String name) {
            this.address = address;
            this.name = name;
        }
    
        public String getAddress() {
            return address;
        }
    
        public void setAddress(String address) {
            this.address = address;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    }
    View Code

      .MainActivity

    package myapplication.com.sdmp3;
    
    import android.content.Intent;
    import android.media.MediaPlayer;
    import android.os.Environment;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.ListView;
    import android.widget.SimpleAdapter;
    import android.widget.Toast;
    
    import java.io.File;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    
    public class MainActivity extends AppCompatActivity {
        private ListView mListView;
        private ArrayList name;
        List<MuiDao> datas;
        String address;
        MediaPlayer player;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            mListView = (ListView) findViewById(R.id.mListView);
            name = new ArrayList();
            player = new MediaPlayer();
    
            datas=new ArrayList<>();
            address=Environment.getExternalStorageDirectory()+"";
    
            if (Environment.getExternalStorageState().equals(
                    Environment.MEDIA_MOUNTED)) {
                File path = Environment.getExternalStorageDirectory(); // 获得SD卡路径
                File[] files = path.listFiles();// 读取
                getFileName(files);
            }
    
    //        SimpleAdapter adapter = new SimpleAdapter(MainActivity.this, name,
    //                R.layout.sd_list, new String[] { "Name" },
    //                new int[] { R.id.tc_fileName });
            adapter adapter1=new adapter(getApplicationContext(),datas);
            mListView.setAdapter(adapter1);
            mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    
                    MuiDao bean  = (MuiDao) parent.getItemAtPosition(position);
                    String path=bean.getName();
                    player.stop();
    
                    Toast.makeText(MainActivity.this,path,Toast.LENGTH_SHORT).show();
                    try {
                        player=new MediaPlayer();
                        player.setDataSource(path);
                        player.prepare();
                        player.start();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
    
    
                }
            });
        }
    
        private void getFileName(File[] files) {
            if (files != null)// 先判断目录是否为空,否则会报空指针
            {
                for (File file : files) {
                    if (file.isDirectory()) {
                        getFileName(file.listFiles());
    
                    } else {
                        String fileName = file.getName();
                        if (fileName.endsWith(".mp3")) {
                          address=file.getAbsolutePath();
                            HashMap map = new HashMap();
                            String s = fileName.substring(0,
                                    fileName.lastIndexOf("."));
                            System.out.println("***"+address);
                           datas.add(new MuiDao(fileName.substring(0,fileName.lastIndexOf(".")),address));
                            map.put("Name", fileName.substring(0,fileName.lastIndexOf(".")));
                            name.add(map);
                        }
                    }
                }
            }
        }
    
    }
    View Code

    后面呢:

    <1>. 播放进度条实现

    <2>. 断点播放

    <3>.播放完毕自动顺序的播放

    <4>.播放模式的实现

    <5>.数据库的加入,避免扫描浪费时间

    今天多一点积累,明天少一分烦恼
  • 相关阅读:
    directX根据设备类GUID查询所属的filter 分类: DirectX 2014-09-20 08:34 501人阅读 评论(0) 收藏
    directX枚举系统设备类 分类: DirectX 2014-09-20 08:30 491人阅读 评论(0) 收藏
    INF 右键安装驱动以及卸载 分类: 生活百科 2014-09-18 14:03 573人阅读 评论(0) 收藏
    AVStream ddk 翻译 分类: DirectX 2014-09-13 10:43 534人阅读 评论(0) 收藏
    SCADA系统 分类: 生活百科 2014-09-11 17:26 627人阅读 评论(0) 收藏
    VxWorks操作系统shell命令与调试方法总结 分类: vxWorks 2014-08-29 14:46 1191人阅读 评论(0) 收藏
    winhex中判断+MBR+DBR+EBR方法 分类: VC++ 2014-08-27 09:57 443人阅读 评论(0) 收藏
    解析FAT16文件系统 分类: 生活百科 2014-08-27 09:56 580人阅读 评论(1) 收藏
    win9x_win2k下对物理磁盘的操作 分类: VC++ 磁盘的扇区读写 2014-08-27 09:55 421人阅读 评论(0) 收藏
    FAT32文件系统的存储组织结构(二) 分类: VC++ 2014-08-27 09:15 467人阅读 评论(0) 收藏
  • 原文地址:https://www.cnblogs.com/galibujianbusana/p/5950187.html
Copyright © 2011-2022 走看看