本文主要是介绍MediaPlayer和SeekBar配合起来,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
MediaPlayer和SeekBar配合在一起的例子。
预备
- MediaPlayer
- SeekBar使用方法
- Handler使用方法
例子
my_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"><!-- Use LinearLayout to seperate the two buttons. So clear the warning:Buttons in button bars should be borderless; --><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="match_parent"android:orientation="vertical" ><Buttonandroid:id="@+id/start"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/start" /></LinearLayout><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="match_parent"android:orientation="vertical" ><Buttonandroid:id="@+id/stop"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/stop" /></LinearLayout></LinearLayout><SeekBarandroid:id="@+id/mediaSeekBar"android:layout_width="match_parent"android:layout_height="wrap_content" /></LinearLayout>
代码
package com.example.mediaplayerexample;import java.io.IOException;import android.app.Activity;
import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;public class MainActivity extends Activity {private final String TAG = "MainActivity";private MediaPlayer mediaPlayer = null;private Button start = null;private Button stop = null;private SeekBar mediaSeekBar = null;boolean isPlaying = false;private final int MONITOR_MSG_ID = 0;private Thread monitor = null;private Handler handler = new Handler() {@Overridepublic void handleMessage(Message msg) {if (!isPlaying || msg.what != MONITOR_MSG_ID) {super.handleMessage(msg);return;}mediaSeekBar.setProgress(mediaPlayer.getCurrentPosition());}};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.my_layout);start = (Button) findViewById(R.id.start);stop = (Button) findViewById(R.id.stop);stop.setEnabled(false);start.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {try {mediaPlayer.prepare();} catch (IllegalStateException e) {Log.d(TAG, "start click error", e);} catch (IOException e) {Log.d(TAG, "start click error", e);}isPlaying = true;mediaPlayer.start();mediaSeekBar.setMax(mediaPlayer.getDuration());start.setEnabled(false);stop.setEnabled(true);monitor = new MonitorThread(1000);monitor.start();}});stop.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {isPlaying = false;mediaPlayer.stop();start.setEnabled(true);stop.setEnabled(false);try {monitor.join();} catch (InterruptedException e) {Log.d(TAG, "stop click error", e);}}});Log.d(TAG, "new MediaPlyer()");mediaPlayer = new MediaPlayer();mediaPlayer.setOnCompletionListener(new OnCompletionListener() {@Overridepublic void onCompletion(MediaPlayer mp) {isPlaying = false;start.setEnabled(true);stop.setEnabled(false);try {monitor.join();} catch (InterruptedException e) {Log.e(TAG, "start thread failed.", e);}}});Log.d(TAG, "Set data source");AssetFileDescriptor fd = null;try {fd = getResources().openRawResourceFd(R.raw.test);mediaPlayer.setDataSource(fd.getFileDescriptor(), fd.getStartOffset(), fd.getLength());fd.close();Log.d(TAG, "set data source ok");} catch (IOException e) {Log.e(TAG, "set data source failed.", e);finish();}mediaSeekBar = (SeekBar) findViewById(R.id.mediaSeekBar);mediaSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {@Overridepublic void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {}@Overridepublic void onStartTrackingTouch(SeekBar seekBar) {}@Overridepublic void onStopTrackingTouch(SeekBar seekBar) {if (isPlaying) {mediaPlayer.seekTo(seekBar.getProgress());}}});Log.d(TAG, "create ok");}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {// Handle action bar item clicks here. The action bar will// automatically handle clicks on the Home/Up button, so long// as you specify a parent activity in AndroidManifest.xml.int id = item.getItemId();if (id == R.id.action_settings) {return true;}return super.onOptionsItemSelected(item);}private class MonitorThread extends Thread{private int interval;public MonitorThread(int interval) {this.interval = interval;}public void run(){Log.d(TAG, "MonitorThread::run()");while(true) {if (!isPlaying) return;try {sleep(interval);} catch (InterruptedException e) {e.printStackTrace();}handler.sendEmptyMessage(MONITOR_MSG_ID);}}}
}
这篇关于MediaPlayer和SeekBar配合起来的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!