android音乐播放器(雏形)
来源:互联网 发布:网络公选课时间 编辑:程序博客网 时间:2024/05/01 13:55
建议:学习本实例之前,请掌握Activity的生命周期相关的事件和方法,这样学习效果会更好。
本实例仅供参考学习,并非一款非常完善的产品。由于时间和本人技术有限,不足或者错误之处敬请谅解。希望热心的网友能够继续完善。
下面是Activity部分代码(我一般都会有详细注释):
package cn.chaoyang.activity;import java.io.File;import java.io.IOException;import android.app.Activity;import android.media.MediaPlayer;import android.os.Bundle;import android.os.Environment;import android.text.BoringLayout.Metrics;import android.view.View;import android.widget.Button;import android.widget.EditText;//学习本实例之前,请掌握Activity的生命周期和相关的方法,这样学习效果会更好。public class MainActivity extends Activity {private MediaPlayer mediaplayer;private EditText txtName;private int postion;private String fileName; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ButtonClickListener listener =new ButtonClickListener(); txtName =(EditText)this.findViewById(R.id.inputName); Button btnPlay =(Button)this.findViewById(R.id.btnPlay); Button btnPause =(Button) this.findViewById(R.id.btnPause); Button btnStop =(Button) this.findViewById(R.id.btnStop); Button btnResart=(Button) this.findViewById(R.id.btnRestart); btnPlay.setOnClickListener(listener); btnPause.setOnClickListener(listener); btnStop.setOnClickListener(listener); btnResart.setOnClickListener(listener); } //当系统恢复后,可以重新读取出之前保存的状态值 @Overrideprotected void onRestoreInstanceState(Bundle savedInstanceState) { this.fileName=savedInstanceState.getString("fileName"); this.postion=savedInstanceState.getInt("postion"); super.onRestoreInstanceState(savedInstanceState);} //当发生意外时,在系统将Activity的进程杀死之前,保存一些状态值@Overrideprotected void onSaveInstanceState(Bundle outState) {outState.putString("fileName", fileName);outState.putInt("postion",postion);super.onSaveInstanceState(outState);} //onDestroy方法可以杀掉程序的进程,彻底释放资源@Overrideprotected void onDestroy() {mediaplayer.release();super.onDestroy();} //如果打电话结束了,继续播放音乐@Overrideprotected void onResume() {if(postion>0&&fileName!=null){try {play();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}mediaplayer.seekTo(postion);postion=0;}super.onResume();} //如果突然来电话或者来短信,Acticity会暂停,停止播放音乐@Overrideprotected void onPause() {if(mediaplayer.isPlaying()){postion =mediaplayer.getCurrentPosition();//保存当前播放点mediaplayer.stop();}super.onPause();}private final class ButtonClickListener implements View.OnClickListener { @Overridepublic void onClick(View v) {// TODO Auto-generated method stubmediaplayer =new MediaPlayer();Button button =(Button) v ;try {switch (v.getId()){//播放case R.id.btnPlay:if(!mediaplayer.isPlaying()){play();}break;//暂停case R.id.btnPause://如果正在播放,则按下按钮后暂停.且按钮上的文本显示为"继续“if(mediaplayer.isPlaying()){mediaplayer.pause();button.setText(R.string.txtContinue);//设置按钮文本}else{//如果是暂停状态,按下按钮后继续播放 //play();}break;//停止case R.id.btnStop:if(mediaplayer.isPlaying()){mediaplayer.stop();}break;//重复case R.id.btnRestart:if(mediaplayer.isPlaying()){mediaplayer.seekTo(0);}else{play();}break;}}catch (Exception e) {// TODO: handle exception}} }private void play() throws IOException{//获得音乐文件的绝对路径 fileName=txtName.getText().toString();File file =new File(Environment.getExternalStorageDirectory(),fileName); mediaplayer.reset();//归位mediaplayer.setDataSource(file.getAbsolutePath());//设置需要播放的数据源 mediaplayer.prepare();mediaplayer.start();}}
下面是软件布局文件代码,很简单的线性布局
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" ><TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/labName" /> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/inputName" /> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/play" android:id="@+id/btnPlay" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/pause" android:id="@+id/btnPause" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/stop" android:id="@+id/btnStop" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/restart" android:id="@+id/btnRestart" /> </LinearLayout></LinearLayout>
下面是资源文件string.xml代码
<?xml version="1.0" encoding="utf-8"?><resources> <string name="hello">Hello World, MainActivity!</string> <string name="app_name">mp3播放器</string> <string name="labName">输入歌名</string> <string name="play">播放</string> <string name="pause">暂停</string> <string name="stop">停止</string> <string name="restart">重复</string> <string name="txtContinue">继续</string></resources>
本实例的目的,是为了熟悉android中音频接口的使用及相关操作,巩固Activity生命周期的相关知识。至于页面布局部分,采用的是非常傻瓜式的显示和控制风格。
如果想要开发一款完善的(音乐播放器相关的)产品,还有太多太多的地方需要完善。
- android音乐播放器(雏形)
- android 音乐播放器(播放,停止)
- android音乐播放器播放音乐卡
- Android 音乐播放 类似音乐播放器
- android 音乐播放器
- Android 音乐播放器
- android 音乐播放器
- Android音乐播放器
- Android--音乐播放器
- Android音乐播放器
- android 音乐播放器
- Android音乐播放器
- Android 音乐播放器
- Android音乐播放器
- Android音乐播放器
- Android:音乐播放器
- Android 音乐播放器。
- android 音乐播放器
- controller/action/parameters 默认值设置
- ISP 接口隔离原则 Interface Seperate Principle
- .诊断并解决ORA-04031 错误
- 用C#编写ActiveX控件(二)
- VC 中使用CHtmlView
- android音乐播放器(雏形)
- 给老妈买了几件衣服
- 健康笔录
- AutoCode 2010 [A powerful add-in for Visual Studio .NET]
- adb getevent/sendevent
- 自动激活ActiveX控件与签名\发布
- 文件间调用变量(extern,include)
- PowerDesigner 面向对象模型(OOM)说明
- ubutu的触摸板命令