我使用的媒体播放器,流视频。它仅播放该音频而不是视频。谁能帮助吗?我的code是如下。
公共类VideoViewApplication扩展应用{
@覆盖
公共无效的onCreate(){
}
@覆盖
公共无效onTerminate(){
}
}
公共类VideoViewDemo扩展活动器具
OnErrorListener,OnBufferingUpdateListener,OnCompletionListener,
MediaPlayer.On preparedListener,SurfaceHolder.Callback {
私有静态最后字符串变量=VideoViewDemo;
私人MediaPlayer的熔点;
私人的EditText的mpath;
私人SurfaceHolder持有人;
私人的ImageButton mPlay;
私人的ImageButton mPause;
私人的ImageButton MRESET;
私人的ImageButton MSTOP;
私人字符串电流;
私人SurfaceView米preVIEW;
@覆盖
公共无效的onCreate(包冰柱){
super.onCreate(冰柱);
的setContentView(R.layout.main);
米preVIEW =(SurfaceView)findViewById(R.id.surface);
的mpath =(EditText上)findViewById(R.id.path);
mPath.setText(RTSP://video2.americafree.tv/AFTVHorrorH26496.sdp);
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(新OnClickListener(){
公共无效的onClick(视图查看){
的playVideo();
}
});
mPause.setOnClickListener(新OnClickListener(){
公共无效的onClick(视图查看){
如果(熔点!= NULL){
mp.pause();
}
}
});
mReset.setOnClickListener(新OnClickListener(){
公共无效的onClick(视图查看){
如果(熔点!= NULL){
mp.seekTo(0);
}
}
});
mStop.setOnClickListener(新OnClickListener(){
公共无效的onClick(视图查看){
如果(熔点!= NULL){
电流=无效;
mp.stop();
mp.release();
}
}
});
// getWindow()和setFormat(PixelFormat.TRANSPARENT);
持有人= M preview.getHolder();
holder.addCallback(本);
holder.setFixedSize(100,100);
runOnUiThread(新的Runnable(){
公共无效的run(){
的playVideo();
}
});
}
私人无效的playVideo(){
尝试 {
。最终的字符串路径= mPath.getText()的toString();
Log.v(TAG,路径为:+路径);
如果(path.equals(电流)及&安培;熔点=空!){
mp.start();
返回;
}
电流=路径;
熔点为新的MediaPlayer();
mp.setOnErrorListener(本);
mp.setOnBufferingUpdateListener(本);
mp.setOnCompletionListener(本);
mp.setOn preparedListener(本);
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.setScreenOnWhilePlaying(真正的);
mp.setDisplay(M preview.getHolder());
mp.setDataSource(路径);
MP prepare()。
Log.v(TAG,持续时间:===>+ mp.getDuration());
mp.start();
}赶上(例外五){
Log.e(TAG,错误:+ e.getMessage(),E);
如果(熔点!= NULL){
mp.stop();
mp.release();
}
}
}
公共无效surfaceChanged(SurfaceHolder为arg0,诠释ARG1,诠释ARG2,诠释ARG3){
Log.d(TAG,surfaceChanged名为);
}
公共无效surfaceCreated(SurfaceHolder为arg0){
Log.d(TAG,surfaceCreated名为);
}
公共无效surfaceDestroyed(SurfaceHolder为arg0){
Log.d(TAG,surfaceDestroyed名为);
}
公共无效于prepared(MediaPlayer的为arg0){
Log.d(TAG,在prepared名为);
}
公共无效onCompletion(MediaPlayer的为arg0){
Log.d(TAG,onCompletion名为);
}
公共无效onBufferingUpdate(MediaPlayer的媒体播放器,INT百分比){
Log.d(TAG,onBufferingUpdate叫--->百分比为:+%的);
}
公共布尔的onError(MediaPlayer的媒体播放器,诠释了什么,整型附加){
Log.e(TAG的onError - >什么:+什么+额外+另计);
如果(媒体播放器!= NULL){
mediaPlayer.stop();
mediaPlayer.release();
}
返回true;
}
}
< XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:方向=垂直
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
>
< EditText上机器人:ID =@ + ID /路径
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
/>
< SurfaceView
机器人:ID =@ + ID /面
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT>
< / SurfaceView>
<的LinearLayout
机器人:方向=横向
机器人:layout_height =WRAP_CONTENT
机器人:layout_width =FILL_PARENT
>
< ImageButton的机器人:ID =@ + ID /播放
机器人:layout_height =WRAP_CONTENT
机器人:layout_width =WRAP_CONTENT
机器人:SRC =@可绘制/播放/>
< ImageButton的机器人:ID =@ + ID /暂停
机器人:layout_height =WRAP_CONTENT
机器人:layout_width =WRAP_CONTENT
机器人:SRC =@可绘制/暂停/>
< ImageButton的机器人:ID =@ + ID /复位
机器人:layout_height =WRAP_CONTENT
机器人:layout_width =WRAP_CONTENT
机器人:SRC =@可绘制/复位/>
< ImageButton的机器人:ID =@ + ID /停止
机器人:layout_height =WRAP_CONTENT
机器人:layout_width =WRAP_CONTENT
机器人:SRC =@可绘制/停止/>
< / LinearLayout中>
< / LinearLayout中>
解决方案
我有这个问题,并利用该解preciated方法设置类型解决它。
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
值得一试,如果它的工作原理,你可以调查为什么类型没有被自动设置为它应该是。
I am using media player to stream a video. It only plays the audio but not the video. Could anyone help? My code is below.
public class VideoViewApplication extends Application {
@Override
public void onCreate() {
}
@Override
public void onTerminate() {
}
}
public class VideoViewDemo extends Activity implements
OnErrorListener,OnBufferingUpdateListener, OnCompletionListener,
MediaPlayer.OnPreparedListener, SurfaceHolder.Callback {
private static final String TAG = "VideoViewDemo";
private MediaPlayer mp;
private EditText mPath;
private SurfaceHolder holder;
private ImageButton mPlay;
private ImageButton mPause;
private ImageButton mReset;
private ImageButton mStop;
private String current;
private SurfaceView mPreview;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
mPreview = (SurfaceView) findViewById(R.id.surface);
mPath = (EditText) findViewById(R.id.path);
mPath.setText("rtsp://video2.americafree.tv/AFTVHorrorH26496.sdp");
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 (mp != null) {
mp.pause();
}
}
});
mReset.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
if (mp != null) {
mp.seekTo(0);
}
}
});
mStop.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
if (mp != null) {
current = null;
mp.stop();
mp.release();
}
}
});
// getWindow().setFormat(PixelFormat.TRANSPARENT);
holder = mPreview.getHolder();
holder.addCallback(this);
holder.setFixedSize(100, 100);
runOnUiThread(new Runnable(){
public void run(){
playVideo();
}
});
}
private void playVideo() {
try {
final String path = mPath.getText().toString();
Log.v(TAG, "path: " + path);
if (path.equals(current) && mp != null) {
mp.start();
return;
}
current = path;
mp = new MediaPlayer();
mp.setOnErrorListener(this);
mp.setOnBufferingUpdateListener(this);
mp.setOnCompletionListener(this);
mp.setOnPreparedListener(this);
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.setScreenOnWhilePlaying(true);
mp.setDisplay(mPreview.getHolder());
mp.setDataSource(path);
mp.prepare();
Log.v(TAG, "Duration: ===>" + mp.getDuration());
mp.start();
} catch (Exception e) {
Log.e(TAG, "error: "+ e.getMessage(), e);
if (mp != null) {
mp.stop();
mp.release();
}
}
}
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
Log.d(TAG, "surfaceChanged called");
}
public void surfaceCreated(SurfaceHolder arg0) {
Log.d(TAG, "surfaceCreated called");
}
public void surfaceDestroyed(SurfaceHolder arg0) {
Log.d(TAG, "surfaceDestroyed called");
}
public void onPrepared(MediaPlayer arg0) {
Log.d(TAG, "onPrepared called");
}
public void onCompletion(MediaPlayer arg0) {
Log.d(TAG, "onCompletion called");
}
public void onBufferingUpdate(MediaPlayer mediaPlayer, int percent) {
Log.d(TAG, "onBufferingUpdate called ---> percent:" + percent);
}
public boolean onError(MediaPlayer mediaPlayer, int what, int extra) {
Log.e(TAG, "onError---> what:"+what+" extra:"+extra);
if (mediaPlayer != null) {
mediaPlayer.stop();
mediaPlayer.release();
}
return true;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<EditText android:id="@+id/path"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<SurfaceView
android:id="@+id/surface"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</SurfaceView>
<LinearLayout
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
>
<ImageButton android:id="@+id/play"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/play"/>
<ImageButton android:id="@+id/pause"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/pause"/>
<ImageButton android:id="@+id/reset"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/reset"/>
<ImageButton android:id="@+id/stop"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/stop"/>
</LinearLayout>
</LinearLayout>
解决方案
I had this problem and solved it by setting the type using this depreciated method.
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
Worth a try, and if it works you could investigate why the type isn't automatically set as it is supposed to be.