播放器

package com.example.day11_yinpin2;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

	private TextView tv;
	private Button bf;
	private Button zt;
	private Button tz;
	private Button sy;
	private Button xy;
	private SeekBar see;

	private int currentindex = 0;
	private MediaPlayer mediaPlayer;
	private int sumtime;

	List<String> listsrc = new ArrayList<String>();
	List<String> listname = new ArrayList<String>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Init();
		// 将数据添加到集合中
		listsrc.add("mnt/sdcard/music1.mp3");
		listsrc.add("mnt/sdcard/music2.mp3");
		listname.add("歌曲一");
		listname.add("歌曲二");
	}

	// seekbar设监听
	OnSeekBarChangeListener listener = new OnSeekBarChangeListener() {

		@Override
		public void onStopTrackingTouch(SeekBar seekBar) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onStartTrackingTouch(SeekBar seekBar) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onProgressChanged(SeekBar seekBar, int progress,
				boolean fromUser) {
			mediaPlayer.seekTo(progress);
		}
	};
	public void Init() {
		tv = (TextView) findViewById(R.id.tv);
		bf = (Button) findViewById(R.id.button1);
		zt = (Button) findViewById(R.id.button2);
		tz = (Button) findViewById(R.id.button3);
		sy = (Button) findViewById(R.id.button4);
		xy = (Button) findViewById(R.id.button5);
		see = (SeekBar) findViewById(R.id.seekBar1);
		mediaPlayer = new MediaPlayer();
		bf.setOnClickListener(this);
		zt.setOnClickListener(this);
		tz.setOnClickListener(this);
		sy.setOnClickListener(this);
		xy.setOnClickListener(this);
		see.setOnSeekBarChangeListener(listener);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.button1:
			play();
			break;
		case R.id.button2:
			if (mediaPlayer != null && mediaPlayer.isPlaying()) {
				mediaPlayer.pause();
			} else if (mediaPlayer != null) {
				mediaPlayer.start();
			}
			break;
		case R.id.button3:
			if (mediaPlayer != null) {
				mediaPlayer.stop();
				// 释放
				mediaPlayer.release();
			}
			break;
		case R.id.button4:
			if (currentindex <= 0) {
				Toast.makeText(this, "已经是第一首歌曲", 0);
			return;
			}
			currentindex--;
			play();
			break;
		case R.id.button5:
			if (currentindex >= listsrc.size()-1) {
				Toast.makeText(this, "已经是最后一首歌曲", 0);
			return;
			}
			currentindex++;
			play();
		default:

			break;
		}
	}
	// 播放的方法
	public void play() {		
		try {
			mediaPlayer.setDataSource(listsrc.get(currentindex));
			mediaPlayer.prepare();
			mediaPlayer.start();
			sumtime = mediaPlayer.getDuration();
			// 设置进度条最大进度
			see.setMax(sumtime);
		} catch (Exception e) {
			e.printStackTrace();
		}
		tv.setText(listname.get(currentindex));
	}
}

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="播放" />
    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="暂停/播放" />
    <Button
        android:id="@+id/button3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="停止" />
    <Button
        android:id="@+id/button4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="上一首" />
    <Button
        android:id="@+id/button5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="下一首" />

    <SeekBar
        android:id="@+id/seekBar1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

http://www.cnblogs.com/snake-hand/archive/2013/02/22/2922923.html

猜你喜欢

转载自zhengaihua.iteye.com/blog/2337762