I'm having an annoying problem with playing a video in TextureView from raw or assets folder... or even from the external storage. The code in the fragment looks something like this
public class MainFragment extends Fragment implements TextureView.SurfaceTextureListener, MediaPlayer.OnBufferingUpdateListener,
MediaPlayer.OnCompletionListener, MediaPlayer.OnPreparedListener, MediaPlayer.OnVideoSizeChangedListener {
private ParallaxAdapter mAdapter;
private MediaPlayer mMediaPlayer;
private MediaPlayer mMediaPlayer2;
private TextureView tv;
private TextureView tv2;
private ImageView imageview;
private TextureView mTexture;
String path;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstance) {
super.onCreateView(inflater, container, savedInstance);
View v = inflater.inflate(R.layout.view_fragment, container, false);
path = getArguments().getString("path");
Log.d("Fragment","" + path);
String tag = getArguments().getString("Tag");
imageview = (ImageView) v.findViewById(R.id.imageView);
if (tag == "image") {
imageview.setImageResource(getArguments().getInt("image"));
} else if (tag == "video") {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inDither = false;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmapThumb = ThumbnailUtils.createVideoThumbnail(path, MediaStore.Images.Thumbnails.FULL_SCREEN_KIND);
imageview.setImageBitmap(bitmapThumb);
tv = (TextureView) v.findViewById(R.id.textureView);
tv.setSurfaceTextureListener(this);
}
return v;
}
public void setAdapter(ParallaxAdapter Adapter) {
mAdapter = Adapter;
}
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
Surface s = new Surface(surface);
try {
MediaPlayer mp = new MediaPlayer();
mp.setDataSource(path);
mp.setSurface(s);
mp.prepare();
mp.setOnBufferingUpdateListener(this);
mp.setOnCompletionListener(this);
mp.setOnPreparedListener(this);
mp.setOnVideoSizeChangedListener(this);
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.setVolume(0, 0);
mMediaPlayer.setLooping(true);
mMediaPlayer.start();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
It looks like the MediaPlayer cannot find the file from raw:
defpath = "android.resource://" + getPackageName() + "/" + R.raw.video;
or from assets:
AssetFileDescriptor afd = getAssets().openFd(FILE_NAME);
mp.setDataSource(afd.getFileDescriptor());
and i've tried to copy the assets to the sdcard with some codes from this discussion: android-how-to-copy-files-from-assets-folder-to-sdcard
but if I copy it, the file is somehow corrupt. I cannot even play it with the normal VideoPlayer in my device.
It is not a codec error! If I put the video into my device like on a USB and set the path and it's working with all 3 test videos. The manifest has read and write permission. And the VideoView makes even more problems with the ViewPager and paths.
Hopefully I'm just making some kind of stupid mistake... Thank you!