码迷,mamicode.com
首页 > 其他好文 > 详细

扫描SD卡,显示歌曲信息,简单播放MP3,层层低级掌握服务.............

时间:2016-02-24 22:41:07      阅读:354      评论:0      收藏:0      [点我收藏+]

标签:

一,在主线程直接完成这一切,app退出,歌曲不播放了....(掌握contentResolver读取media信息和播放mp3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package china.playsong;
 
import android.app.ListActivity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;
 
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
 
public class MainActivity extends ListActivity implements AdapterView.OnItemClickListener {
 
    private ContentResolver resolver;
    private List<Bean> datas;
    private MediaPlayer player;
    private int currPlay = 0;
 
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        setListAdapter(new MyAdapter(datas, this));
        getListView().setOnItemClickListener(this);
 
    }
 
 
    private void initView() {
 
        resolver = getContentResolver();
        datas = new ArrayList<Bean>();
        getDataFromPrivoder();
        player = new MediaPlayer();
 
 
        player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
 
                //自动播放下一曲
                currPlay = currPlay + 1;
 
                if (currPlay < datas.size()) {
 
                    //如果不是最后一曲
                    String pathNext = datas.get(currPlay).getPath();
                    palySong(pathNext);
 
                } else {
                    //如果是最后一曲,则归零
                    currPlay = 0;
                    String pathZore = datas.get(0).getPath();
                    palySong(pathZore);
 
                }
            }
        });
    }
 
 
    private void getDataFromPrivoder() {
 
        //过滤其他信息,只取MP3
        Cursor cursor = resolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, "MIME_TYPE=?", new String[]{"audio/mpeg"}, null);
 
        //有时候参数设置错误,可能导致cursor的为空,所以加一个判断
        if (cursor.getCount() != 0) {
 
 
            for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
 
                String title = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
                String songName = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
                String path = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
                String size = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.SIZE)) + "";
 
                //加入数据
                datas.add(new Bean(title, songName, path, size));
 
            }
        } else {
            //提示为空
            Log.i("ddd", "cursor is empty,please check!");
            Toast.makeText(MainActivity.this, "cursor数据为空,请检查参数!", Toast.LENGTH_SHORT).show();
        }
 
 
    }
 
 
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
        //当前点击的位置传给全局变量
        currPlay = position;
 
        //播放歌曲
        palySong(datas.get(position).getPath());
    }
 
 
    public void palySong(String path) {
 
        try {
            //检查清空
            if (player.isPlaying()) {
 
                player.stop();
            }
 
            //因为在主线程,所以把播放的歌曲信息显示在当前屏幕第上面
            getListView().setSelection(currPlay);
 
            player.reset();
            player.setDataSource(path);
            player.prepare();
            player.start();
            //歌曲播放完之后,进入onCompleteListener
 
        } catch (IOException e) {
            e.printStackTrace();
        }
 
    }
 
    @Override
    protected void onDestroy() {
 
        if (player.isPlaying()) {
 
            player.stop();
        }
 
        player.release();
 
        super.onDestroy();
    }
 
 
}

?其他的Adapter就是简单的继承baseAdapter的,item的layout就是四个textView;



二,过度到用Server播放,这里简单满足一下功能,(退出app后,继续后台播放,这下面没有添加在界面停止服务的功能,需要在应用程序中停止......  )这里仅仅是应用一下service

这里的MainActivity只是获取一下数据,添加点击监听,打开服务....

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package china.playsongbyservice;
 
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
 
import java.util.ArrayList;
import java.util.List;
 
public class MainActivity extends AppCompatActivity {
 
    private ListView listView;
    private List<Bean> lists;
    private ContentResolver resolver;
    protected static List<String> paths;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }
 
    private void initView() {
 
        listView = (ListView) findViewById(R.id.listView);
        lists = new ArrayList<Bean>();
        //把path的路径集中,方便读取,因为这次service不与主线程互相通信
        paths = new ArrayList<String>();
        resolver =getContentResolver();
        getDatas();
        listView.setAdapter(new DemoAdapter(this, lists));
 
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
 
                 
                //打开服务
                Intent intent = new Intent(MainActivity.this,PlayService.class);
                intent.putExtra("pos", i);
                intent.putExtra("path", lists.get(i).getPath());
                startService(intent);
            }
        });
    }
 
     
    //获取数据.....
    private void getDatas() {
 
        Cursor cursor = resolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, "MIME_TYPE=?", new String[]{"audio/mpeg"}, null);
 
        if (cursor.getCount() != 0) {
 
            for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
 
                String mtitle = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
                String mName = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
                String mType = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.MIME_TYPE));
                String mPath = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
 
                lists.add(new Bean(mtitle, mName, mType, mPath));
 
            }
        } else {
            //提示为空
            Log.i("ddd", "cursor is empty,please check!");
            Toast.makeText(MainActivity.this, "cursor数据为空,请检查参数!", Toast.LENGTH_SHORT).show();
        }
 
        if (lists != null) {
 
            for (int i = 0; i < lists.size(); i++) {
 
                paths.add(lists.get(i).getPath());
 
            }
        }
 
    }
}

再写一个继承Service的类,基本是第一种主线程的移花接木

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package china.playsongbyservice;
 
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.support.annotation.Nullable;
 
import java.io.IOException;
 
/**
 * Created by Anonymous on 2016/2/24.
 */
public class PlayService extends Service {
 
    private MediaPlayer player;
    private int currentPos;
 
 
    @Override
    public void onCreate() {
        super.onCreate();
 
        player = new MediaPlayer();
        player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
                //自动下一曲
                currentPos = currentPos + 1;
 
                if (currentPos < MainActivity.paths.size()) {
                    String playNext = MainActivity.paths.get(currentPos);
                    playSong(playNext);
                } else {
                    currentPos = 0;
                    String playZero = MainActivity.paths.get(0);
                    playSong(playZero);
                }
            }
        });
    }
 
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
 
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
 
        //      在这里赋值
 
        String playPath = intent.getStringExtra("path");
        currentPos = intent.getIntExtra("pos", 0);
        playSong(playPath);
 
 
        return super.onStartCommand(intent, flags, startId);
    }
 
    @Override
    public void onDestroy() {
 
        if (player != null) {
 
            player.stop();
            player.release();
        }
        super.onDestroy();
    }
 
    public void playSong(String str) {
 
        try {
 
            if (player.isPlaying()) {
 
                player.stop();
            }
 
            player.reset();
            player.setDataSource(str);
            player.prepare();
            player.start();
 
        } catch (IOException e) {
            e.printStackTrace();
        }
 
 
    }
 
}




? 

       





扫描SD卡,显示歌曲信息,简单播放MP3,层层低级掌握服务.............

标签:

原文地址:http://www.cnblogs.com/share2015/p/5215222.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!