I have tried several different examples but I cannot get any video to show. I hear sound but no video. I thought maybe I just had a incorrect video format so I downloaded a fiat commercial in 3gp format and still no joy. I am using the Eclipse Java EE with android sdk and my app targets the 1.5 sdk (Api Level 3) without google api. Could someone please post a link to a video known to play in android or point out my problem with the code. I have tried all I could think of .. with/without prepare .. different layouts etc.
Here is my onCreate in 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();
}
her is the layout in 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>
> Blockquote
You class must implement SurfaceHolder.Callback and call methods setDisplay, prepare and etc only after you get in surfaceCreated. Also you may need to change type of surface holder to SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS. Like this:
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) {
}
}
// ...
}
If you try it in OnCreate you got nothing as result, because Surface is not yet created...
If you are using the emulator, it may not work. The emulator lacks the hardware acceleration available in Android devices. For example, on a 2.6GHz Core 2 Duo, I sometimes can get a video to play back, but not always. On slower machines, video playback never works. On a 2.5GHz Core 2 Quad, I always get video playback. That being said, I usually test video playback on actual devices.
I am also uncertain if video playback works from raw resources. I strongly encourage you to try using a video from a file on the SD card first.
With respect to videos that definitely work, I know that "Documentaries and You" and "Music for our Grandchildren" from here work in their MP4 forms.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With