php几个不起眼儿的小技巧

http://curran.blog.51cto.com/2788306/523085

今天我们要讲述一个具有控制界面的音乐播放器
1:UI布局layout/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"
        >
  <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="迷你音乐播放器"
  />
  
  <LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
  >
    <ImageButton
      android:id="@+id/play"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:src="@drawable/play"
    />
    <ImageButton
      android:id="@+id/pause"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:src="@drawable/pause"
    />  
    <ImageButton
      android:id="@+id/stop"
      android:layout_height="wrap_content"
      android:layout_width="wrap_content"
      android:src="@drawable/stop"
    />
  </LinearLayout>
</LinearLayout>
2:实现功能核心代码MediaActivity.java
package com.android.test;

import android.app.Activity;
import android.app.AlertDialog;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ProgressBar;

public class MediaActivity extends Activity implements OnCompletionListener {
    
   private ImageButton play, pause, stop;
   //声明MediaPlayer实例
   private MediaPlayer mp;  
         public void onCreate(Bundle savedInstanceState) {
                 super.onCreate(savedInstanceState);
                 //设置当前页面布局
                setContentView(R.layout.main);        
                 //通过findViewById方法获得按钮实例
                play = (ImageButton)findViewById(R.id.play);
                pause = (ImageButton)findViewById(R.id.pause);
                stop = (ImageButton)findViewById(R.id.stop);
        
                 //为按钮添加单击时间监听器
                play.setOnClickListener( new View.OnClickListener() {

       public void onClick(View v) {
         //开始
        play();
      }                
                });
                
                pause.setOnClickListener( new View.OnClickListener() {

       public void onClick(View v) {
         //暂停
        pause();
      }
                });
                
                stop.setOnClickListener( new View.OnClickListener() {

       public void onClick(View v) {
         //停止
        stop();
      }              
                });
                 //准备
                setup();
        }
        
         public void onDestory() {
           super.onDestroy();
           if(stop.isEnabled()) {
            stop();
          }
        }

         //准备
   private void setup() {
     //加载播放文件
    loadClip();
     //使播放按钮生效
    play.setEnabled( true);
     //使暂停按钮失效
    pause.setEnabled( false);
     //使停止按钮失效
    stop.setEnabled( false);
  }

   private void loadClip() {
     try{
       //实例化MediaPlayer
      mp = MediaPlayer.create( this, R.raw.test);
       //设置监听器
      mp.setOnCompletionListener( this);
    } catch(Throwable t) {
      error(t);
    }
  }
  
   private void error(Throwable t) {
    AlertDialog.Builder builder = new AlertDialog.Builder( this);
    builder.setTitle( "报错啦!").setMessage(t.toString()).setPositiveButton( "确定", null).show();
  }

   public void stop() {
     //停止
    mp.stop();
     //使暂停按钮失效
    pause.setEnabled( false);
     //使停止按钮失效
    stop.setEnabled( false);
    
     try {
       //准备
      mp.prepare();
       //定位到开始
      mp.seekTo(0);
       //使播放按钮生效
      play.setEnabled( true);
    } catch (Throwable t) {
      error(t);
    }
  }

   public void pause() {
     //暂停
    mp.pause();
     //使播放按钮生效
    play.setEnabled( true);
     //使暂停按钮失效
    pause.setEnabled( false);
     //使停止按钮生效
    stop.setEnabled( true);
  }

   public void play() {
     //播放
    mp.start();
     //使播放按钮失效
    play.setEnabled( false);
     //使暂停按钮生效
    pause.setEnabled( true);
     //使停止按钮生效
    stop.setEnabled( true);
  }

   public void onCompletion(MediaPlayer mp) {
     //停止
    stop();
  }
}
3:运行程序如图

本文出自 “Art_Hero” 博客,请务必保留此出处http://curran.blog.51cto.com/2788306/523085

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值