Android音乐播放器开发
16lz
2021-01-23
今日看书,看到这个播放器,我就写了个例子,感觉还行,这个播放器能播放后缀是。MP3的音乐,这个例子在main.xml设置listView的时候,注意:android:id="@+id/android:list"的设置,否则程序会报错,说找不到listview。这个效果还是不错的。可以当做是简单的音乐播放器,可以读取sdcard里面后缀是。MP3的歌曲。有问题可以留言,想要源码可以留言,这个代码比较简单。转载请标明出处:
http://blog.csdn.net/wdaming1986/article/details/6768884
csdn资源下载链接地址:http://download.csdn.net/detail/wdaming1986/3611735
看程序效果图:可以点击每首歌播放,
也可以用下面的按钮:修改后的程序加了滚动条了
代码说明一切:
一、MainActivity。java类中的代码:
Java代码- packagecom.cn.daming;
- importjava.io.File;
- importjava.io.FilenameFilter;
- importjava.io.IOException;
- importjava.util.ArrayList;
- importjava.util.List;
- importandroid.app.ListActivity;
- importandroid.graphics.Color;
- importandroid.graphics.drawable.GradientDrawable;
- importandroid.graphics.drawable.GradientDrawable.Orientation;
- importandroid.media.MediaPlayer;
- importandroid.media.MediaPlayer.OnCompletionListener;
- importandroid.os.Bundle;
- importandroid.os.Handler;
- importandroid.view.KeyEvent;
- importandroid.view.View;
- importandroid.view.View.OnClickListener;
- importandroid.widget.ArrayAdapter;
- importandroid.widget.ImageButton;
- importandroid.widget.ListView;
- importandroid.widget.SeekBar;
- importandroid.widget.SeekBar.OnSeekBarChangeListener;
- importandroid.widget.TextView;
- publicclassMainActivityextendsListActivity{
- privateImageButtonmFrontImageButton=null;
- privateImageButtonmStopImageButton=null;
- privateImageButtonmStartImageButton=null;
- privateImageButtonmPauseImageButton=null;
- privateImageButtonmNextImageButton=null;
- /*定义进度handler,显示百分比进度*/
- HandlermPercentHandler=newHandler();
- privateSeekBarmSeekBar=null;
- privateTextViewcurProgressText=null;
- privateTextViewcurtimeAndTotaltime=null;
- publicMediaPlayermMediaPlayer;
- privateList<String>mMusicList=newArrayList<String>();
- privateintcurrentListItem=0;
- privatestaticfinalStringMUSIC_PATH=newString("/sdcard/");
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- drawBackground();
- setContentView(R.layout.main);
- musicList();
- mMediaPlayer=newMediaPlayer();
- initmFrontMusic();
- initStopMusic();
- initStartMusic();
- initPauseMusic();
- initNextMusic();
- initSeekBar();
- }
- publicvoiddrawBackground()
- {
- GradientDrawablegrad=newGradientDrawable(
- Orientation.TL_BR,
- newint[]{
- Color.rgb(0,0,127),
- Color.rgb(0,0,255),
- Color.rgb(127,0,255),
- Color.rgb(127,127,255),
- Color.rgb(127,255,255),
- Color.rgb(255,255,255)
- }
- );
- this.getWindow().setBackgroundDrawable(grad);
- }
- publicvoidinitmFrontMusic()
- {
- mFrontImageButton=(ImageButton)findViewById(R.id.front_button);
- mFrontImageButton.setOnClickListener(newOnClickListener(){
- publicvoidonClick(Viewarg0){
- if(--currentListItem>=0){
- currentListItem=mMusicList.size();
- }else{
- playMusic(MUSIC_PATH+mMusicList.get(currentListItem));
- }
- }
- });
- }
- publicvoidinitStopMusic()
- {
- mStopImageButton=(ImageButton)findViewById(R.id.stop_button);
- mStopImageButton.setOnClickListener(newOnClickListener(){
- publicvoidonClick(Viewarg0){
- if(mMediaPlayer.isPlaying())
- {
- mMediaPlayer.reset();
- }
- }
- });
- }
- publicvoidinitStartMusic()
- {
- mStartImageButton=(ImageButton)findViewById(R.id.start_button);
- mStartImageButton.setOnClickListener(newOnClickListener(){
- publicvoidonClick(Viewarg0){
- playMusic(MUSIC_PATH+mMusicList.get(currentListItem));
- startSeekBarUpdate();
- }
- });
- }
- publicvoidinitPauseMusic()
- {
- mPauseImageButton=(ImageButton)findViewById(R.id.pause_button);
- mPauseImageButton.setOnClickListener(newOnClickListener(){
- publicvoidonClick(Viewarg0){
- if(mMediaPlayer.isPlaying()){
- mMediaPlayer.pause();
- }
- else{
- mMediaPlayer.start();
- }
- }
- });
- }
- publicvoidinitNextMusic()
- {
- mNextImageButton=(ImageButton)findViewById(R.id.next_button);
- mNextImageButton.setOnClickListener(newOnClickListener(){
- publicvoidonClick(Viewarg0){
- nextMusic();
- }
- });
- }
- publicvoidinitSeekBar()
- {
- /*初始化拖动条和当前进度显示值*/
- mSeekBar=(SeekBar)findViewById(R.id.SeekBar01);
- curProgressText=(TextView)findViewById(R.id.currentProgress);
- curtimeAndTotaltime=(TextView)findViewById(R.id.curtimeandtotaltime);
- mSeekBar.setOnSeekBarChangeListener(newOnSeekBarChangeListener(){
- publicvoidonProgressChanged(SeekBarseekBar,intprogress,
- booleanfromUser){
- /*如果拖动进度发生改变,则显示当前进度值*/
- curProgressText.setText("当前进度:"+progress);
- }
- publicvoidonStartTrackingTouch(SeekBararg0){
- curProgressText.setText("拖动中...");
- }
- publicvoidonStopTrackingTouch(SeekBararg0){
- intdest=mSeekBar.getProgress();
- intmMax=mMediaPlayer.getDuration();
- intsMax=mSeekBar.getMax();
- mMediaPlayer.seekTo(mMax*dest/sMax);
- }
- });
- }
- privatevoidplayMusic(Stringpath)
- {
- try{
- mMediaPlayer.reset();
- mMediaPlayer.setDataSource(path);
- mMediaPlayer.prepare();
- mMediaPlayer.start();
- mMediaPlayer.setOnCompletionListener(newOnCompletionListener(){
- publicvoidonCompletion(MediaPlayerarg0){
- nextMusic();
- }
- });
- }catch(IOExceptione){
- e.printStackTrace();
- }
- }
- privatevoidnextMusic()
- {
- if(++currentListItem>=mMusicList.size())
- {
- currentListItem=0;
- }
- else
- {
- playMusic(MUSIC_PATH+mMusicList.get(currentListItem));
- }
- }
- @Override
- publicbooleanonKeyDown(intkeyCode,KeyEventevent){
- if(keyCode==KeyEvent.KEYCODE_BACK){
- mMediaPlayer.stop();
- mMediaPlayer.release();
- }
- returnsuper.onKeyDown(keyCode,event);
- }
- @Override
- protectedvoidonListItemClick(ListViewl,Viewv,intposition,longid){
- currentListItem=position;
- playMusic(MUSIC_PATH+mMusicList.get(position));
- super.onListItemClick(l,v,position,id);
- }
- //播放列表
- publicvoidmusicList()
- {
- Filehome=newFile(MUSIC_PATH);
- if(home.listFiles(newMusicFilter()).length>0)
- {
- for(Filefile:home.listFiles(newMusicFilter()))
- {
- mMusicList.add(file.getName());
- }
- ArrayAdapter<String>musicList=newArrayAdapter<String>(MainActivity.this,R.layout.musicitem,mMusicList);
- setListAdapter(musicList);
- }
- }
- /*更新拖动条进度*/
- publicvoidstartSeekBarUpdate(){
- mPercentHandler.post(start);
- }
- Runnablestart=newRunnable(){
- publicvoidrun(){
- //用一个handler更新SeekBar
- mPercentHandler.post(updatesb);
- }
- };
- Runnableupdatesb=newRunnable(){
- publicvoidrun(){
- intposition=mMediaPlayer.getCurrentPosition();
- intmMax=mMediaPlayer.getDuration();
- intsMax=mSeekBar.getMax();
- mSeekBar.setProgress(position*sMax/mMax);
- curtimeAndTotaltime.setText("当前播放时间:"+position/1000+"秒"
- +"\n歌曲总时间:"+mMax/1000+"秒");
- //每秒钟更新一次
- mPercentHandler.postDelayed(updatesb,1000);
- }
- };
- //过滤文件类型
- classMusicFilterimplementsFilenameFilter
- {
- publicbooleanaccept(Filedir,Stringname){
- //这里还可以设置其他格式的音乐文件
- return(name.endsWith(".mp3"));
- }
- }
- }
二、main。xml布局文件的代码:
Html代码- <spanstyle="font-size:13px;color:#000000;"><?xmlversion="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"
- >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="25dip"
- android:paddingTop="5dip"
- android:layout_gravity="center_horizontal"
- android:gravity="center_horizontal"
- android:textColor="#FF000000"
- android:text="大明制作Mp3播放器"
- />
- <ListView
- android:id="@+id/android:list"
- android:layout_width="fill_parent"
- android:layout_height="200dip"
- android:layout_weight="1"
- android:drawSelectorOnTop="false"
- />
- <SeekBar
- android:id="@+id/SeekBar01"
- android:layout_height="wrap_content"
- android:layout_width="fill_parent"
- android:max="100"
- android:progress="0"
- android:secondaryProgress="0"
- android:visibility="visible"
- />
- <TextView
- android:layout_height="wrap_content"
- android:layout_width="fill_parent"
- android:id="@+id/currentProgress"
- />
- <TextView
- android:layout_height="wrap_content"
- android:layout_width="fill_parent"
- android:layout_y="300dp"
- android:id="@+id/curtimeandtotaltime"
- />
- <LinearLayout
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- >
- <ImageButton
- android:id="@+id/front_button"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="@drawable/first1"
- android:layout_marginLeft="10dip"
- />
- <ImageButton
- android:id="@+id/stop_button"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="@drawable/stop1"
- android:layout_marginLeft="10dip"
- />
- <ImageButton
- android:id="@+id/start_button"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="@drawable/start1"
- android:layout_marginLeft="10dip"
- />
- <ImageButton
- android:id="@+id/pause_button"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="@drawable/pose1"
- android:layout_marginLeft="10dip"
- />
- <ImageButton
- android:id="@+id/next_button"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="@drawable/next1"
- android:layout_marginLeft="10dip"
- />
- </LinearLayout>
- </LinearLayout>
- </span>
三、musicitem.xml布局文件的代码:
Html代码- <?xmlversion="1.0"encoding="utf-8"?>
- <TextViewxmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/TextView01"
- android:layout_width="fill_parent"
- android:layout_height="26dip"
- android:layout_gravity="center_vertical"
- android:paddingTop="5dip"
- android:paddingLeft="20dip"
- android:textColor="#FF000000"
- android:text="@string/hello1"/>
四、Manifest。xml文件
- <spanstyle="font-size:13px;color:#000000;"><?xmlversion="1.0"encoding="utf-8"?>
- <manifestxmlns:android="http://schemas.android.com/apk/res/android"
- package="com.cn.daming"
- android:versionCode="1"
- android:versionName="1.0">
- <uses-sdkandroid:minSdkVersion="8"/>
- <applicationandroid:icon="@drawable/icon"android:label="@string/app_name">
- <activityandroid:name=".MainActivity"
- android:label="@string/app_name">
- <intent-filter>
- <actionandroid:name="android.intent.action.MAIN"/>
- <categoryandroid:name="android.intent.category.LAUNCHER"/>
- </intent-filter>
- </activity>
- </application>
- </manifest></span>
更多相关文章
- Android软件广告屏蔽方法及代码
- Android studio使用技巧(二:国际化以及代码重构)
- Android的反编译和代码混淆
- Android SeekBar的使用,进度条的另一种实现方式
- 在Android上实现HttpServer的示例代码
- Android WebView 调用React Js 代码
- android recover 系统代码分析 -- 选择进入