写一个小程序,在App里面实现mp3的播发,暂停,停止


步骤:

1. 新建项目,添加mp3



2. UI 制作  activity_main.xml添加3个按钮,实现播放,暂停,停止 

    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
            android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/app_name"
        android:layout_gravity="center"
        
        android:src="@drawable/ic_launcher" />
    
                android:layout_width="match_parent"
            android:layout_height="wrap_content" >
       
                    android:id="@+id/start"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/play" />
                    android:id="@+id/pause"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/pause"
             android:layout_weight="1"
            />
                    android:id="@+id/stop"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:background="@drawable/stop" 
             android:layout_weight="1"/>


         

          
   

3  MainActivity.class 添加3个按钮的监控器

package com.example.hello;


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;




public class MainActivity extends Activity {
   private Button start;
   private Button pause;
   private Button stop;
   private MediaPlayer player;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        start = (Button)findViewById(R.id.start);
        pause = (Button)findViewById(R.id.pause);
        stop = (Button)findViewById(R.id.stop);
        NewListener newListener = new NewListener();
        start.setOnClickListener(newListener);
        pause.setOnClickListener(newListener);
        stop.setOnClickListener(newListener);
        
        //MediaPlayer初始化
       player = MediaPlayer.create(this, R.raw.test);
    }
    
    class NewListener implements OnClickListener{
    @Override
    public void onClick(View v){
     switch (v.getId()) {
        case R.id.start:{
   
          //不播发就播发
          if(!player.isPlaying()){
               player.start(); 
           }
          break;
            }
     
        case R.id.pause:{
         if(player.isPlaying()){
              player.pause(); 
            }
           }
         
        case R.id.stop:{
        if(player.isPlaying()){
          player.stop(); 
        }
      
    }
    }
    }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    @Override
    public void onDestroy(){
    super.onDestroy();
    if(player != null){
    player.release();
    }
    }
    
}


这个小程序还没有实现后台播放音乐,还应该添加service

更多相关文章

  1. Android(安卓)实现全屏显示的几种方法整理
  2. Android不依赖Activity的全局悬浮窗实现
  3. Android不依赖Activity的全局悬浮窗实现
  4. android之照相、相冊裁剪功能的实现过程
  5. 实现Android包的批量删除
  6. android 5.0多用户支持
  7. Android(安卓)选择器 PickerView实例,时间选择器、地址选择器、单
  8. 简单实现Android顶部工具栏和底部工具栏(原创)
  9. 选项卡使用方法二(Android学习随笔十三)

随机推荐

  1. Android笔记汇总
  2. Android(安卓)ApiDemos示例解析(55):Grap
  3. Android(安卓)P版本编译出现报错ninja: b
  4. Android(安卓)性能优化的一些方法
  5. android:imeOptions="actionDone"
  6. Android(安卓)service跨进程调用和启动检
  7. Android(安卓)任意位置(指空间上的位置)弹
  8. 解决Android文档打开慢的问题
  9. foursquare -5 BroadcastReceiver 退出程
  10. Android源码分析:VoIP