編輯:Android開發實例
以前曾經地介紹過MediaPlayer的基本用法,這裡就深入地講解MediaPlayer的在線播放功能。本文主要實現MediaPlayer在線播放音頻的功能,由於在線視頻播放比在線音頻播放復雜,因此先介紹在線音頻播放的實現,這樣可以幫助大家逐步深入了解MediaPlayer的在線播放功能。先來看看本文程序運行的結果:
main.xml的源碼如下:
- <?xml version="1.0" encoding="utf-8"?>
- <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_height="fill_parent" android:layout_width="fill_parent">
- <LinearLayout android:layout_height="wrap_content"
- android:layout_width="fill_parent" android:orientation="vertical"
- android:layout_gravity="top">
- <LinearLayout android:orientation="horizontal"
- android:layout_gravity="center_horizontal" android:layout_marginTop="4.0dip"
- android:layout_height="wrap_content" android:layout_width="wrap_content">
- <Button android:layout_width="wrap_content"
- android:layout_height="wrap_content" android:id="@+id/btnPlayUrl"
- android:text="播放網絡音頻"></Button>
- <Button android:layout_height="wrap_content" android:id="@+id/btnPause"
- android:text="暫停" android:layout_width="80dip"></Button>
- <Button android:layout_height="wrap_content"
- android:layout_width="80dip" android:text="停止" android:id="@+id/btnStop"></Button>
- </LinearLayout>
- <LinearLayout android:orientation="horizontal"
- android:layout_width="fill_parent" android:layout_height="wrap_content"
- android:layout_marginBottom="20dip">
- <SeekBar android:paddingRight="10dip" android:layout_gravity="center_vertical"
- android:paddingLeft="10dip" android:layout_weight="1.0"
- android:layout_height="wrap_content" android:layout_width="wrap_content"
- android:id="@+id/skbProgress" android:max="100"></SeekBar>
- </LinearLayout>
- </LinearLayout>
- </FrameLayout>
Player.java是本文的核心,Player.java實現了“進度條更新”、“數據緩沖”等功能,雖然不是很復雜的功能,但卻是非常有用的功能。Player.java源碼如下:
- package com.musicplayer;
- import java.io.IOException;
- import java.util.Timer;
- import java.util.TimerTask;
- import android.media.AudioManager;
- import android.media.MediaPlayer;
- import android.media.MediaPlayer.OnBufferingUpdateListener;
- import android.media.MediaPlayer.OnCompletionListener;
- import android.os.Handler;
- import android.os.Message;
- import android.util.Log;
- import android.widget.SeekBar;
- public class Player implements OnBufferingUpdateListener,
- OnCompletionListener, MediaPlayer.OnPreparedListener{
- public MediaPlayer mediaPlayer;
- private SeekBar skbProgress;
- private Timer mTimer=new Timer();
- public Player(SeekBar skbProgress)
- {
- this.skbProgress=skbProgress;
- try {
- mediaPlayer = new MediaPlayer();
- mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
- mediaPlayer.setOnBufferingUpdateListener(this);
- mediaPlayer.setOnPreparedListener(this);
- } catch (Exception e) {
- Log.e("mediaPlayer", "error", e);
- }
- mTimer.schedule(mTimerTask, 0, 1000);
- }
- /*******************************************************
- * 通過定時器和Handler來更新進度條
- ******************************************************/
- TimerTask mTimerTask = new TimerTask() {
- @Override
- public void run() {
- if(mediaPlayer==null)
- return;
- if (mediaPlayer.isPlaying() && skbProgress.isPressed() == false) {
- handleProgress.sendEmptyMessage(0);
- }
- }
- };
- Handler handleProgress = new Handler() {
- public void handleMessage(Message msg) {
- int position = mediaPlayer.getCurrentPosition();
- int duration = mediaPlayer.getDuration();
- if (duration > 0) {
- long pos = skbProgress.getMax() * position / duration;
- skbProgress.setProgress((int) pos);
- }
- };
- };
- //*****************************************************
- public void play()
- {
- mediaPlayer.start();
- }
- public void playUrl(String videoUrl)
- {
- try {
- mediaPlayer.reset();
- mediaPlayer.setDataSource(videoUrl);
- mediaPlayer.prepare();//prepare之後自動播放
- //mediaPlayer.start();
- } 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();
- }
- }
- public void pause()
- {
- mediaPlayer.pause();
- }
- public void stop()
- {
- if (mediaPlayer != null) {
- mediaPlayer.stop();
- mediaPlayer.release();
- mediaPlayer = null;
- }
- }
- @Override
- /**
- * 通過onPrepared播放
- */
- public void onPrepared(MediaPlayer arg0) {
- arg0.start();
- Log.e("mediaPlayer", "onPrepared");
- }
- @Override
- public void onCompletion(MediaPlayer arg0) {
- Log.e("mediaPlayer", "onCompletion");
- }
- @Override
- public void onBufferingUpdate(MediaPlayer arg0, int bufferingProgress) {
- skbProgress.setSecondaryProgress(bufferingProgress);
- int currentProgress=skbProgress.getMax()*mediaPlayer.getCurrentPosition()/mediaPlayer.getDuration();
- Log.e(currentProgress+"% play", bufferingProgress + "% buffer");
- }
- }
test_musicplayer.java是主程序,負責調用Player類,其中關鍵部分是SeekBarChangeEvent這個SeekBar拖動的事件:SeekBar的Progress是0~SeekBar.getMax()之內的數,而MediaPlayer.seekTo()的參數是0~MediaPlayer.getDuration()之內數,所以MediaPlayer.seekTo()的參數是(progress/seekBar.getMax())*player.mediaPlayer.getDuration()。
test_musicplayer.java源碼如下:
- package com.musicplayer;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.SeekBar;
- public class test_musicplayer extends Activity {
- private Button btnPause, btnPlayUrl, btnStop;
- private SeekBar skbProgress;
- private Player player;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- this.setTitle("在線音樂播放---hellogv編寫");
- btnPlayUrl = (Button) this.findViewById(R.id.btnPlayUrl);
- btnPlayUrl.setOnClickListener(new ClickEvent());
- btnPause = (Button) this.findViewById(R.id.btnPause);
- btnPause.setOnClickListener(new ClickEvent());
- btnStop = (Button) this.findViewById(R.id.btnStop);
- btnStop.setOnClickListener(new ClickEvent());
- skbProgress = (SeekBar) this.findViewById(R.id.skbProgress);
- skbProgress.setOnSeekBarChangeListener(new SeekBarChangeEvent());
- player = new Player(skbProgress);
- }
- class ClickEvent implements OnClickListener {
- @Override
- public void onClick(View arg0) {
- if (arg0 == btnPause) {
- player.pause();
- } else if (arg0 == btnPlayUrl) {
- //在百度MP3裡隨便搜索到的,大家可以試試別的鏈接
- String url="http://219.138.125.22/myweb/mp3/CMP3/JH19.MP3";
- player.playUrl(url);
- } else if (arg0 == btnStop) {
- player.stop();
- }
- }
- }
- class SeekBarChangeEvent implements SeekBar.OnSeekBarChangeListener {
- int progress;
- @Override
- public void onProgressChanged(SeekBar seekBar, int progress,
- boolean fromUser) {
- // 原本是(progress/seekBar.getMax())*player.mediaPlayer.getDuration()
- this.progress = progress * player.mediaPlayer.getDuration()
- / seekBar.getMax();
- }
- @Override
- public void onStartTrackingTouch(SeekBar seekBar) {
- }
- @Override
- public void onStopTrackingTouch(SeekBar seekBar) {
- // seekTo()的參數是相對與影片時間的數字,而不是與seekBar.getMax()相對的數字
- player.mediaPlayer.seekTo(progress);
- }
- }
- }
簡介 在Android中運用了很多機制,例如:廣播、服務、數據庫、通知、包……等等。 什麼是廣播?Broadcast是一種廣泛運用的在應用
當我們第一次下載QQ並且打開的時候,會有一個新手引導,引導是幾張圖片,再加上一些文字說明,向右滑動,直到結束,今天一大早起來研究了一下關於此種效果的實現之View
篇幅較長遂分成上下兩篇,上一篇我們已經快要一氣呵成了,但是美中不足的是,這個界面並不能討得美工MM的歡心,美工MM曾寄希望於您,卻交出這麼作出這麼一副死型樣,我都
QuickContactsDemo示例介紹了如果使用Content Provider來訪問Android系統的Contacts 數據庫。 Cont