我在媒体播放器中播放视频时遇到问题。我有声但没有视频。我尝试了一切,找不到解决方案。或许你知道如何在videoview中播放sdp文件?这是代码:
public class TestStream1 extends Activity implements Callback{
MediaPlayer mMediaPlayer;
String SrcPath = "rtsp://184.72.239.149/vod/mp4:BigBuckBunny_175k.mov";
private SurfaceView mPreview;
private SurfaceHolder holder;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mPreview = (SurfaceView) findViewById(R.id.surface);
holder = mPreview.getHolder();
holder.addCallback(TestStream1.this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
holder.setFixedSize(400,300);
mMediaPlayer = new MediaPlayer();
try{
mMediaPlayer.setDataSource(SrcPath);
mMediaPlayer.setDisplay(holder);
mMediaPlayer.prepare();
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.start();
}catch(Exception e){
Toast .makeText( TestStream1.this, "Fail", Toast.LENGTH_LONG).show();
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
}
编辑:布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<LinearLayout
android:orientation="vertical"
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" />
</LinearLayout>
</LinearLayout>
答案 0 :(得分:2)
某些视频无法在媒体播放器中播放,如果它没有针对其a / v的编解码器,或者它太复杂或交错不当(请查看Logcat中有关某些内容的迹象)。可能是一个或多个原因。要确保媒体播放器可以播放您要求播放的视频类型,请将相同a / v编解码器和扩展名的视频文件保存到SD卡中,然后尝试使用默认媒体播放器播放。 Meanwile,尝试使用 videoView 检查视频是否可以播放。您的代码似乎没问题。
试试这段代码:
打包a.b;
public class TestStream1 extends Activity implements Callback{
MediaPlayer mMediaPlayer;
String SrcPath = "rtsp://184.72.239.149/vod/mp4:BigBuckBunny_175k.mov";
private SurfaceView mPreview;
private SurfaceHolder holder;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mPreview = (SurfaceView) findViewById(R.id.surface);
holder = mPreview.getHolder();
holder.addCallback(TestStream1.this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
holder.setFixedSize(400,300);
mMediaPlayer = new MediaPlayer();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
try{
mMediaPlayer.setDisplay(holder);
mMediaPlayer.setDataSource(SrcPath);
mMediaPlayer.prepare();
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.start();
}catch(Exception e){
Toast .makeText( TestStream1.this, "Fail", Toast.LENGTH_LONG).show();
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
mMediaPlayer.release();
}
}
SurfaceHolders是异步创建的,因此我们必须等到表面已经创建,然后分配返回的表面持有者对象。 我使用以下布局:
<?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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<SurfaceView
android:id="@+id/surface"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
上述解决方案应该有效。
答案 1 :(得分:1)
请注意,但请尝试设置数据源,如:
mMediaPlayer.setDataSource(this, SrcPath);