Android--音乐播放器Demo android音乐播放器ui

自己制作的一个Android音乐播放器,功能比较简单,问题还多多,在模拟器上运行更新seekBar的时候有点卡,希望大家多给意见.帮助我继续改进.

Android--音乐播放器Demo android音乐播放器ui
main.xml文件---------------------------------------------------------------------------------------------
<?xml version="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent" ><ListViewandroid:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_weight="1"android:id="@id/android:list"android:drawSelectorOnTop="false"android:background="@drawable/background"/><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:gravity="center_horizontal|center_vertical"android:background="@drawable/listviewbackground"><Buttonandroid:background="@drawable/forwardframe"android:layout_marginRight="10px"android:id="@+id/btnForwardFrame"/><SeekBarandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_weight="1"android:id="@+id/seekBar"/><Buttonandroid:background="@drawable/nextframe"android:layout_marginRight="10px"android:id="@+id/btnNextFrame"/></LinearLayout><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:gravity="center_horizontal|center_vertical"android:background="@drawable/listviewbackground"><Buttonandroid:background="@drawable/forward"android:layout_marginRight="10px"android:id="@+id/btnForward"/><Buttonandroid:background="@drawable/paused"android:layout_marginRight="10px"android:id="@+id/btnPlayed"/><Buttonandroid:background="@drawable/reset"android:layout_marginRight="10px"android:id="@+id/btnReseted"/><Buttonandroid:background="@drawable/nexted"android:layout_marginRight="10px"android:id="@+id/btnNexted"/></LinearLayout></LinearLayout>---------------------------------------------------------------------------------------------MainActivity文件
---------------------------------------------------------------------------------------------import java.io.File;import java.lang.reflect.Field;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;
import android.app.ListActivity;import android.content.Context;import android.media.MediaPlayer;import android.media.MediaPlayer.OnCompletionListener;import android.os.Bundle;import android.os.Environment;import android.os.Handler;import android.os.Message;import android.util.Log;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.widget.AdapterView;importandroid.widget.AdapterView.OnItemSelectedListener;import android.widget.ArrayAdapter;import android.widget.Button;import android.widget.ListAdapter;import android.widget.ListView;import android.widget.SeekBar;import android.widget.SeekBar.OnSeekBarChangeListener;import android.widget.Toast;import cn.muc.MusicPlayer.R.id;
public class MainActivity extends ListActivity implementsOnClickListener {private List<String> filePaths =null;private List<String> fileNames =null;private MediaPlayer mediaPlayer = null;private Button btnForwardFrame;private Button btnNextFrame;private SeekBar seekBar;private int currentIndex = 0;private boolean iscontinue ;private Handler handler = new Handler(){public void handleMessage(Message msg) {Bundle b = msg.getData();Log.i("other", "bbbbb");int currentPosition = b.getInt("currentPosition");seekBar.setProgress(currentPosition);}};public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);filePaths = newArrayList<String>();fileNames = newArrayList<String>();mediaPlayer = new MediaPlayer();btnForwardFrame = (Button)findViewById(R.id.btnForwardFrame);btnNextFrame = (Button) findViewById(R.id.btnNextFrame);seekBar = (SeekBar) findViewById(R.id.seekBar); //增加滑块变化监听seekBar.setOnSeekBarChangeListener(newOnSeekBarChangeListener() {public void onStopTrackingTouch(SeekBar seekBar) {}public void onStartTrackingTouch(SeekBar seekBar) {}public void onProgressChanged(SeekBar seekBar, intprogress,boolean fromUser) {mediaPlayer.seekTo(progress);}}); // 设置button监听setListener();// 获取SDCard目录File dir = Environment.getExternalStorageDirectory();File sddir = new File(dir.getPath());// 准备好列表文件prepareList(sddir);// 装载适配器ListAdapter adapter = newArrayAdapter<String>(this,android.R.layout.simple_list_item_1,fileNames);setListAdapter(adapter);}
public void onClick(View v) {int id = v.getId();switch (id) {//向前一首case R.id.btnForward:lastMusic();break;//播放case R.id.btnPlayed:if(!mediaPlayer.isPlaying()){Button play = (Button) findViewById(id);play.setBackgroundResource(R.drawable.palyed);if(iscontinue){mediaPlayer.start();}else{playMusic(currentIndex);}}else if(mediaPlayer.isPlaying()){Button play = (Button) findViewById(id);play.setBackgroundResource(R.drawable.paused);iscontinue = true;mediaPlayer.pause();}break;//重置case R.id.btnReseted:mediaPlayer.reset();Button play = (Button) findViewById(R.id.btnPlayed);play.setBackgroundResource(R.drawable.paused);Button reset = (Button) findViewById(id);iscontinue = false;break;//向后一首case R.id.btnNexted:nextMusic();break;//向后一帧case R.id.btnForwardFrame:if(mediaPlayer.isPlaying()){int currentPosition =mediaPlayer.getCurrentPosition()-1000;if(currentPosition<0){currentPosition = 0;}seekBar.setProgress(currentPosition);mediaPlayer.seekTo(currentPosition);}break;//向后一帧case R.id.btnNextFrame:if(mediaPlayer.isPlaying()){int currentPosition =mediaPlayer.getCurrentPosition()+1000;if(currentPosition>seekBar.getMax()){currentPosition = seekBar.getMax();}seekBar.setProgress(currentPosition);mediaPlayer.seekTo(currentPosition);}break;}}

//当listview被选择的时候出发这个事件protected void onListItemClick(ListView l, View v, intposition, long id) {if(!mediaPlayer.isPlaying()){Button play = (Button) findViewById(R.id.btnPlayed);play.setBackgroundResource(R.drawable.palyed);}currentIndex = position;playMusic(position);super.onListItemClick(l, v, position, id);}
// 准备好文件路径public void prepareList(File sddir) {File[] files = sddir.listFiles();//防止出现文件夹下面什么东西都没有的时候出现NullPointerExceptionif (files != null) {for (File file : files) {if (file.isFile()) {if (file.getName().endsWith(".mp3")||file.getName().endsWith(".wma")||file.getName().endsWith(".wav")) {filePaths.add(file.getPath());fileNames.add(file.getName());}} else if (file.isDirectory()) {prepareList(file);}}}}//播放public void playMusic(int index) {try {mediaPlayer.reset();mediaPlayer.setDataSource(filePaths.get(index));mediaPlayer.prepare();mediaPlayer.start();//设置滑块最大值是文件播放时长seekBar.setMax(mediaPlayer.getDuration());seekBar.setProgress(0);new MyThread(handler,mediaPlayer).start(); //播放完毕处理事件mediaPlayer.setOnCompletionListener(new OnCompletionListener(){public void onCompletion(MediaPlayer mp) {nextMusic();}});// 错误处理事件mediaPlayer.setOnErrorListener(newMediaPlayer.OnErrorListener() {public boolean onError(MediaPlayer mp, int what, int extra){mediaPlayer.release();return false;}});} catch (Exception e) {e.printStackTrace();}}//下一首public void nextMusic(){if(++currentIndex>=filePaths.size()){currentIndex=0;playMusic(currentIndex);}else{playMusic(currentIndex);}}//下一首public void lastMusic(){if(--currentIndex<=-1){currentIndex=filePaths.size()-1;playMusic(currentIndex);}else{playMusic(currentIndex);}}
// 通过反射给Button添加监听事件public void setListener() {Class r = id.class;Field[] fields = r.getFields();for (Field field : fields) {// 判断是否是我们定义的Buttonif (field.getName().startsWith("btn")) {try {int id = field.getInt(r);findViewById(id).setOnClickListener(this);} catch (Exception e) {e.printStackTrace();}}}}}
---------------------------------------------------------------------------------------------MyThread文件---------------------------------------------------------------------------------------------import android.media.MediaPlayer;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.util.Log;import android.widget.Toast;
public class MyThread extends Thread{private Handler handler ;private MediaPlayer mediaPlayer ;public MyThread(Handler handler, MediaPlayer mediaPlayer){this.handler = handler;this.mediaPlayer = mediaPlayer;}
public void run() {try {while(true &&mediaPlayer.isPlaying()){Thread.sleep(1200);int currentPosition = mediaPlayer.getCurrentPosition();Log.i("other", currentPosition+"");Log.i("other", "aaaaa");Message msg = new Message();Bundle bundle = new Bundle();bundle.putInt("currentPosition", currentPosition);msg.setData(bundle);handler.sendMessage(msg);}} catch (Exception e) {e.printStackTrace();}}}

  

爱华网本文地址 » http://www.413yy.cn/a/25101011/66249.html

更多阅读

怎样免费给QQ空间音乐播放器里添加音乐 qq空间音乐免费添加

QQ空间可以免费添加音乐,你信吗?在很多朋友看来这是不可能的。其实是可以的,按照以下方法操作就可以了。怎样免费给QQ空间音乐播放器里添加音乐——工具/原料无怎样免费给QQ空间音乐播放器里添加音乐——步骤/方法怎样免费给QQ空间音

ui的设计教程 qt ui界面设计教程

ui的设计教程——简介 其实,我也是个初学者,这里分享下我学习的经验。请不要把这当做能够使你直接成为大师的作品。目前我学习的是VS和Android进行设计。ui的设计教程——方法——androidui的设计教程 1、 下图是我刚刚学的时候所使

电脑上哪个音乐播放器最好用 2016音乐播放器的排名

????????用一款高质量的音乐播放器欣赏喜欢的音乐是多么惬意的一件事情,当然前提是你的耳机也不能太差。本人作为一个音乐爱好者一直在寻找高质量的播放器,无奈现在音乐播放器数不胜数,质量也参差不齐,而且现在技术都有了很大的提升,各个

MUSIC(202030)在线音乐播放器制作方法 在线音乐播放器制作

如何制作202030音乐播放器(太阳嘟嘟书馆编辑)给书友们介绍一款202030音乐播放器,希望大家都能学会,设计自己的音乐播放器,自己的音乐自己做主!(一)、进入这个网站http://www.202030.com/先注册(二)、注册成功后,登入后就是这个页面,分2步。1、先

android全格式多媒体播放器一:ffmpeg移植

为了能在android平台上播放全格式的多媒体文件,我们需要自己做一个多媒体播放器。android自带的opencore系统解码格式较少,只支持mp4和ogg,并且结构不是很好理解。如果要加其他的解码方式实在太费劲。经过考虑,基于ffmpeg实现全功能的播

声明:《Android--音乐播放器Demo android音乐播放器ui》为网友沧海桑田分享!如侵犯到您的合法权益请联系我们删除