当前位置: 代码迷 >> Android >> Android上MP3播放器的实现源代码02
  详细解决方案

Android上MP3播放器的实现源代码02

热度:51   发布时间:2016-05-01 16:49:10.0
Android下MP3播放器的实现源代码02
第七个类ListActivity

package com.alex.media;import android.app.Activity;import android.app.AlertDialog;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.database.Cursor;import android.media.AudioManager;import android.net.Uri;import android.os.Bundle;import android.os.Environment;import android.provider.MediaStore;import android.view.KeyEvent;import android.view.View;import android.widget.AdapterView;import android.widget.LinearLayout;import android.widget.ListView;import android.widget.TextView;import android.widget.AdapterView.OnItemClickListener;import android.widget.LinearLayout.LayoutParams;public class ListActivity extends Activity {	private static final String MUSIC_LIST = "com.alex.list";	private ListView listview;	private int[] _ids;	private String[]_titles;	private int pos;	private  MusicListAdapter adapter;	private ScanSdReceiver scanSdReceiver = null;	private AlertDialog ad = null;	private AlertDialog.Builder  builder = null;		//关于音量的变量	private AudioManager mAudioManager = null;	private int maxVolume;//最大音量	private int currentVolume;//当前音量    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);      //注册广播接收器        IntentFilter filter = new IntentFilter();        filter.addAction(MUSIC_LIST);        registerReceiver(changeItem, filter);                //测试是否有音乐在播放        Intent intent = new Intent();        intent.setAction("com.alex.media.MUSIC_SERVICE");        intent.putExtra("list", 1);        startService(intent);                listview = new ListView(this);        Cursor c = this.getContentResolver()        		.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,        				new String[]{MediaStore.Audio.Media.TITLE,						MediaStore.Audio.Media.DURATION,						MediaStore.Audio.Media.ARTIST,						MediaStore.Audio.Media._ID,						MediaStore.Audio.Media.DISPLAY_NAME },        				null, null, null);        if (c==null || c.getCount()==0){        	builder = new AlertDialog.Builder(this);			builder.setMessage("存储列表为空...").setPositiveButton("确定", null);			ad = builder.create();			ad.show();        }        c.moveToFirst();        _ids = new int[c.getCount()];        _titles = new String[c.getCount()];        for(int i=0;i<c.getCount();i++){        	_ids[i] = c.getInt(3);        	_titles[i] = c.getString(0);        	c.moveToNext();        }        adapter = new MusicListAdapter(this, c);        listview.setAdapter(adapter);        listview.setOnItemClickListener(new ListItemClickListener());                mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); 		maxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);//获得最大音量  		currentVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);//获得当前音量 		LinearLayout list = new LinearLayout(this);		list.setBackgroundResource(R.drawable.listbg);        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(        LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);        list.addView(listview,params);        setContentView(list);    }       class ListItemClickListener implements OnItemClickListener{    	@Override    	public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {    		// TODO Auto-generated method stub    		Intent intent = new Intent(ListActivity.this,MusicActivity.class);    		intent.putExtra("_ids", _ids);    		intent.putExtra("_titles", _titles);    		intent.putExtra("position", position);    		startActivity(intent);    		finish();    	}    	    }               public boolean onKeyDown(int keyCode, KeyEvent event) {		if (keyCode == event.KEYCODE_BACK) {			if (scanSdReceiver!=null)      				unregisterReceiver(scanSdReceiver);			Intent intent = new Intent();			intent.setClass(this, MainActivity.class);			startActivity(intent);			finish();		}		return true;    }    public boolean dispatchKeyEvent(KeyEvent event) { 		int action = event.getAction(); 		int keyCode = event.getKeyCode(); 		switch (keyCode) { 			case KeyEvent.KEYCODE_VOLUME_UP: 			if (action == KeyEvent.ACTION_UP) {				if (currentVolume<maxVolume){					currentVolume = currentVolume + 1;					mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, currentVolume, 0);				} else {					mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, currentVolume, 0);				}			} 			return false; 			case KeyEvent.KEYCODE_VOLUME_DOWN: 			if (action == KeyEvent.ACTION_UP) { 				if (currentVolume>0){					currentVolume = currentVolume - 1;					mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, currentVolume , 0);				} else {					mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, currentVolume, 0);				}			} 			return false; 			default: 			return super.dispatchKeyEvent(event); 		} 	}    private BroadcastReceiver changeItem = new BroadcastReceiver() {				@Override		public void onReceive(Context context, Intent intent) {			if (intent.getAction().equals(MUSIC_LIST)){				pos = intent.getExtras().getInt("position");				adapter.setItemIcon(pos);//改变列表项图标				adapter.notifyDataSetChanged();//通知UI更新			}					}	};		@Override	protected void onStop() {		super.onStop();		unregisterReceiver(changeItem);	}	}


类八LRCbean

package com.alex.media;public class LRCbean {	private int beginTime=0;	public int getBeginTime() {		return beginTime;	}	public void setBeginTime(int beginTime) {		this.beginTime = beginTime;	}	public int getLineTime() {		return lineTime;	}	public void setLineTime(int lineTime) {		this.lineTime = lineTime;	}	public String getLrcBody() {		return lrcBody;	}	public void setLrcBody(String lrcBody) {		this.lrcBody = lrcBody;	}	private int  lineTime=0;	private String lrcBody = null;	}



类⑨LRCKlokView


package com.alex.media;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.util.AttributeSet;import android.view.View;public class LRCKlokView extends View{	private Paint p = null;	public LRCKlokView(Context context, AttributeSet attrs) {		super(context, attrs);		p = new Paint();	}	@Override	public void draw(Canvas canvas) {		// TODO Auto-generated method stub		super.draw(canvas);		p.setColor(Color.GREEN);	}		public void invalidate(String lrc){		Canvas canvas = new Canvas();		canvas.drawText(lrc, 0, 30, p);	}}


类⑩MainActivity


package com.alex.media;import android.app.Activity;import android.app.Application;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.database.Cursor;import android.media.AudioManager;import android.net.Uri;import android.os.Bundle;import android.os.Environment;import android.provider.MediaStore;import android.view.KeyEvent;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.AdapterView;import android.widget.ArrayAdapter;import android.widget.ListView;import android.widget.AdapterView.OnItemClickListener;public class MainActivity extends Activity{	private ListView listview=null;	private Intent intent = null;	private static final int ITEM1 = Menu.FIRST;	private static final int ITEM2 = Menu.FIRST+1;	private ScanSdReceiver scanSdReceiver = null;	private AudioManager mAudioManager = null;	private int maxVolume;//最大音量	private int currentVolume;//当前音量	@Override	protected void onCreate(Bundle savedInstanceState) {		// TODO Auto-generated method stub		super.onCreate(savedInstanceState);		String[] strs = {"全部音乐","最近播放列表","最经常播放列表"};		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,				android.R.layout.simple_list_item_1,strs);		listview = new ListView(this);		listview.setAdapter(adapter);		listview.setOnItemClickListener(new OnItemClickListener() {			@Override			public void onItemClick(AdapterView<?> arg0, View arg1, int position,					long id) {				switch (position) {				case 0:			        intent = new Intent();					intent.setClass(MainActivity.this, ListActivity.class);					startActivity(intent);					finish();					break;				case 1:					intent = new Intent();					intent.setClass(MainActivity.this, RecentlyActivity.class);					startActivity(intent);					finish();					break;				case 2:					intent = new Intent();					intent.setClass(MainActivity.this, ClicksActivity.class);					startActivity(intent);					finish();					break;				}							}		});	    mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); 		maxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);//获得最大音量  		currentVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);//获得当前音量 		this.setContentView(listview);	}		@Override	public boolean onCreateOptionsMenu(Menu menu) {		super.onCreateOptionsMenu(menu);		menu.add(0, ITEM1, 0, "刷新库");		menu.add(0, ITEM2, 0, "退出");		return true;	}		@Override	public boolean onOptionsItemSelected(MenuItem item) {				switch (item.getItemId()){			case ITEM1:				scanSdCard();				break;			case ITEM2:				if (scanSdReceiver!=null)      				unregisterReceiver(scanSdReceiver);				this.finish();				Intent intent = new Intent();				intent.setAction("com.alex.media.MUSIC_SERVICE");				stopService(intent);				break;			default:				break;		}		return true;	}		 private void scanSdCard(){	    	IntentFilter intentfilter = new IntentFilter( Intent.ACTION_MEDIA_SCANNER_STARTED);	    	intentfilter.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED);	    	intentfilter.addDataScheme("file");	    	scanSdReceiver = new ScanSdReceiver();	    	registerReceiver(scanSdReceiver, intentfilter);	    	sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,	    			Uri.parse("file://" + Environment.getExternalStorageDirectory().getAbsolutePath())));	    }	 	 public boolean dispatchKeyEvent(KeyEvent event) { 			int action = event.getAction(); 			int keyCode = event.getKeyCode(); 			switch (keyCode) { 				case KeyEvent.KEYCODE_VOLUME_UP: 				if (action == KeyEvent.ACTION_UP) {					if (currentVolume<maxVolume){						currentVolume = currentVolume + 1;						mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, currentVolume, 0);					} else {						mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, currentVolume, 0);					}				} 				return false; 				case KeyEvent.KEYCODE_VOLUME_DOWN: 				if (action == KeyEvent.ACTION_UP) { 					if (currentVolume>0){						currentVolume = currentVolume - 1;						mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, currentVolume , 0);					} else {						mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, currentVolume, 0);					}				} 				return false; 				default: 				return super.dispatchKeyEvent(event); 			} 		}	 }


1 楼 wanghaisheng 2011-10-09  
楼主,你的进度条是如何实现的?
  相关解决方案