Reputation: 2837
After lots of research i found VideoView is not able to play all video formats due to codec,frame size,etc. So i found alternative of using Exoplayer Library in my project. Is it possible that i can run all type of videos using exoplayer. By implementing small part i am getting following error. I have already added exoplayer library. i have not added anythigng to UI.
09-04 14:46:57.631: D/AndroidRuntime(25845): Shutting down VM
09-04 14:46:57.631: W/dalvikvm(25845): threadid=1: thread exiting with uncaught exception (group=0x416b4d40)
09-04 14:46:57.633: E/AndroidRuntime(25845): FATAL EXCEPTION: main
09-04 14:46:57.633: E/AndroidRuntime(25845): Process: com.example.myexo, PID: 25845
09-04 14:46:57.633: E/AndroidRuntime(25845): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myexo/com.example.myexo.MainActivity}: java.lang.IllegalStateException
09-04 14:46:57.633: E/AndroidRuntime(25845): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2198)
09-04 14:46:57.633: E/AndroidRuntime(25845): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
09-04 14:46:57.633: E/AndroidRuntime(25845): at android.app.ActivityThread.access$800(ActivityThread.java:139)
09-04 14:46:57.633: E/AndroidRuntime(25845): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
09-04 14:46:57.633: E/AndroidRuntime(25845): at android.os.Handler.dispatchMessage(Handler.java:102)
09-04 14:46:57.633: E/AndroidRuntime(25845): at android.os.Looper.loop(Looper.java:136)
09-04 14:46:57.633: E/AndroidRuntime(25845): at android.app.ActivityThread.main(ActivityThread.java:5086)
09-04 14:46:57.633: E/AndroidRuntime(25845): at java.lang.reflect.Method.invokeNative(Native Method)
09-04 14:46:57.633: E/AndroidRuntime(25845): at java.lang.reflect.Method.invoke(Method.java:515)
09-04 14:46:57.633: E/AndroidRuntime(25845): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
09-04 14:46:57.633: E/AndroidRuntime(25845): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
09-04 14:46:57.633: E/AndroidRuntime(25845): at dalvik.system.NativeStart.main(Native Method)
09-04 14:46:57.633: E/AndroidRuntime(25845): Caused by: java.lang.IllegalStateException
09-04 14:46:57.633: E/AndroidRuntime(25845): at com.google.android.exoplayer.util.Assertions.checkState(Assertions.java:66)
09-04 14:46:57.633: E/AndroidRuntime(25845): at com.google.android.exoplayer.MediaCodecAudioTrackRenderer.<init>(MediaCodecAudioTrackRenderer.java:225)
09-04 14:46:57.633: E/AndroidRuntime(25845): at com.example.myexo.MainActivity.onCreate(MainActivity.java:32)
09-04 14:46:57.633: E/AndroidRuntime(25845): at android.app.Activity.performCreate(Activity.java:5248)
09-04 14:46:57.633: E/AndroidRuntime(25845): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
09-04 14:46:57.633: E/AndroidRuntime(25845): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
09-04 14:46:57.633: E/AndroidRuntime(25845): ... 11 more
09-04 14:46:59.456: I/Process(25845): Sending signal. PID: 25845 SIG: 9
MainActivity.java
package com.example.myexo;
import com.google.android.exoplayer.ExoPlayer;
import com.google.android.exoplayer.FrameworkSampleSource;
import com.google.android.exoplayer.MediaCodecAudioTrackRenderer;
import com.google.android.exoplayer.MediaCodecVideoTrackRenderer;
import com.google.android.exoplayer.SampleSource;
import com.google.android.exoplayer.TrackRenderer;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
public class MainActivity extends Activity {
Context context;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int numRenderers = 2;
Uri uri=Uri.parse("https://ia600505.us.archive.org/3/items/Windows7WildlifeSampleVideo/Wildlife_512kb.mp4");
SampleSource samplesource = new FrameworkSampleSource(context,uri,null, numRenderers);
TrackRenderer videorenderer = new MediaCodecVideoTrackRenderer(samplesource, 0);
TrackRenderer audiorenderer = new MediaCodecAudioTrackRenderer(samplesource, null, false, 0, null, null);
ExoPlayer exoplayer = ExoPlayer.Factory.newInstance(numRenderers);
exoplayer.prepare(videorenderer,audiorenderer);
exoplayer.setPlayWhenReady(true);
}
}
Upvotes: 0
Views: 1415
Reputation: 61
Try using the single parameter constructor of the MediaCodecAudioTrackRenderer.
TrackRenderer audiorenderer = new MediaCodecAudioTrackRenderer(samplesource)
Also, you're forgetting to pass the surface to the video renderer. Look at step 4 of the main steps that the developer guide of Android gives you.
// 1. Instantiate the player.
player = ExoPlayer.Factory.newInstance(RENDERER_COUNT);
// 2. Construct renderers.
MediaCodecVideoTrackRenderer videoRenderer = …
MediaCodecAudioTrackRenderer audioRenderer = ...
// 3. Inject the renderers through prepare.
player.prepare(videoRenderer, audioRenderer);
// 4. Pass the surface to the video renderer.
player.sendMessage(videoRenderer, MediaCodecVideoTrackRenderer.MSG_SET_SURFACE,
surface);
// 5. Start playback.
player.setPlayWhenReady(true);
(Source: http://developer.android.com/guide/topics/media/exoplayer.html)
I would also recommend to instantiate the player in the onResume() or the surfaceCreated(). The onCreate() is used for the setup of the global state of an Activity. (Source: http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle)
Upvotes: 1