Onelong

分享知识,与你一起进步......
RSS icon Home icon
  • 简单的android音乐播放器

    post by onelong / 2010-9-3 3:24 Friday [android]

    废话不说了!!最近做了个播放器,感觉有点问题,就是stop()和pause()函数的问题,stop()调用后音乐应该重头开始的!但是感觉他和pause()没有多大的分别!!是不是我那个地方写错了呢?

    看看源码吧!!

    //main.xml

    <?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"
         android:padding="10px"
        >
    <TextView
     android:id="@+id/alert"
     android:layout_width="fill_parent"
        android:layout_height="wrap_content"
    />
    <LinearLayout
     android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="right"
        android:padding="5px"
     >
     <Button
     android:id="@+id/play"
     android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/play"
     />
     <Button
     android:id="@+id/pause"
     android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/pause"
     />
     <Button
     android:id="@+id/stop"
     android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/stop"
     />
    </LinearLayout>
    </LinearLayout>

    界面的样子基本就是这个样子了!!

    点击查看原图

    //Player.java

    package com.hl.player;

    import android.app.Activity;
    import android.media.MediaPlayer;
    import android.media.MediaPlayer.OnCompletionListener;
    import android.media.MediaPlayer.OnErrorListener;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.TextView;

    public class Player extends Activity {
     
        private Button playBtn,pauseBtn,stopBtn;
        private TextView alert;
        private MediaPlayer mp;
        private boolean isPlayed = false;
       
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
           
            alert = (TextView)findViewById(R.id.alert);
            playBtn = (Button)findViewById(R.id.play);
            pauseBtn = (Button)findViewById(R.id.pause);
            stopBtn = (Button)findViewById(R.id.stop);
            mp = MediaPlayer.create(this, R.raw.dangerous);
           
            mp.setOnErrorListener(new OnErrorListener() {
       public boolean onError(MediaPlayer mp, int what, int extra) {
        mp.release();
        alert.setText("音乐播放错误");
        return false;
       }
      });
            mp.setOnCompletionListener(new OnCompletionListener() {
       
       public void onCompletion(MediaPlayer mp) {
        isPlayed = false;
        alert.setText("音乐播放结束");
       }
      });
           
            playBtn.setOnClickListener(new OnClickListener() {
       
       public void onClick(View v) {
        if(!isPlayed){
         try {
          if(mp != null){
           mp.stop();
          }
          mp.reset();
          mp.prepare();
          isPlayed = true;
         } catch (Exception e) {
          alert.setText(e.toString());
         } 
         mp.start();
         alert.setText("音乐播放中...");
        }
       }
      });
            pauseBtn.setOnClickListener(new OnClickListener() {
       
       public void onClick(View v) {
        if(isPlayed  && mp != null){
         mp.pause();
         isPlayed = false;
         alert.setText("音乐暂停中...");
        }
        
       }
      });
            stopBtn.setOnClickListener(new OnClickListener() {
     
             public void onClick(View v) {
              if(mp != null){
         mp.seekTo(0);
         mp.pause();
         //mp.stop();
         isPlayed = false;
         alert.setText("音乐停止");
        }
             }
            });
        }
       
    }

    附上源文件!!!

    附件下载:
    MyPlayer.rar 12.52KB

    标签: 播放器 android
    引用地址:
     

    评论:

    1. Gaurav
      All of these aritlces have saved me a lot of headaches.

    我要评论