android音乐播放器的问题

k1999hj 发布于 2014/06/27 14:33
阅读 507
收藏 0

package com.example.music;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import android.R.drawable;
import android.R.integer;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.UserHandle;
import android.provider.MediaStore;
import android.provider.MediaStore.Audio.Media;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.IntentSender;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.content.IntentSender.SendIntentException;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.AssetManager;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.content.res.Resources.Theme;
import android.database.Cursor;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.view.Display;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
class musicl{
	private long id;
	private String uri;
	private String name;
	private long m_time;
	private long size;
	private int sit;
	public void setSit(int sit) {
		this.sit = sit;
	}
	public int getSit() {
		return sit;
	}
	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	public String getUri() {
		return uri;
	}
	
	public void setUri(String uri) {
		this.uri = uri;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public long getM_time() {
		return m_time;
	}
	public void setM_time(long m_time) {
		this.m_time = m_time;
	}
	public long getSize() {
		return size;
	}
	public void setSize(long size) {
		this.size = size;
	}
}
public class MainActivity extends Activity {
	public int playorstop;
    public MediaPlayer mpl;
    public ListView lv_music;
    public Button btr_pos;
    private List<HashMap<String,String>> music_map;
    @Override   
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        playorstop=0;
        lv_music=(ListView)findViewById(R.id.listView1);
        btr_pos=(Button)findViewById(R.id.button1);
        music_map=new ArrayList<HashMap<String,String>>();
        Button btr_find=(Button)findViewById(R.id.button5);
        btr_find.setOnClickListener(new Button.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
		        List<musicl> music_l=new ArrayList<musicl>();
				Cursor cursor=getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null,   MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
				music_l.clear();
				for(int i=0;i<cursor.getCount();i++){
					musicl mp3=new musicl();
					long id=cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media._ID));
					String name=cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
					long m_time=cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION));
					long size=cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.SIZE));
					String uri=cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
					int ismusic=cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.IS_MUSIC));
					if(ismusic!=0){
						musicl ms=new musicl();
						ms.setId(id);
						ms.setName(name);
						ms.setM_time(m_time);
						ms.setSize(size);
						ms.setUri(uri);
						music_l.add(ms);
					}
				}
				for(Iterator ite=music_l.iterator();ite.hasNext();){
					musicl music_ev=(musicl)ite.next();
					HashMap<String,String> m_evmap=new HashMap<String, String>();
					m_evmap.put("name", music_ev.getName());
					m_evmap.put("uri",music_ev.getUri());
					music_map.add(m_evmap);
				}
				SimpleAdapter sadapter=new SimpleAdapter(getBaseContext(), music_map, R.layout.activity_main, new String[]{"name"},new int[]{R.id.textView3});
				lv_music.setAdapter(sadapter);
			}
		});
        lv_music.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long id) {
				// TODO Auto-generated method stub
				HashMap<String,String> get_map=(HashMap<String, String>)arg0.getItemAtPosition(position);
				if(mpl!=null){
					mpl.pause();
					mpl=null;
				}
				mpl=new MediaPlayer();
				try {
					mpl.setDataSource(MainActivity.this,Uri.parse(get_map.get("uri")
							));
				} catch (IllegalArgumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (SecurityException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalStateException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				try {
					mpl.prepare();
				} catch (IllegalStateException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				position=1;
				btr_pos.setBackgroundDrawable(getResources().getDrawable(R.drawable.stop));
			}
		});
        btr_pos.setOnClickListener(new Button.OnClickListener() {
			
			@SuppressWarnings("deprecation")
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if(mpl!=null){
					if(playorstop==0){
						btr_pos.setBackgroundDrawable(getResources().getDrawable(R.drawable.stop));
						mpl.start();
					}
					if(playorstop==1){
						btr_pos.setBackgroundDrawable(getResources().getDrawable(R.drawable.play));
						mpl.stop();
					}
				}
			}
		});
    }
    


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
}



       这是我写的音乐播放器,未完成而且连调试都崩溃,不过我想先问清楚一些问题,由于程序里面没有用到广播,我自己也没有学到那里,因此希望大家能在没有广播的情况下给点建议谢谢。

    首先想问下程序里面的遍历的多媒体数据库的一些问题,1:数据库里面是存了哪里的多媒体文件呢?就是说如果我想调试我是把音乐放到DDMS里面的file explor里面还是sd卡里面还是什么其它地方呢?还有file explor和sd卡还有手机内存三者的关系是怎么的呢?

    然后就是关于设置下一首和上一首歌的问题了,我是不是能够通过listviw或者是里面的Item来获取当前点击的位置呢?或者顺便得到这个listview各item的位置呢?然后我就可以按照位置,用模拟点击来实现下一首上一首歌了,我也查到过确实有模拟点击方法但是根本看不懂它的参数啊!哪位高人能示范下怎么用吗?还有怎么寻找那个位置呢?

    最后就是想问下大家我的思路行不行,还有用没用能用的API,我找到的都不能用。

    谢谢!!!!

加载中
返回顶部
顶部