网络音乐播放器
一。布局
文本框默认不支持边框,这里使用了一个自定义的xml配置。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<LinearLayout android:orientation="vertical"> <TextView android:id="@+id/tv_sound" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/textview_border" android:text="http://www.gaohaiyan.com/download/359/" /> <Button android:id="@+id/btn_play" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#33cc33" android:layout_marginTop="10px" android:text="播放" /> <Button android:id="@+id/btn_push" android:text="暂停" /> <Button android:id="@+id/btn_stop" android:text="停止" /> <Button android:id="@+id/btn_afresh" android:text="重播" /> </LinearLayout> |
1 2 3 4 5 6 7 |
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <solid android:color="#ffffff" /> <stroke android:width="1dip" android:color="#333333"/> </shape> |
二。主程
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
public class MainActivity extends Activity implements OnClickListener { private String sound; private Button btn_play, btn_push, btn_stop, btn_afresh; /** 媒体播放器 */ private MediaPlayer mediaPlayer; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView tv_sound = (TextView) findViewById(R.id.tv_sound); sound = tv_sound.getText().toString(); btn_play = (Button) findViewById(R.id.btn_play); btn_play.setOnClickListener(this); btn_push = (Button) findViewById(R.id.btn_push); btn_push.setOnClickListener(this); btn_stop = (Button) findViewById(R.id.btn_stop); btn_stop.setOnClickListener(this); btn_afresh = (Button) findViewById(R.id.btn_afresh); btn_afresh.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_play: if (sound.startsWith("http://")) { try { // 1.初始化媒体播放器 mediaPlayer = new MediaPlayer(); // 2.设置播放的数据源 mediaPlayer.setDataSource(sound); // 3.设置播放方式 mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); // 4.通知底层做准备,通过C控制硬件 // mediaPlayer.prepare(); // 同步的准备,一般用于本地资源,基本无延迟 mediaPlayer.prepareAsync(); // 异步的准备,一般用于网络,加载、缓存资源同时即播放 // 5.网络缓存情况下的播放处理 mediaPlayer.setOnPreparedListener(new OnPreparedListener() { @Override public void onPrepared(MediaPlayer mp) { // 6.播放网络音频 mediaPlayer.start(); // 开始播放后,按钮不能再点击 btn_play.setEnabled(false); } }); // 6.播放完毕事件处理 mediaPlayer.setOnCompletionListener(new OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { btn_play.setEnabled(true); } }); } catch (Exception e) { Toast.makeText(this, "播放失败", 0).show(); e.printStackTrace(); } } else { Toast.makeText(this, "请检查文件的路径", 0).show(); } break; case R.id.btn_push: if("继续".equals(btn_push.getText().toString())){ mediaPlayer.start(); btn_push.setText("暂停"); return; } // 如果正在播放 if(mediaPlayer!=null&&mediaPlayer.isPlaying()){ // 暂停播放 mediaPlayer.pause(); btn_push.setText("继续"); } break; case R.id.btn_stop: if(mediaPlayer!=null&&mediaPlayer.isPlaying()){ // 停止播放 mediaPlayer.stop(); // 释放资源 mediaPlayer.release(); mediaPlayer = null; } btn_push.setText("暂停"); btn_play.setEnabled(true); break; case R.id.btn_afresh: if(mediaPlayer!=null&&mediaPlayer.isPlaying()){ // 设置播放位置,0秒位置 mediaPlayer.seekTo(0); }else{ onClick(btn_play); } btn_push.setText("暂停"); break; default: break; } } } |
三。权限
1 2 3 |
<uses-permission android:name="android.permission.INTERNET"/> |
声明
本文由崔维友 威格灵 cuiweiyou vigiles cuiweiyou 原创,转载请注明出处:http://www.gaohaiyan.com/361.html
承接App定制、企业web站点、办公系统软件 设计开发,外包项目,毕设