如何解决Android音频播放器出现问题:搜寻栏无法转到位置设置
| 我想使用seekbar将视频搜索到垂直位置 我已经尝试过了,但是seekbar不能正常工作,是说我改变了位置,但是视频却没有找到该位置。我做错了什么? 我的代码如下:package android.com.mp3play;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.URLUtil;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.Toast;
import android.widget.VideoView;
import android.media.MediaPlayer;
import android.os.Environment;
import android.os.Handler;
import android.media.MediaPlayer.OnPreparedListener;
import android.view.MotionEvent;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.MediaController.MediaPlayerControl;
import android.widget.SeekBar.OnSeekBarChangeListener;
public class mp3playActivity extends Activity{
private static final String TAG = \"VideoViewDemo\";
private VideoView mVideoView;
private EditText mPath;
private ImageButton mPlay;
private ImageButton mPause;
private ImageButton mReset;
private ImageButton mStop;
private String current;
private SeekBar sb;
//implements OnPreparedListener,MediaPlayerControl {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sb=(SeekBar) findViewById(R.id.seekBar1);
mVideoView = (VideoView) findViewById(R.id.surface_view);
mPath = (EditText) findViewById(R.id.path);
mPath.setText(\"http://daily3gp.com/vids/747.3gp\");
mPlay = (ImageButton) findViewById(R.id.play);
mPause = (ImageButton) findViewById(R.id.pause);
mReset = (ImageButton) findViewById(R.id.reset);
mStop = (ImageButton) findViewById(R.id.stop);
mPlay.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
playVideo();
}
});
mPause.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
if (mVideoView != null) {
mVideoView.pause();
}
}
});
mReset.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
if (mVideoView != null) {
mVideoView.seekTo(0);
}
}
});
mStop.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
if (mVideoView != null) {
current = null;
mVideoView.stopPlayback();
}
}
});
runOnUiThread(new Runnable(){
public void run() {
playVideo();
}
});
sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}
@Override
public void onStartTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}
@Override
public void onProgressChanged(SeekBar arg0,int arg1,boolean arg2) {
// TODO Auto-generated method stub
arg1=mVideoView.getCurrentPosition();
mVideoView.seekTo(arg1);
}
});
}
private void playVideo() {
try {
final String path = mPath.getText().toString();
Log.v(TAG,\"path: \" + path);
if (path == null || path.length() == 0) {
Toast.makeText(mp3playActivity.this,\"File URL/path is empty\",Toast.LENGTH_LONG).show();
} else {
// If the path has not changed,just start the media player
if (path.equals(current) && mVideoView != null) {
mVideoView.start();
mVideoView.requestFocus();
return;
}
current = path;
mVideoView.setVideoPath(getDataSource(path));
mVideoView.start();
mVideoView.requestFocus();
}
} catch (Exception e) {
Log.e(TAG,\"error: \" + e.getMessage(),e);
if (mVideoView != null) {
mVideoView.stopPlayback();
}
}
}
private String getDataSource(String path) throws IOException {
if (!URLUtil.isNetworkUrl(path)) {
return path;
} else {
URL url = new URL(path);
URLConnection cn = url.openConnection();
cn.connect();
InputStream stream = cn.getInputStream();
if (stream == null)
throw new RuntimeException(\"stream is null\");
File temp = File.createTempFile(\"mediaplayertmp\",\"dat\");
temp.deleteOnExit();
String tempPath = temp.getAbsolutePath();
FileOutputStream out = new FileOutputStream(temp);
byte buf[] = new byte[128];
do {
int numread = stream.read(buf);
if (numread <= 0)
break;
out.write(buf,numread);
} while (true);
try {
stream.close();
} catch (IOException ex) {
Log.e(TAG,\"error: \" + ex.getMessage(),ex);
}
return tempPath;
}
}
}
解决方法
通过理解您的代码,您实际上并没有寻求其他位置,而是停留在当前的演奏位置。
mVideoView.getCurrentPosition()+5;
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。