码迷,mamicode.com
首页 > 移动开发 > 详细

部分学习(android)

时间:2015-08-03 19:30:20      阅读:109      评论:0      收藏:0      [点我收藏+]

标签:

login

public class login extends Activity {
    ImageView iv3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.login);
        iv3=(ImageView) findViewById(R.id.iv3);
        iv3.setOnClickListener(new OnClickListener() {            
            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent();
                intent.setClass(login.this,
                        com.anhui.music_player.view.register.class);               
                startActivity(intent);            }        });    }}

login.xml

<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
     android:orientation="vertical" 
    tools:context=".MainActivity" >
    <LinearLayout       
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"       
        android:orientation="horizontal" >  
   <TextView
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:layout_gravity="center_horizontal"
        android:text="用户登陆"
        android:textColor="#ffffff"
        android:background="#3399ff"
        android:textSize="25sp"
        android:gravity="center"         />  
</LinearLayout>
  <LinearLayout       
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"  
        android:paddingTop="35dp"     
        android:orientation="vertical" >
    <EditText
            android:layout_width="fill_parent"
            android:layout_height="50dp"
            android:hint="用户名"           />    
</LinearLayout>
  <LinearLayout       
        android:layout_width="fill_parent"
        android:layout_height="50dp"       
        android:orientation="vertical" >
    <EditText
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="密码"
            android:paddingTop="15dp"           />    
</LinearLayout>  
   <ImageView 
        android:id="@+id/iv2" 
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:src="@drawable/login"
      android:layout_marginTop="30dp"
        android:onClick="doClick"         />
   <ImageView 
        android:id="@+id/iv3" 
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:src="@drawable/register"
        android:layout_marginTop="12dp"
        android:onClick="doClick"         />
   <TextView 
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="使用第三方登录:"
         android:layout_marginTop="30dp"
        android:textSize="25sp"       />
    <LinearLayout       
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
         android:paddingTop="22dp"      
        android:orientation="horizontal" >
    <ImageView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:src="@drawable/login1"          />
    <ImageView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:src="@drawable/login2"         />        
</LinearLayout></LinearLayout>

PlayerService

public class PlayerService extends Service {  
    private MediaPlayer mediaPlayer; // 媒体播放器对象  
    private String path;            // 音乐文件路径  
    private boolean isPause;        // 暂停状态  
    private int current = 0;        // 记录当前正在播放的音乐  
    private List<Music> mp3Infos;   //存放Mp3Info对象的集合  
    private int status = 3;         //播放状态,默认为顺序播放  
    private MyReceiver myReceiver;  //自定义广播接收器  
    private int currentTime;        //当前播放进度  
    private int duration;           //播放长度  
    public static final String UPDATE_ACTION = "com.wwj.action.UPDATE_ACTION";  //更新动作  
    public static final String CTL_ACTION = "com.wwj.action.CTL_ACTION";        //控制动作  
    public static final String MUSIC_CURRENT = "com.wwj.action.MUSIC_CURRENT";  //当前音乐播放时间更新动作  
    public static final String MUSIC_DURATION = "com.wwj.action.MUSIC_DURATION";//新音乐长度更新动作  
    private Handler handler = new Handler() {  
        public void handleMessage(android.os.Message msg) {  
            if (msg.what == 1) {  
                if(mediaPlayer != null) {  
                    currentTime = mediaPlayer.getCurrentPosition(); // 获取当前音乐播放的位置  
                    Intent intent = new Intent();  
                    intent.setAction(MUSIC_CURRENT);  
                    intent.putExtra("currentTime", currentTime);  
                    sendBroadcast(intent); // 给PlayerActivity发送广播  
                    handler.sendEmptyMessageDelayed(1, 1000);  
                }                                }          };      };  
    @Override  
    public void onCreate() {  
        super.onCreate();  
        Log.d("service", "service created");  
        mediaPlayer = new MediaPlayer();  
        mp3Infos =MusicList.getMusicData(this);   
        mediaPlayer.setOnCompletionListener(new OnCompletionListener() {    
            @Override  
            public void onCompletion(MediaPlayer mp1) {  
                if (status == 1) { // 单曲循环  
                    mediaPlayer.start();  
                } 
                else if (status == 2) { // 全部循环  
                    current++;  
                    if(current > mp3Infos.size() - 1) {  //变为第一首的位置继续播放  
                        current = 0;  
                    }               
                } else if (status == 3) { // 顺序播放  
                    current++;  //下一首位置  
                    if (current <= mp3Infos.size() - 1) {  
                        Intent sendIntent = new Intent(UPDATE_ACTION);  
                        sendIntent.putExtra("current", current);  
                        sendBroadcast(sendIntent);  
                        path = mp3Infos.get(current).getUrl();  
                        play(0);  
                    }else {  
                        mediaPlayer.seekTo(0);  
                        current = 0;  
                        Intent sendIntent = new Intent(UPDATE_ACTION);  
                        sendIntent.putExtra("current", current);  
                        sendBroadcast(sendIntent);  
                    }  
                } else if(status == 4) {    //随机播放  
                    current = getRandomIndex(mp3Infos.size() - 1);  
                    System.out.println("currentIndex ->" + current);  
                    Intent sendIntent = new Intent(UPDATE_ACTION);  
                    sendIntent.putExtra("current", current);   
                    sendBroadcast(sendIntent);  
                    path = mp3Infos.get(current).getUrl();  
                    play(0);                  }              }          });   
        myReceiver = new MyReceiver();  
        IntentFilter filter = new IntentFilter();
        registerReceiver(myReceiver, filter);      }  
    protected int getRandomIndex(int end) {  
        int index = (int) (Math.random() * end);  
        return index;      }  
    @Override  
    public IBinder onBind(Intent arg0) {  
        return null;      }  
    private void play(int currentTime) {  
        try {  
            mediaPlayer.reset();// 把各项参数恢复到初始状态  
            mediaPlayer.setDataSource(path);  
            mediaPlayer.prepare(); // 进行缓冲  
            mediaPlayer.setOnPreparedListener(new PreparedListener(currentTime));// 注册一个监听器  
            handler.sendEmptyMessage(1);  
        } catch (Exception e) {  
            e.printStackTrace();          }      }  
    private void pause() {  
        if (mediaPlayer != null && mediaPlayer.isPlaying()) {  
            mediaPlayer.pause();  
            isPause = true;          }      }    
    private void resume() {          if (isPause) {  
            mediaPlayer.start();  
            isPause = false;          }      }  
    private void previous() {  
        Intent sendIntent = new Intent(UPDATE_ACTION);  
        sendIntent.putExtra("current", current); 
        sendBroadcast(sendIntent);  
        play(0);  
    }   
    private void next() {  
        Intent sendIntent = new Intent(UPDATE_ACTION);  
        sendIntent.putExtra("current", current);
        sendBroadcast(sendIntent);  
        play(0);      }  
    private void stop() {  
        if (mediaPlayer != null) {  
            mediaPlayer.stop();  
            try {  
                mediaPlayer.prepare(); // 在调用stop后如果需要再次通过start进行播放,需要之前调用prepare函数  
            } catch (Exception e) {  
                e.printStackTrace();              }          }      }   
    @Override  
    public void onDestroy() {  
        if (mediaPlayer != null) {  
            mediaPlayer.stop();  
            mediaPlayer.release();  
            mediaPlayer = null;          }                }  
    private final class PreparedListener implements OnPreparedListener {  
        private int currentTime;  
  
        public PreparedListener(int currentTime) {  
            this.currentTime = currentTime;         }    
        @Override  
        public void onPrepared(MediaPlayer mp) {  
            mediaPlayer.start(); // 开始播放  
            if (currentTime > 0) { // 如果音乐不是从头播放  
                mediaPlayer.seekTo(currentTime);              }  
            Intent intent = new Intent();  
            intent.setAction(MUSIC_DURATION);  
            duration = mediaPlayer.getDuration();  
            intent.putExtra("duration", duration);  //通过Intent来传递歌曲的总长度  
            sendBroadcast(intent);          }      }    
    public class MyReceiver extends BroadcastReceiver {  
  
        @Override  
        public void onReceive(Context context, Intent intent) {  
            int control = intent.getIntExtra("control", -1);  
            switch (control) {  
            case 1:                  status = 1; // 将播放状态置为1表示:单曲循环                  break;  
            case 2:                  status = 2; //将播放状态置为2表示:全部循环                  break;  
            case 3:                  status = 3; //将播放状态置为3表示:顺序播放                  break;  
            case 4:                  status = 4; //将播放状态置为4表示:随机播放                  break;              }          }      }   }


部分学习(android)

标签:

原文地址:http://my.oschina.net/u/1994482/blog/487158

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!