我正在尝试使用MediaPlayer
运行Android的runOnUiThread
。 setDataSource
我没有发现任何异常。但在那之后,MediaPlayer
没有任何反应。它应该在表面改变时给出回调onPrepared
。
似乎MediaPlayer
不支持这种方式。
如果是真的,有没有解决方法?
我需要这种逻辑,因为我需要获取被阻止的网络查询的信息。我需要从那里运行onSuccess
。
你对此有何建议?非常感谢!
onResume()
{
getInfo(xxx);
}
void getInfo(url, new DataListener() {
@Override
public void onDataSuccess(xxx) {
playVideoOnSuccess(xxx);
}
}
public void playVideoOnSuccess(xxx)
{
myBaseActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
try {
mPlayerListener = new VideoPlayerListener(null, content);
// create new mediaplayer
mVideoPlayer = VideoPlayer.getInstance();
mVideoPlayer.setVideoPlayerListener(mPlayerListener);
// setDataSource
mVideoPlayer.consumeContent(content);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
答案 0 :(得分:1)
以下是我用来播放视频的示例代码,希望这可能会以某种方式帮助您
public class VideoViewDemo extends Activity {
private static final String TAG = "VideoViewDemo";
private String current;
/**
* TODO: Set the path variable to a streaming video URL or a local media
* file path.
*/
private String path = "http://www.boisestatefootball.com/sites/default/files/videos/original/01%20-%20coach%20pete%20bio_4.mp4";
private VideoView mVideoView;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.videoview);
mVideoView = (VideoView) findViewById(R.id.surface_view);
runOnUiThread(new Runnable() {
public void run() {
playVideo();
}
});
}
private void playVideo() {
try {
// final String path = path;
Log.v(TAG, "path: " + path);
if (path == null || path.length() == 0) {
Toast.makeText(VideoViewDemo.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.setMediaController(new MediaController(this));
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, 0, numread);
} while (true);
try {
stream.close();
} catch (IOException ex) {
Log.e(TAG, "error: " + ex.getMessage(), ex);
}
return tempPath;
}
}
}
这项工作对我来说