您的位置:首页 > 其它

MediaPlayer和SeekBar配合起来

2016-03-25 18:27 471 查看
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" >

<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);
}
}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: