简体   繁体   English

Android从原始或资产中在TextureView中播放视频

[英]Android play video in TextureView from raw or assets

I'm having an annoying problem with playing a video in TextureView from raw or assets folder... or even from the external storage. 我在Raw或Assets文件夹中甚至从外部存储中的TextureView中播放视频时遇到了一个烦人的问题。 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: 看来MediaPlayer无法从原始文件中找到文件:

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 并且我尝试使用此讨论中的一些代码将资产复制到sdcard: android-how-to-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. 我什至无法在设备中使用普通的VideoPlayer播放它。

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. 如果我将视频像USB一样放入我的设备并设置路径,则可以使用所有3个测试视频。 The manifest has read and write permission. 清单具有读写权限。 And the VideoView makes even more problems with the ViewPager and paths. 而且VideoView会使ViewPager和路径产生更多问题。

Hopefully I'm just making some kind of stupid mistake... Thank you! 希望我会犯一些愚蠢的错误……谢谢!

Only because you can play the video with the default video player of your device doesn't mean it's not a codec problem or another detail it doesn't like about it. 仅仅因为您可以使用设备的默认视频播放器播放视频,并不意味着这不是编解码器问题,也不是它不喜欢的其他细节。

What I' missing here is a MediaPlayer.OnErrorListener If you get something like /MediaPlayer﹕ error (1, -2147483648) , which indicates a MEDIA_ERROR_UNKNOWN with the extra info MEDIA_ERROR_SYSTEM (-2147483648) - low-level system error it's probably a video issue. 我”丢失这里是一个MediaPlayer.OnErrorListener如果你喜欢的东西/MediaPlayer﹕ error (1, -2147483648)这表明MEDIA_ERROR_UNKNOWN与额外的信息MEDIA_ERROR_SYSTEM (-2147483648) - low-level system error它可能是一个视频的问题。

You could take a look at the video metadata with ffmpeg: 您可以使用ffmpeg查看视频元数据:

ffmpeg -i video.mp4 ffmpeg -i video.mp4

Check for: 检查:

  • Codec 编解码器
  • Metadata (eg video is rotated) 元数据(例如,视频被旋转)
  • Video width/height too big 视频宽度/高度太大

Didn't get a reasonable error message for any of these. 没有收到关于这些的合理的错误消息。

Here's the official info: Supported Media Formats which doesn't help much IMHO 这是官方信息: 支持的媒体格式 ,对IMHO并没有多大帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM