android第十八步音乐播放器
音乐播放器使用Android自带SDK实现
主要功能包括:播放、暂停、重播、停止、电话来时暂停通话结束后继续的功能
MediaPlayer:用于播放多媒体文件
方法:release()是否资源
isPlaying()是否正在播放
getCurrentPosition()获取播放位置,返回值为int型
reset()把各项参数恢复到初始状态
setDataSource(path)设置要播放的文件path路径
prepare()进行缓冲
start()开始播放
pause()暂停
seekTo(0)跳转到哪,0为从头开始播放
stop()停止播放
事件:setOnPreparedListener缓存完毕触发
TelephonyManager获取手机信息
listen根据状态监听手机,如果是监听的状体就触发事件
package com.example.musicplay; import java.io.File; import java.io.IOException; import android.media.MediaPlayer; import android.media.MediaPlayer.OnPreparedListener; import android.os.Bundle; import android.os.Environment; import android.app.Activity; import android.content.Context; import android.telephony.PhoneStateListener; import android.telephony.TelephonyManager; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { EditText editText; String path; MediaPlayer mediaPlayer; boolean pause; int position; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editText = (EditText)this.findViewById(R.id.filename); mediaPlayer = new MediaPlayer(); TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE); telephonyManager.listen(new MyPhoneListener(), PhoneStateListener.LISTEN_CALL_STATE); } private final class MyPhoneListener extends PhoneStateListener{ @Override public void onCallStateChanged(int state, String incomingNumber) { switch(state){ case TelephonyManager.CALL_STATE_RINGING: if(mediaPlayer.isPlaying()){ position=mediaPlayer.getCurrentPosition(); mediaPlayer.stop(); } break; case TelephonyManager.CALL_STATE_IDLE: if(position >0 && path!=null){ play(position); } break; } } } @Override protected void onDestroy() { mediaPlayer.release(); mediaPlayer = null; super.onDestroy(); } public void play(View view){ String filename = editText.getText().toString(); switch (view.getId()) { case R.id.play: File audio = new File("/data/data/com.example.musicplay/1.mp3"); if(audio.exists()){ path=audio.getAbsolutePath(); play(0); } else{ Toast.makeText(getApplicationContext(),path+ " "+ R.string.filenoexist, Toast.LENGTH_LONG).show(); } break; case R.id.pause: if(mediaPlayer.isPlaying()){ mediaPlayer.pause(); pause=true; ((Button)view).setText(R.string.continues); } else{ if(pause){ mediaPlayer.start(); pause=false; ((Button)view).setText(R.string.pausebutton); } } break; case R.id.reset: if(mediaPlayer.isPlaying()){ mediaPlayer.seekTo(0); } else{ if(path!=null){ play(0); } } break; case R.id.stop: if(mediaPlayer.isPlaying()){ mediaPlayer.stop(); } break; default: break; } } /* @Override protected void onPause() { if(mediaPlayer.isPlaying()){ position=mediaPlayer.getCurrentPosition(); mediaPlayer.stop(); } super.onPause(); } @Override protected void onResume() { if(position >0 && path!=null){ play(); mediaPlayer.seekTo(position); } super.onResume(); }*/ private void play(int p){ try { mediaPlayer.reset(); //把各项参数恢复到初始状态 mediaPlayer.setDataSource(path); mediaPlayer.prepare();//进行缓冲 mediaPlayer.setOnPreparedListener(new PrepareListener(p)); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } private final class PrepareListener implements OnPreparedListener{ int pos; public PrepareListener(int p) { pos = p; // TODO Auto-generated constructor stub } @Override public void onPrepared(MediaPlayer arg0) { mediaPlayer.start(); if(pos>0){ mediaPlayer.seekTo(pos); } } } @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; } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。