編輯:關於Android編程
main.xml的源碼:
本文程序的源碼,有點長:
package com.testMedia; import java.io.IOException; import java.util.Timer; import java.util.TimerTask; import android.app.Activity; import android.media.AudioManager; import android.media.MediaPlayer; import android.os.Bundle; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; import android.widget.Button; import android.widget.SeekBar; import android.widget.Toast; public class testMedia extends Activity { /** Called when the activity is first created. */ private SeekBar skb_audio=null; private Button btn_start_audio = null; private Button btn_stop_audio = null; private SeekBar skb_video=null; private Button btn_start_video = null; private Button btn_stop_video = null; private SurfaceView surfaceView; private SurfaceHolder surfaceHolder; private MediaPlayer m = null; private Timer mTimer; private TimerTask mTimerTask; private boolean isChanging=false;//互斥變量,防止定時器與SeekBar拖動時進度沖突 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //----------Media控件設置---------// m=new MediaPlayer(); //播放結束之後彈出提示 m.setOnCompletionListener(new MediaPlayer.OnCompletionListener(){ @Override public void onCompletion(MediaPlayer arg0) { Toast.makeText(testMedia.this, "結束", 1000).show(); m.release(); } }); //----------定時器記錄播放進度---------// mTimer = new Timer(); mTimerTask = new TimerTask() { @Override public void run() { if(isChanging==true) return; if(m.getVideoHeight()==0) skb_audio.setProgress(m.getCurrentPosition()); else skb_video.setProgress(m.getCurrentPosition()); } }; mTimer.schedule(mTimerTask, 0, 10); btn_start_audio = (Button) this.findViewById(R.id.Button01); btn_stop_audio = (Button) this.findViewById(R.id.Button02); btn_start_audio.setOnClickListener(new ClickEvent()); btn_stop_audio.setOnClickListener(new ClickEvent()); skb_audio=(SeekBar)this.findViewById(R.id.SeekBar01); skb_audio.setOnSeekBarChangeListener(new SeekBarChangeEvent()); btn_start_video = (Button) this.findViewById(R.id.Button03); btn_stop_video = (Button) this.findViewById(R.id.Button04); btn_start_video.setOnClickListener(new ClickEvent()); btn_stop_video.setOnClickListener(new ClickEvent()); skb_video=(SeekBar)this.findViewById(R.id.SeekBar02); skb_video.setOnSeekBarChangeListener(new SeekBarChangeEvent()); surfaceView = (SurfaceView) findViewById(R.id.SurfaceView01); surfaceHolder = surfaceView.getHolder(); surfaceHolder.setFixedSize(100, 100); surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); } /* * 按鍵事件處理 */ class ClickEvent implements View.OnClickListener{ @Override public void onClick(View v) { if(v==btn_start_audio) { m.reset();//恢復到未初始化的狀態 m=MediaPlayer.create(testMedia.this, R.raw.big);//讀取音頻 skb_audio.setMax(m.getDuration());//設置SeekBar的長度 try { m.prepare(); //准備 } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } m.start(); //播放 } else if(v==btn_stop_audio || v==btn_stop_video) { m.stop(); } else if(v==btn_start_video) { m.reset();//恢復到未初始化的狀態 m=MediaPlayer.create(testMedia.this, R.raw.test);//讀取視頻 skb_video.setMax(m.getDuration());//設置SeekBar的長度 m.setAudioStreamType(AudioManager.STREAM_MUSIC); m.setDisplay(surfaceHolder);//設置屏幕 try { m.prepare(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } m.start(); } } } /* * SeekBar進度改變事件 */ class SeekBarChangeEvent implements SeekBar.OnSeekBarChangeListener{ @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { // TODO Auto-generated method stub } @Override public void onStartTrackingTouch(SeekBar seekBar) { isChanging=true; } @Override public void onStopTrackingTouch(SeekBar seekBar) { m.seekTo(seekBar.getProgress()); isChanging=false; } } }
實現功能:實現NetMusicListAdapter(網絡音樂列表適配器)實現SearchResult(搜索音樂對象)使用Jsoup組件請求網絡,並解析音樂數據,並,音樂
Gesture在 ViewGroup中使用GestureDetector類可以讓我們快速的處理手勢事件,如點擊,滑動等。使用GestureDetector分三步:1. 定
方法一:Android的界面布局可以用兩種方法,一種是在xml中布局,一種是和JAVA中Swing一樣在JAVA代碼中實現Ui界面的布局,用xml的布局管理器布局是很方便
今天咱們看看android studio的jar打包,但是jar包裡面呢res文件是用不了的,那想用圖片文件怎麼辦呢,這裡可以把圖片文件放進assets文件裡面打進jar