音乐播放器的实现(简易版)
2013-09-17 21:55
316 查看
截图:
代码如下:
1、main.xml
2、MainActivity
3、AndroidManifest.xml
代码如下:
1、main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="女神牌音乐播放器(章泽天,我的女神)" /> <EditText android:id="@+id/et_name" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="2.mp3" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal"> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="播放" android:onClick="play" /> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="暂停" android:onClick="pause" /> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="停止" android:onClick="stop" /> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="重播" android:onClick="reset" /> </LinearLayout> </LinearLayout>
2、MainActivity
package com.njupt.mp3_1; import java.io.File; 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 { private MediaPlayer mp; private File file; private EditText et_name; private boolean pause; private int position = 0; private TelephonyManager tm; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); et_name = (EditText) findViewById(R.id.et_name); tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); tm.listen(new MyPhoneStateListener(), PhoneStateListener.LISTEN_CALL_STATE); } private class MyPhoneStateListener extends PhoneStateListener{ @Override public void onCallStateChanged(int state, String incomingNumber) { super.onCallStateChanged(state, incomingNumber); switch (state) { case TelephonyManager.CALL_STATE_IDLE: if(position > 0 && mp != null){ mp.seekTo(position); mp.start(); } break; case TelephonyManager.CALL_STATE_OFFHOOK: if(mp!= null){ if(mp.isPlaying()){ position = mp.getCurrentPosition(); mp.pause(); } } case TelephonyManager.CALL_STATE_RINGING: if(mp != null){ if(mp.isPlaying()){ position = mp.getCurrentPosition(); mp.pause(); } } default: break; } } } public void play(View v){ String name = et_name.getText().toString(); file = new File(Environment.getExternalStorageDirectory(),name); if(!file.exists()){ Toast.makeText(this, "sorry,文件不存在", 1).show(); }else{ play(); } } public void play(){ try{ mp = new MediaPlayer(); mp.reset(); mp.setDataSource(file.getAbsolutePath()); mp.prepare(); mp.setOnPreparedListener(new MyOnPreparedListener()); }catch(Exception e){ e.printStackTrace(); } } private class MyOnPreparedListener implements OnPreparedListener{ @Override public void onPrepared(MediaPlayer mp) { mp.start(); } } public void pause(View v){ Button bt = (Button) v; if(mp != null){ if(mp.isPlaying()){ mp.pause(); bt.setText("继续"); pause = true; }else{ if(pause){ mp.start(); bt.setText("暂停"); pause = false; } } } } public void stop(View v){ if(mp != null){ mp.stop(); } } public void reset(View v){ stop(v); play(v); } @Override protected void onDestroy() { super.onDestroy(); if(mp != null){ mp.release(); mp = null; } } @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; } }
3、AndroidManifest.xml
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
相关文章推荐
- Android 使用Service实现简易音乐播放器
- android 简易音乐播放器的实现
- 简易音乐播放器(js,html,css实现)
- Android简易音乐播放器之界面实现(第一篇)
- Android简易音乐播放器之播放列表实现(第二篇)
- Java实现简易音乐播放器
- 【Java实战】Java实现简易音乐播放器
- 安卓使用MediaPlayer实现简易音乐播放器(应用内的音频文件)
- Android简易音乐播放器实现代码
- Android简易音乐播放器实现代码
- 安卓使用MediaPlayer实现简易音乐播放器(应用内的音频文件)
- 音乐播放器的实现(简易版)
- Android应用开发之简易、大气音乐播放器实现专辑倒影效果
- 使用MediaPlayer实现播放、暂停、继续和停止功能的简易音乐播放器
- 音乐播放器的实现(简易版)
- Android简易的音乐播放器实现
- 用HTML5实现简易的音乐播放器
- pygame实现简易音乐播放器
- 通过html5实现简易的音乐播放器
- 简单实现java音乐播放器