今日看书,看到这个播放器,我就写了个例子,感觉还行,这个播放器能播放后缀是。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代码
- package com.cn.daming;
- import java.io.File;
- import java.io.FilenameFilter;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.List;
- import android.app.ListActivity;
- import android.graphics.Color;
- import android.graphics.drawable.GradientDrawable;
- import android.graphics.drawable.GradientDrawable.Orientation;
- import android.media.MediaPlayer;
- import android.media.MediaPlayer.OnCompletionListener;
- import android.os.Bundle;
- import android.os.Handler;
- import android.view.KeyEvent;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.ArrayAdapter;
- import android.widget.ImageButton;
- import android.widget.ListView;
- import android.widget.SeekBar;
- import android.widget.SeekBar.OnSeekBarChangeListener;
- import android.widget.TextView;
- public class MainActivity extends ListActivity {
- private ImageButton mFrontImageButton = null;
- private ImageButton mStopImageButton = null;
- private ImageButton mStartImageButton = null;
- private ImageButton mPauseImageButton = null;
- private ImageButton mNextImageButton = null;
- /*定义进度handler,显示百分比进度*/
- Handler mPercentHandler = new Handler();
- private SeekBar mSeekBar=null;
- private TextView curProgressText=null;
- private TextView curtimeAndTotaltime=null;
- public MediaPlayer mMediaPlayer;
- private List<String> mMusicList = new ArrayList<String>();
- private int currentListItem = 0;
- private static final String MUSIC_PATH = new String("/sdcard/");
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- drawBackground();
- setContentView(R.layout.main);
- musicList();
- mMediaPlayer = new MediaPlayer();
- initmFrontMusic();
- initStopMusic();
- initStartMusic();
- initPauseMusic();
- initNextMusic();
- initSeekBar();
- }
- public void drawBackground()
- {
- GradientDrawable grad = new GradientDrawable(
- Orientation.TL_BR,
- new int[] {
- 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);
- }
- public void initmFrontMusic()
- {
- mFrontImageButton = (ImageButton)findViewById(R.id.front_button);
- mFrontImageButton.setOnClickListener(new OnClickListener(){
- public void onClick(View arg0) {
- if(--currentListItem >= 0){
- currentListItem = mMusicList.size();
- }else{
- playMusic(MUSIC_PATH + mMusicList.get(currentListItem));
- }
- }
- });
- }
- public void initStopMusic()
- {
- mStopImageButton = (ImageButton)findViewById(R.id.stop_button);
- mStopImageButton.setOnClickListener(new OnClickListener(){
- public void onClick(View arg0) {
- if(mMediaPlayer.isPlaying())
- {
- mMediaPlayer.reset();
- }
- }
- });
- }
- public void initStartMusic()
- {
- mStartImageButton = (ImageButton)findViewById(R.id.start_button);
- mStartImageButton.setOnClickListener(new OnClickListener(){
- public void onClick(View arg0) {
- playMusic(MUSIC_PATH + mMusicList.get(currentListItem));
- startSeekBarUpdate();
- }
- });
- }
- public void initPauseMusic()
- {
- mPauseImageButton = (ImageButton)findViewById(R.id.pause_button);
- mPauseImageButton.setOnClickListener(new OnClickListener(){
- public void onClick(View arg0) {
- if(mMediaPlayer.isPlaying()){
- mMediaPlayer.pause();
- }
- else{
- mMediaPlayer.start();
- }
- }
- });
- }
- public void initNextMusic()
- {
- mNextImageButton = (ImageButton)findViewById(R.id.next_button);
- mNextImageButton.setOnClickListener(new OnClickListener(){
- public void onClick(View arg0) {
- nextMusic();
- }
- });
- }
- public void initSeekBar()
- {
- /*初始化拖动条和当前进度显示值*/
- mSeekBar=(SeekBar)findViewById(R.id.SeekBar01);
- curProgressText=(TextView)findViewById(R.id.currentProgress);
- curtimeAndTotaltime=(TextView)findViewById(R.id.curtimeandtotaltime);
- mSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
- public void onProgressChanged(SeekBar seekBar, int progress,
- boolean fromUser) {
- /* 如果拖动进度发生改变,则显示当前进度值 */
- curProgressText.setText("当前进度: " + progress);
- }
- public void onStartTrackingTouch(SeekBar arg0) {
- curProgressText.setText("拖动中...");
- }
- public void onStopTrackingTouch(SeekBar arg0) {
- int dest = mSeekBar.getProgress();
- int mMax = mMediaPlayer.getDuration();
- int sMax = mSeekBar.getMax();
- mMediaPlayer.seekTo(mMax*dest/sMax);
- }
- });
- }
- private void playMusic(String path)
- {
- try {
- mMediaPlayer.reset();
- mMediaPlayer.setDataSource(path);
- mMediaPlayer.prepare();
- mMediaPlayer.start();
- mMediaPlayer.setOnCompletionListener(new OnCompletionListener(){
- public void onCompletion(MediaPlayer arg0) {
- nextMusic();
- }
- });
- }catch (IOException e) {
- e.printStackTrace();
- }
- }
- private void nextMusic()
- {
- if(++currentListItem >= mMusicList.size())
- {
- currentListItem = 0;
- }
- else
- {
- playMusic(MUSIC_PATH + mMusicList.get(currentListItem));
- }
- }
- @Override
- public boolean onKeyDown(int keyCode, KeyEvent event) {
- if(keyCode == KeyEvent.KEYCODE_BACK){
- mMediaPlayer.stop();
- mMediaPlayer.release();
- }
- return super.onKeyDown(keyCode, event);
- }
- @Override
- protected void onListItemClick(ListView l, View v, int position, long id) {
- currentListItem = position;
- playMusic(MUSIC_PATH + mMusicList.get(position));
- super.onListItemClick(l, v, position, id);
- }
- //播放列表
- public void musicList()
- {
- File home = new File(MUSIC_PATH);
- if(home.listFiles(new MusicFilter()).length > 0)
- {
- for(File file : home.listFiles(new MusicFilter()))
- {
- mMusicList.add(file.getName());
- }
- ArrayAdapter<String> musicList = new ArrayAdapter<String>(MainActivity.this,R.layout.musicitem,mMusicList);
- setListAdapter(musicList);
- }
- }
- /*更新拖动条进度*/
- public void startSeekBarUpdate() {
- mPercentHandler.post(start);
- }
- Runnable start = new Runnable() {
- public void run() {
- // 用一个handler更新SeekBar
- mPercentHandler.post(updatesb);
- }
- };
- Runnable updatesb =new Runnable(){
- public void run() {
- int position = mMediaPlayer.getCurrentPosition();
- int mMax = mMediaPlayer.getDuration();
- int sMax = mSeekBar.getMax();
- mSeekBar.setProgress(position * sMax / mMax);
- curtimeAndTotaltime.setText("当前播放时间: " + position / 1000 + "秒"
- + "\n歌曲总时间: " + mMax / 1000 + "秒");
- // 每秒钟更新一次
- mPercentHandler.postDelayed(updatesb, 1000);
- }
- };
- //过滤文件类型
- class MusicFilter implements FilenameFilter
- {
- public boolean accept(File dir, String name) {
- //这里还可以设置其他格式的音乐文件
- return (name.endsWith(".mp3"));
- }
- }
- }
二、main。xml布局文件的代码:
Html代码
- <span style="font-size: 13px; color: #000000;"><?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns: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代码
- <?xml version="1.0" encoding="utf-8"?>
- <TextView xmlns: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文件
Html代码
- <span style="font-size: 13px; color: #000000;"><?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.cn.daming"
- android:versionCode="1"
- android:versionName="1.0">
- <uses-sdk android:minSdkVersion="8" />
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:name=".MainActivity"
- android:label="@string/app_name">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- </manifest></span>
时间: 2024-10-06 00:51:06