很长时间没有写博客了,懒惰是编程最不可有的坏习惯,争取每天抽点时间敲敲代码。言归正传,今天实现一个音乐播放的小例子,编程就是不断累积不断记忆的一个过程
没有捷径可取。
简单的界面,这里只是知识的积累,没必要做的很精致。
布局文件:
<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" tools:context="${relativePackage}.${activityClass}" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <EditText android:id="@+id/filename" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Let It Go.mp3" > </EditText> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/play" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/play" android:onClick="Mediaplay"/> <Button android:id="@+id/pause" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/pause" android:onClick="Mediaplay"/> <Button android:id="@+id/replay" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/replay" android:onClick="Mediaplay"/> <Button android:id="@+id/stop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/stop" android:onClick="Mediaplay"/> </LinearLayout> </LinearLayout>
Mainactivity :
package com.example.day10_musicplay; import java.io.File; import java.io.IOException; import android.app.Activity; import android.content.Context; import android.media.MediaPlayer; import android.media.MediaPlayer.OnPreparedListener; import android.os.Bundle; import android.os.Environment; import android.telephony.PhoneStateListener; import android.telephony.TelephonyManager; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { private EditText filename; private String path; private MediaPlayer mediaplayer; private boolean pause; private int posion; private TelephonyManager telephonyManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mediaplayer = new MediaPlayer(); filename = (EditText) this.findViewById(R.id.filename); telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);//获取电话监听 telephonyManager.listen(new MyphoneListener(), PhoneStateListener.LISTEN_CALL_STATE); } public class MyphoneListener extends PhoneStateListener{ @Override public void onCallStateChanged(int state, String incomingNumber) { switch (state) { case TelephonyManager.CALL_STATE_RINGING://来电 if(mediaplayer.isPlaying()){ posion = mediaplayer.getCurrentPosition(); mediaplayer.stop(); } break; case TelephonyManager.CALL_STATE_IDLE://结束的通话 if(posion>0 && path!=null){ play(posion); posion =0; } break; } } } @Override protected void onDestroy() { mediaplayer.release();//将创建的对象销毁 mediaplayer=null; super.onDestroy(); } public void Mediaplay (View v){ switch (v.getId()) { case R.id.play: String file = filename.getText().toString(); File name = new File(Environment.getExternalStorageDirectory(), file); if(name.exists()){//判断文件是否存在 path = name.getAbsolutePath(); play(0); }else{ Toast.makeText(MainActivity.this, R.string.error, 5).show(); } break; case R.id.pause: if(mediaplayer.isPlaying()){ mediaplayer.pause();//暂停 ((Button)v).setText(R.string.continues); pause=true; }else{ if(pause){ mediaplayer.start();//继续播放 ((Button)v).setText(R.string.pause); pause=false; } } break; case R.id.replay: if(mediaplayer.isPlaying()){ mediaplayer.seekTo(0);//从开始位置播放音乐 }else{ if(path!=null){ play(0); } } break; case R.id.stop: if(mediaplayer.isPlaying()){ mediaplayer.stop(); } break; } } private void play(int posion) { try { mediaplayer.reset();//对各项参数进行初始化准备 mediaplayer.setDataSource(path);//设置数据源 mediaplayer.prepare();//数据的缓冲 mediaplayer.setOnPreparedListener( new PreparedListener(posion)); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private class PreparedListener implements OnPreparedListener{ private int posion; public PreparedListener(int posion) { this.posion=posion; } @Override public void onPrepared(MediaPlayer mp) { mediaplayer.start();//播放 if(posion>0) mediaplayer.seekTo(posion); } } }
清单文件加上权限:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/><!-- 监听通话权限 -->
里面注释很详细。
先说说这里学到的一个很实用的技巧,就是按钮的OnClick方法的处理。相信很多和我一样的初学者都有一个苦恼就是每添加一个按钮就要写一个fiandViewById和一个监听器,使内容很斑驳也很臃肿,造成其他重要的业务逻辑都不是那么明显的显示。
这里只要在建立按钮的xml文件时,加上android:onClick="方法名" 这个参数就行了。到了activity里,只需要写一下该方法,在用switch,cath方法就可以很快捷和方便的将所有按钮的业务逻辑集成在一起,很实用的一个方法。