日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Android之MediaPlayer播放音乐并实现进度条实例

發布時間:2025/6/15 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android之MediaPlayer播放音乐并实现进度条实例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

首先,說明我們是從sd卡里讀文件,來播放文件!!

1、效果圖:


提前工作,往sd卡里放音樂文件,如圖:



2、布局文件main.xml

[html] view plain copy
  • <?xml?version="1.0"?encoding="utf-8"?>??
  • <LinearLayout?xmlns:android="http://schemas.android.com/apk/res/android"??
  • ????android:layout_width="fill_parent"??
  • ????android:layout_height="fill_parent"??
  • ????android:orientation="vertical"?>??
  • ????<TextView???
  • ????????android:layout_width="wrap_content"??
  • ????????android:layout_height="wrap_content"??
  • ????????android:text="歌曲名:"/>??
  • ????<TextView???
  • ????????android:layout_width="wrap_content"??
  • ????????android:layout_height="wrap_content"??
  • ????????android:text="blueflawer.mp3"/>??
  • ????<Button???
  • ????????android:layout_width="wrap_content"??
  • ????????android:layout_height="wrap_content"??
  • ????????android:text="播放"??
  • ????????android:id="@+id/play_pause"/>??
  • ????<Button???
  • ????????android:layout_width="wrap_content"??
  • ????????android:layout_height="wrap_content"??
  • ????????android:text="重置"??
  • ????????android:id="@+id/reset"/>??
  • ????<SeekBar???
  • ????????android:layout_width="fill_parent"??
  • ????????android:layout_height="wrap_content"??
  • ????????android:id="@+id/seekbar"/>??
  • </LinearLayout>??
  • 3、activity類 [java] view plain copy
  • package?cn.csdn.activity;??
  • ??
  • import?java.io.File;??
  • import?java.io.IOException;??
  • import?java.util.Timer;??
  • import?java.util.TimerTask;??
  • ??
  • import?android.app.Activity;??
  • import?android.media.MediaPlayer;??
  • import?android.os.Bundle;??
  • import?android.os.Environment;??
  • import?android.view.View;??
  • import?android.view.View.OnClickListener;??
  • import?android.widget.Button;??
  • import?android.widget.SeekBar;??
  • import?android.widget.SeekBar.OnSeekBarChangeListener;??
  • ??
  • public?class?MyPlayerActivity?extends?Activity?{??
  • ????private?Button?play_pause,?reset;??
  • ????private?SeekBar?seekbar;??
  • ????private?boolean?ifplay?=?false;??
  • ????private?MediaPlayer?player?=?null;??
  • ????private?String?musicName?=?"blueflawer.mp3";??
  • ????private?boolean?iffirst?=?false;??
  • ????private?Timer?mTimer;????
  • ????private?TimerTask?mTimerTask;???
  • ????private?boolean?isChanging=false;//互斥變量,防止定時器與SeekBar拖動時進度沖突???
  • ????public?void?onCreate(Bundle?savedInstanceState)?{??
  • ????????super.onCreate(savedInstanceState);??
  • ????????setContentView(R.layout.main);??
  • ????????player?=?new?MediaPlayer();??
  • ????????findViews();//?各組件??
  • ????}??
  • ??
  • ????private?void?findViews()?{??
  • ????????play_pause?=?(Button)?findViewById(R.id.play_pause);??
  • ????????reset?=?(Button)?findViewById(R.id.reset);??
  • ????????play_pause.setOnClickListener(new?MyClick());??
  • ????????reset.setOnClickListener(new?MyClick());??
  • ??
  • ????????seekbar?=?(SeekBar)?findViewById(R.id.seekbar);??
  • ????????seekbar.setOnSeekBarChangeListener(new?MySeekbar());??
  • ????}??
  • ??
  • ????class?MyClick?implements?OnClickListener?{??
  • ????????public?void?onClick(View?v)?{??
  • ????????????File?file?=?new?File(Environment.getExternalStorageDirectory(),??
  • ????????????????????musicName);??
  • ????????????//?判斷有沒有要播放的文件??
  • ????????????if?(file.exists())?{??
  • ????????????????switch?(v.getId())?{??
  • ????????????????case?R.id.play_pause:??
  • ????????????????????if?(player?!=?null?&&?!ifplay)?{??
  • ????????????????????????play_pause.setText("暫停");??
  • ????????????????????????if?(!iffirst)?{??
  • ????????????????????????????player.reset();??
  • ????????????????????????????try?{??
  • ????????????????????????????????player.setDataSource(file.getAbsolutePath());??
  • ????????????????????????????????player.prepare();//?準備??
  • ??
  • ????????????????????????????}?catch?(IllegalArgumentException?e)?{??
  • ????????????????????????????????e.printStackTrace();??
  • ????????????????????????????}?catch?(IllegalStateException?e)?{??
  • ????????????????????????????????e.printStackTrace();??
  • ????????????????????????????}?catch?(IOException?e)?{??
  • ????????????????????????????????e.printStackTrace();??
  • ????????????????????????????}??
  • ????????????????????????????seekbar.setMax(player.getDuration());//設置進度條??
  • ????????????????????????????//----------定時器記錄播放進度---------//????
  • ????????????????????????????mTimer?=?new?Timer();????
  • ????????????????????????????mTimerTask?=?new?TimerTask()?{????
  • ????????????????????????????????@Override????
  • ????????????????????????????????public?void?run()?{?????
  • ????????????????????????????????????if(isChanging==true)?{???
  • ????????????????????????????????????????return;????
  • ????????????????????????????????????}??
  • ????????????????????????????????????seekbar.setProgress(player.getCurrentPosition());??
  • ????????????????????????????????}????
  • ????????????????????????????};???
  • ????????????????????????????mTimer.schedule(mTimerTask,?0,?10);???
  • ????????????????????????????iffirst=true;??
  • ????????????????????????}??
  • ????????????????????????player.start();//?開始??
  • ????????????????????????ifplay?=?true;??
  • ????????????????????}?else?if?(ifplay)?{??
  • ????????????????????????play_pause.setText("繼續");??
  • ????????????????????????player.pause();??
  • ????????????????????????ifplay?=?false;??
  • ????????????????????}??
  • ????????????????????break;??
  • ????????????????case?R.id.reset:??
  • ????????????????????if?(ifplay)?{??
  • ????????????????????????player.seekTo(0);??
  • ????????????????????}?else?{??
  • ????????????????????????player.reset();??
  • ????????????????????????try?{??
  • ????????????????????????????player.setDataSource(file.getAbsolutePath());??
  • ????????????????????????????player.prepare();//?準備??
  • ????????????????????????????player.start();//?開始??
  • ????????????????????????}?catch?(IllegalArgumentException?e)?{??
  • ????????????????????????????e.printStackTrace();??
  • ????????????????????????}?catch?(IllegalStateException?e)?{??
  • ????????????????????????????e.printStackTrace();??
  • ????????????????????????}?catch?(IOException?e)?{??
  • ????????????????????????????e.printStackTrace();??
  • ????????????????????????}??
  • ????????????????????}??
  • ????????????????????break;??
  • ????????????????}??
  • ????????????}??
  • ????????}??
  • ????}??
  • ????//進度條處理??
  • ????class?MySeekbar?implements?OnSeekBarChangeListener?{??
  • ????????public?void?onProgressChanged(SeekBar?seekBar,?int?progress,??
  • ????????????????boolean?fromUser)?{??
  • ????????}??
  • ??
  • ????????public?void?onStartTrackingTouch(SeekBar?seekBar)?{??
  • ????????????isChanging=true;????
  • ????????}??
  • ??
  • ????????public?void?onStopTrackingTouch(SeekBar?seekBar)?{??
  • ????????????player.seekTo(seekbar.getProgress());??
  • ????????????isChanging=false;????
  • ????????}??
  • ??
  • ????}??
  • ????//來電處理??
  • ????protected?void?onDestroy()?{??
  • ????????if(player?!=?null){??
  • ????????????if(player.isPlaying()){??
  • ????????????????player.stop();??
  • ????????????}??
  • ????????????player.release();??
  • ????????}??
  • ????????super.onDestroy();??
  • ????}??
  • ??
  • ????protected?void?onPause()?{??
  • ????????if(player?!=?null){??
  • ????????????if(player.isPlaying()){??
  • ????????????????player.pause();??
  • ????????????}??
  • ????????}??
  • ????????super.onPause();??
  • ????}??
  • ??
  • ????protected?void?onResume()?{??
  • ????????if(player?!=?null){??
  • ????????????if(!player.isPlaying()){??
  • ????????????????player.start();??
  • ????????????}??
  • ????????}??
  • ????????super.onResume();??
  • ????}??
  • ??
  • }?
  • 總結

    以上是生活随笔為你收集整理的Android之MediaPlayer播放音乐并实现进度条实例的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。