我尝试了几个不同的示例,但无法显示任何视频。我听到声音但没有视频。我想也许我的视频格式不正确,所以我下载了一个 3gp 格式的法定商业广告,但仍然没有任何乐趣。我正在使用带有 android sdk 的 Eclipse Java EE,我的应用程序针对没有 google api 的 1.5 sdk(Api Level 3)。有人可以发布一个已知在 android 中播放的视频的链接,或者指出我的代码问题。我已经尝试了所有我能想到的..有/没有准备..不同的布局等。
这是我的 onCreate Activity
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.setContentView(R.layout.video_test);
SurfaceView v = (SurfaceView) findViewById(R.id.surface_video);
SurfaceHolder holder = v.getHolder();
// Set the transparency
//getWindow().setFormat(PixelFormat.UNKNOWN);
// Set a size for the video screen
//holder.addCallback(this);
holder.setFixedSize(400,300);
MediaPlayer mp = MediaPlayer.create(this, R.raw.fiat);
mp.setDisplay(holder);
//mp.setAudioStreamType(2);
try {
//mp.prepare();
mp.start();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
她是video_test.xml中的布局
<?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"
>
<SurfaceView android:id="@+id/surface_video"
android:layout_width="250px"
android:layout_height="250px">
</SurfaceView>
<LinearLayout
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:padding="10dip"
>
</LinearLayout>
</LinearLayout>
> block 引用
最佳答案
您的类必须实现 SurfaceHolder.Callback 并仅在进入 surfaceCreated 后调用方法 setDisplay、prepare 等。此外,您可能需要将表面支架的类型更改为 SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS。像这样:
import android.view.SurfaceHolder.Callback;
public class TestActivity extends Activity implements Callback {
// ...
@Override
public void onCreate(Bundle savedInstanceState) {
// ...
mSurfaceView = (SurfaceView)findViewById(R.id.yousurfaceview);
holder = mSurfaceView.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
mp.setDisplay(holder);
mp.setDataSource(somesource);
mp.prepare();
mp.start();
// etc...
} catch (IOException e) {
} catch (IllegalArgumentException e) {
} catch (IllegalStateException e) {
}
}
// ...
}
如果您在 OnCreate 中尝试它,您什么也得不到,因为 Surface 尚未创建...
关于android视频,听到声音但没有视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2184364/