create: 创建一个要播放的多媒体
getCurrentPosition:得到当前播放位置
getDuration: 得到文件的时间
prepare: 准备(同步)
prepareAsync:准备(异步)
setLooping(true) 单曲循环
seekTo: 指定播放的位置(以毫秒为单位)
setAudioStreamType: 设置流媒体的类型
setDataSource: 设置数据来源
setDisplay: 设置用SurfaceHolder来显示多媒体
setOnBufferingUpdateListener: 网络流媒体的缓冲监听
setOnErrorListener: 设置错误信息监听
setOnVideoSizeChangedListener:视频尺寸监听
setScreenOnWhilePlaying: 设置是否使用SurfaceHolder来显示
setVolume: 设置音量
//获取sd卡上的音频文件
setDataSource(“/sdcard/test.mp3”);
//装载资源中的音乐
MediaPlayer.create(Activity01.this,R.raw.test);
//目前存在问题,不能循环解析出音频文件
原因:.android_secure文件夹受保护,无法获取里面的文件信息
播放视频
相关类:VideoView
方法说明:
getBufferPercentage:得到缓冲的百分比
getCurrentPosition:得到当前播放位置
getDuration:得到视频文件的时间
resolveAdjustedSize:调整视频显示大小
setMediaController:设置播放控制器模式(播放进度条)
setOnCompletionListener:当视频文件播放完时触发事件
setVideoPath:设置视频源路径
setVideoURI:设置视频源地址
package com.example.eighttween;
import android.Manifest;
import android.app.Service;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Intent;
import android.content.ServiceConnection;
import android.database.Cursor;
import android.net.Uri;
import android.os.IBinder;
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.Button;
import android.widget.ImageView;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
ListView listView;
Button b_loop;
Button b_sui;
Button b_top;
Button b_but;
ImageView sta_sto;
MyService myService;
static boolean flag=false;
List>mapList=new ArrayList<>();
ArrayListarr=new ArrayList<>();
int pos=0;
ServiceConnection connection=new ServiceConnection(){
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
myService=((MyService.Music)service).getService();
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView=findViewById(R.id.list);
b_loop=findViewById(R.id.loop);
b_sui=findViewById(R.id.sui);
b_top=findViewById(R.id.top);
b_but=findViewById(R.id.below);
sta_sto=findViewById(R.id.start_stop);
final Intent intent = new Intent(this, MyService.class);
bindService(intent,connection, Service.BIND_AUTO_CREATE);
listView.getBackground().setAlpha(50);
ContentResolver resolver = getContentResolver();
Uri uri= MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String[] str={MediaStore.Audio.Media.TITLE,MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.DURATION,MediaStore.Audio.Media.DATA};
Cursor cursor = resolver.query(uri, str, null, null, null);
while (cursor.moveToNext()){
String Song_Name = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
String sing_song= cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
String Song_Time = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION));
String Song_Data = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
HashMap hashMap = new HashMap<>();
hashMap.put("S_name",Song_Name+" "+sing_song+" "+Song_Time+" "+Song_Data);
hashMap.put("data",Song_Data);
arr.add(Song_Data);
mapList.add(hashMap);
}
final MyAdapter myAdapter = new MyAdapter(MainActivity.this, mapList);
listView.setAdapter(myAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView> parent, View view, int position, long id) {
pos=position;
HashMap map = mapList.get(position);
String data = map.get("data").toString();
Log.e("##",data);
Intent intent=new Intent(MainActivity.this,MyService.class);
intent.putExtra("action","play");
intent.putExtra("aa",data);
startService(intent);
}
});
b_loop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,MyService.class);
intent.putExtra("action","Loop");
startService(intent);
}
});
b_sui.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Random random = new Random(5);
Collections.shuffle(arr,random);
Intent intent=new Intent(MainActivity.this,MyService.class);
String s = arr.get(0).toString();
intent.putExtra("action","Shuffle");
intent.putExtra("math",s);
startService(intent);
}
});
b_top.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent= new Intent(MainActivity.this,MyService.class);
intent.putExtra("action","Top");
if(pos hashMap = mapList.get(pos);
String s = hashMap.get("data").toString();
intent.putExtra("tt",s);
startService(intent);
}
});
b_but.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent= new Intent(MainActivity.this,MyService.class);
intent.putExtra("action","but");
--pos;
if(pos<0){
pos=mapList.size()-1;
}
HashMap hashMap = mapList.get(pos);
String ss = hashMap.get("data").toString();
intent.putExtra("ss",ss);
startService(intent);
}
});
sta_sto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(flag==false){
sta_sto.setImageResource(R.drawable.start);
Intent intent= new Intent(MainActivity.this, MyService.class);
intent.putExtra("action","stop");
startService(intent);
flag=true;
}else{
sta_sto.setImageResource(R.drawable.stop);
HashMapmap= mapList.get(pos);
String data=map.get("data").toString();
Intent intent=new Intent(MainActivity.this,MyService.class);
intent.putExtra("action","play");
intent.putExtra("aa",data);
startService(intent);
flag=false;
}
}
});
}
class bean{
String data;
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
@Override
public String toString() {
return "bean{" +
"data='" + data + '\'' +
'}';
}
}
}
package com.example.eighttween;
import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.HashMap;
import java.util.List;
class MyAdapter extends BaseAdapter {
Activity activity;
List>mapList;
public MyAdapter(MainActivity mainActivity, List> mapList) {
this.activity=mainActivity;
this.mapList=mapList;
}
@Override
public int getCount() {
return mapList.size();
}
@Override
public Object getItem(int position) {
return mapList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
holder ho = null;
if(convertView==null){
convertView=View.inflate(activity,R.layout.myitem,null);
ho=new holder();
ho.textView= convertView.findViewById(R.id.t_item);
convertView.setTag(ho);
}else{
ho=(holder)convertView.getTag();
}
HashMap map = mapList.get(position);
if(ho!=null){
ho.textView.setText(map.get("S_name").toString());
}
return convertView;
}
class holder{
TextView textView;
}
}
package com.example.eighttween;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Binder;
import android.os.IBinder;
public class MyService extends Service {
private MediaPlayer mediaPlayer;
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
return new Music();
}
class Music extends Binder{
public MyService getService(){
return MyService.this;
}
}
public void stop(){
mediaPlayer.stop();
mediaPlayer.reset();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
String action = intent.getStringExtra("action");
switch (action){
case "play":
if(mediaPlayer==null){
String aa = intent.getStringExtra("aa");
mediaPlayer=MediaPlayer.create(this, Uri.parse(aa));
mediaPlayer.start();
}else{
stop();
String aa = intent.getStringExtra("aa");
mediaPlayer=MediaPlayer.create(this, Uri.parse(aa));
mediaPlayer.start();
}
break;
case "stop":
if(mediaPlayer!=null){
mediaPlayer.stop();
mediaPlayer.reset();
mediaPlayer.release();
mediaPlayer=null;
}
break;
case "loop":
if(mediaPlayer!=null&&mediaPlayer.isLooping()){
mediaPlayer.setLooping(true);
}
break;
case "Shuffle":
if(mediaPlayer==null){
String aa = intent.getStringExtra("math");
mediaPlayer=MediaPlayer.create(this, Uri.parse(aa));
mediaPlayer.start();
}else{
stop();
String aa = intent.getStringExtra("math");
mediaPlayer=MediaPlayer.create(this, Uri.parse(aa));
mediaPlayer.start();
}
break;
case "Top":
if(mediaPlayer==null){
String aa = intent.getStringExtra("tt");
mediaPlayer=MediaPlayer.create(this, Uri.parse(aa));
mediaPlayer.start();
}else{
stop();
String ss=intent.getStringExtra("tt");
mediaPlayer=MediaPlayer.create(this, Uri.parse(ss));
mediaPlayer.start();
}
break;
case "but":
if(mediaPlayer==null){
String aa = intent.getStringExtra("ss");
mediaPlayer=MediaPlayer.create(this, Uri.parse(aa));
mediaPlayer.start();
}else{
stop();
String sss=intent.getStringExtra("ss");
mediaPlayer=MediaPlayer.create(this, Uri.parse(sss));
mediaPlayer.start();
}
break;
}
return super.onStartCommand(intent, flags, startId);
}
}