【Android源码 栏目提醒】:文章导读:在新的一年中,各位网友都进入紧张的学习或是工作阶段。
网学会员整理了Android源码 -Android下MP3播放器的实现源代码 - 编程语言的相关内容供大家参考,祝大家在新的一年里工作和学习顺利!
Android下MP3播放器的实现源代码03 类十一MusicActivity Java代码 package com.alex.media import java.io.BufferedReader import java.io.File import java.io.FileInputStream import java.io.FileNotFoundException import java.io.IOException import java.io.InputStreamReader import java.io.UnsupportedEncodingException import java.util.Iterator import java.util.TreeMap import
android.app.Activity import
android.content.BroadcastReceiver import
android.content.Context import
android.content.Intent import
android.content.IntentFilter import
android.database.Cursor import
android.media.AudioManager import
android.media.MediaPlayer import
android.net.Uri import
android.os.Bundle import
android.os.Handler import
android.provider.MediaStore import
android.view.KeyEvent import
android.view.MotionEvent import
android.view.View import
android.view.View.OnTouchListener import
android.widget.ImageButton import
android.widget.SeekBar import
android.widget.TextView import
android.widget.SeekBar.OnSeekBarChangeListener public class MusicActivity extends Activity private int _ids private int position private String _titles null private Uri uri private ImageButton playBtn null//播放、暂停 //private Button stopBtn null//停止 private ImageButton latestBtn null//上一首 private ImageButton nextBtn null//下一首 private ImageButton forwardBtn null//快进 private ImageButton rewindBtn null//快退 private TextView lrcText null//歌词文本 private TextView playtime null//已播放时间 private TextView durationTime null//歌曲时间 private SeekBar seekbar null//歌曲进度 private SeekBar soundBar null//音量调节 private Handler handler null//用于进度条 private Handler fHandler null//用于快进 private int currentPosition//当前播放位置 private int duration private DBHelper dbHelper null private TextView name null private TreeMapltInteger LRCbeangt lrc_map new TreeMapltInteger LRCbeangt private Cursor myCur private static final String MUSIC_CURRENT quotcom.alex.currentTimequot private static final String MUSIC_DURATION quotcom.alex.durationquot private static final String MUSIC_NEXT quotcom.alex.nextquot private static final String MUSIC_UPDATE quotcom.alex.updatequot private static final int MUSIC_PLAY 1 private static final int MUSIC_PAUSE 2 private static final int MUSIC_STOP 3 private static final int PROGRESS_CHANGE 4 private static final int MUSIC_REWIND 5 private static final int MUSIC_FORWARD 6 private static final int STATE_PLAY 1 private static final int STATE_PAUSE 2 private int flag //关于音量的变量 private AudioManager mAudioManager null private int maxVolume//最大音量 private int currentVolume//当前音量 Override protected void onCreateBundle savedInstanceState // TODO Auto-generated method stub super.onCreatesavedInstanceState setContentViewR.layout.main1 Intent intent this.getIntent Bundle bundle intent.getExtras _ids bundle.getIntArrayquot_idsquot position bundle.getIntquotpositionquot _titles bundle.getStringArrayquot_titlesquot lrcText TextViewfindViewByIdR.id.lrc name TextViewfindViewByIdR.id.name playtime TextViewfindViewByIdR.id.playtime//已经播放的时间 durationTime TextViewfindViewByIdR.id.duration mAudioManager AudioManager getSystemServiceContext.AUDIO_SERVICE maxVolume mAudioManager.getStreamMaxVolumeAudioManager.STREAM_MUSIC//获得最大音量 currentVolume mAudioManager.getStreamVolumeAudioManager.STREAM_MUSIC//获得当前音量 playBtn ImageButtonfindViewByIdR.id.playBtn playBtn.setOnClickListenernew View.OnClickListener Override public void onClickView v switch flag case STATE_PLAY: pause break case STATE_PAUSE: play break seekbar SeekBarfindViewByIdR.id.seekbar seekbar.setOnSeekBarChangeListenernew OnSeekBarChangeListener Override public void onStopTrackingTouchSeekBar seekBar play Override public void onStartTrackingTouchSeekBar seekBar pause Override public void onProgressChangedSeekBar seekBar int progress boolean fromUser if fromUser seekbar_changeprogress rewindBtn ImageButtonfindViewByIdR.id.rewindBtn rewindBtn.setOnTouchListenernew OnTouchListener Override public boolean onTouchView v MotionEvent event switch event.getAction case MotionEvent.ACTION_DOWN: pause rewind break case MotionEvent.ACTION_UP: play break return true forwardBtn ImageButtonfindViewByIdR.id.forwardBtn forwardBtn.setOnTouchListenernew OnTouchListener Override public boolean onTouchView v MotionEvent event switch event.getAction case MotionEvent.ACTION_DOWN: pause forward break case MotionEvent.ACTION_UP: play break return true latestBtn ImageButtonfindViewByIdR.id.latestBtn latestBtn.setOnClickListenernew View.OnClickListener Override public void onClickView v latestOne nextBtn ImageButtonfindViewByIdR.id.nextBtn nextBtn.setOnClickListenernew View.OnClickListener Override public void onClickView v nextOne Override protected void onStart super.onStart setup play private void loadClip seekbar.setProgress0 //int pos _idsposition name.setText_titlesposition//设置歌曲名 Intent intent new Intent intent.putExtraquot_idsquot _ids intent.putExtraquotpositionquot position intent.setActionquotcom.alex.media.MUSIC_SERVICEquot startServiceintent private void init IntentFilter filter new IntentFilter filter.addActionMUSIC_CURRENT filter.addActionMUSIC_DURATION filter.addActionMUSIC_NEXT filter.addActionMUSIC_UPDATE registerReceivermusicReceiver filter private void setup refreshView loadClip init / 音乐播放 / private void play flag STATE_PLAY playBtn.setBackgroundResourceR.drawable.pause_selecor Intent intent new Intent intent.setActionquotcom.alex.media.MUSIC_SERVICEquot intent.putExtraquotopquotMUSIC_PLAY startServiceintent / 音乐暂停 / private void pause flag STATE_PAUSE playBtn.setBackgroundResourceR.drawable.play_selecor Intent intent new Intent intent.setActionquotcom.alex.media.MUSIC_SERVICEquot intent.putExtraquotopquotMUSIC_PAUSE startServiceintent / 音乐停止 / private void stop unregisterReceivermusicReceiver Intent intent new Intent intent.setActionquotcom.alex.media.MUSIC_SERVICEquot intent.putExtraquotopquot MUSIC_STOP startServiceintent / 用户拖动进度条 / private void seekbar_changeint progress Intent intent new Intent intent.setActionquotcom.alex.media.MUSIC_SERVICEquot intent.putExtraquotopquot PROGRESS_CHANGE intent.putExtraquotprogressquot progress startServiceintent / 快退 / private void rewind Intent intent new Intent intent.setActionquotcom.alex.media.MUSIC_SERVICEquot intent.putExtraquotopquot MUSIC_REWIND startServiceintent / 快进 / private void forward Intent intent new Intent intent.setActionquotcom.alex.media.MUSIC_SERVICEquot intent.putExtraquotopquot MUSIC_FORWARD startServiceintent / 上一首 / private void latestOne if position0 position _ids.length-1 else if positiongt0 position-- stop setup play / 下一首 / private void nextOne if _ids.length1 position position Intent intent new Intent intent.setActionquotcom.alex.media.MUSIC_SERVICEquot intent.putExtraquotlengthquot 1 startServiceintent play return else if position _ids.length-1 position 0 else if position lt _ids.length-1 position stop setup play / 定义musicReceiver接收MusicService发送的广播 / protected BroadcastReceiver musicReceiver new BroadcastReceiver Override public void onReceiveContext context Intent intent String action intent.getAction if action.equalsMUSIC_CURRENT currentPosition intent.getExtras.getIntquotcurrentTimequot//获得当前播放位置 playtime.setTexttoTimecurrentPosition seekbar.setProgresscurrentPosition//设置进度条 IteratorltIntegergt iteratorlrc_map.keySet.iterator whileiterator.hasNext Object o iterator.next LRCbean val lrc_map.geto if valnull if currentPositiongtval.getBeginTime ampampcurrentPositionltval.getBeginTimeval.getLineTime lrcText.setTextval.getLrcBody break else if action.equalsMUSIC_DURATION duration intent.getExtras.getIntquotdurationquot seekbar.setMaxduration durationTime.setTexttoTimeduration else if action.equalsMUSIC_NEXT nextOne else if action.equalsMUSIC_UPDATE position intent.getExtras.getIntquotpositionquot //refreshView //name.setText_titlesposition setup Override protected void onStop super.onStop unregisterReceivermusicReceiver public boolean onKeyDownint keyCode KeyEvent event if keyCode event.KEYCODE_BACK Intent intent new Intent intent.setClassthis ListActivity.class startActivityintent finish return true / 音量控制 / public boolean dispatchKeyEventKeyEvent event int action event.getAction int keyCode event.getKeyCode switch keyCode case KeyEvent.KEYCODE_VOLUME_UP: if action KeyEvent.ACTION_UP if currentVolumeltmaxVolume currentVolume currentVolume 1 mAudioManager.setStreamVolumeAudioManager.STREAM_MUSIC currentVolume 0 else mAudioManager.setStreamVolumeAudioManager.STREAM_MUSIC currentVolume 0 .
上一篇:
下载Android源代码(Linux系统)
下一篇:
谈初中数学“一元二次方程”的几种常见题型