MediaPlayer和SeekBar配合起来
2016-03-25 18:27
471 查看
MediaPlayer和SeekBar配合在一起的例子。
SeekBar使用方法
Handler使用方法
预备
MediaPlayerSeekBar使用方法
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" > <LinearLayout android: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; --> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/start" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/stop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/stop" /> </LinearLayout> </LinearLayout> <SeekBar android: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() { @Override public void handleMessage(Message msg) { if (!isPlaying || msg.what != MONITOR_MSG_ID) { super.handleMessage(msg); return; } mediaSeekBar.setProgress(mediaPlayer.getCurrentPosition()); } }; @Override protected 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() { @Override public 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() { @Override public 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() { @Override public 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() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { if (isPlaying) { mediaPlayer.seekTo(seekBar.getProgress()); } } }); Log.d(TAG, "create ok"); } @Override public 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; } @Override public 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); } } } }
相关文章推荐
- 基于微信的域名交易系统(功能细节敲定)
- 浏览器书签管理
- Android实现通过浏览器点击链接打开本地应用(APP),并获取到传递的数据
- LVS NAT + Keepalived HOWTO
- Xcode 编译链接问题汇总(持续更新中....)
- DOT资源链接
- JavaCodeTra 猴子选猴王 约瑟夫循环
- 基于微信的域名交易系统(数据库结构设计)
- C++练习题目记录
- LC102 Binary Tree Level Order Traversal
- 《将博客搬至CSDN》
- 移动端前端UI库—Frozen UI、WeUI、SUI Mobile
- mac 访问局域网服务器地址
- JQuery倒计时
- Android开源项目集合
- python None与Null
- 基于微信的域名交易系统(事件流程图)
- 关于Davlik解释器的一些思考
- 通过MapReduce进行电影推荐Movie Recommendations and More via MapReduce and Scalding
- git初体验(三)git分支