Reputation: 5028
Out there, there are a lot of examples about how to play video on Android (API level 16). But I can't understand why they don't work for me.
This is a simple example:
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="my.owm.package" >
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".ContentActivity"
android:label="@string/title_activity_content" >
</activity>
</application>
</manifest>
activity_content.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="my.owm.package.ContentActivity"
android:orientation="vertical">
<VideoView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/videoView"
android:layout_gravity="center_horizontal" />
</LinearLayout>
contentActivity.java
public class ContentActivity extends Activity {
private static final String TAG = ContentActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_content);
VideoView videoView =(VideoView) findViewById(R.id.videoView);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
videoView.setVideoPath("/sdcard/Movies/test.mp4");
videoView.setMediaController(mediaController);
videoView.requestFocus();
videoView.start();
}
}
The test.mp4 files is actually there and runs fine with any player (btw it's a H264 w/ Baseline Profile). Anyway i tried several other videos, most of them used by the authors of the tutorial on this topic.
I get no errors but no video and no audio. I'm pretty sure I'm missing something obvious because I found this code in a lot of Android tutorials!
Any idea? Thanks!
EDIT:
I run the code on a physical device. However I discovered the code is actually working until I add the following item in the layout xml file:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="300dp"
android:layout_height="match_parent"
android:layout_margin="0dp" />
Upvotes: 0
Views: 171
Reputation: 5028
SOLVED: the DrawerLayout should be added at the END of the layout... it seems the order is important!
Upvotes: 0
Reputation: 5149
I also have come across issues with VideoView
like this across multiple devices before.
Firstly I would consider re-encoding your videos using a tool such as ffmpeg
that can be downloaded here.
The following encoding has always worked for me:
ffmpeg -i input.mp4 -c:v libx264 -profile:v baseline -c:a libfaac -ar 44100 -ac 2 -b:a 128k -movflags faststart output.mp4
Alternatively, some devices cannot handle videos with 'large' dimensions. Try resizing your videos to a smaller size and seeing if that helps also.
Upvotes: 0
Reputation: 1857
I worked on this way :
public class AnVideoView extends Activity {
String SrcPath = "/sdcard/Video/Android in Spaaaaaace!_low.mp4";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
VideoView myVideoView = (VideoView)findViewById(R.id.myvideoview);
myVideoView.setVideoPath(SrcPath);
myVideoView.setMediaController(new MediaController(this));
myVideoView.requestFocus();
myVideoView.start();
}
}
Upvotes: 1