对于遇到类似问题的人,以下是我们的解决方法。
我们仍然不确定为什么在应用中播放嵌入式视频无法正常工作。我们尝试了 res/raw、 assets 和将视频复制到内部存储和 SD 卡中。
由于它能够从 HTTP 服务器播放视频,最终我们嵌入了一个轻量级的 HTTP 服务器到应用中,直接从资产目录中提供文件。这里我使用nanohttpd作为嵌入式服务器。
为了使其正常工作,我只需要将视频放置在资产文件夹中。例如:assets/animation/animation1.mp4。在引用该文件时,您将路径作为“animation/animation1.mp4”传递,服务器将从资产目录中提供该文件。
应用程序类启动 HTTP 服务器并将其注册为服务。
public class MyApplication extends Application {
private NanoServer mNanoServer;
@Override
public void onCreate() {
super.onCreate();
mNanoServer = new NanoServer(0, getApplicationContext());
try {
mNanoServer.start();
} catch (IOException e) {
Log.e("Unable to start embeded video file server. Animations will be disabled.",
"MyApplication", e);
}
}
@Override
public void onTerminate() {
mNanoServer.stop();
super.onTerminate();
}
@Override
public Object getSystemService(String name) {
if (name.equals(NanoServer.SYSTEM_SERVICE_NAME)) {
return mNanoServer;
}
return super.getSystemService(name);
}
}
NanoServer 类
public class NanoServer extends NanoHTTPD {
public static final String SYSTEM_SERVICE_NAME = "NANO_EMBEDDED_SYSTEM_HTTP_SERVER";
private Context mContext;
public NanoServer(int port, Context context) {
super(port);
this.mContext = context;
}
public NanoServer(String hostname, int port) {
super(hostname, port);
}
@Override
public Response serve(IHTTPSession session) {
try {
Uri uri = Uri.parse(session.getUri());
String fileToServe = normalizePath(uri.getPath());
return new Response(Status.OK, "video/mp4", (InputStream) mContext.getAssets().open(fileToServe));
} catch (IOException e) {
return new Response(Status.INTERNAL_ERROR, "", "");
}
}
private String normalizePath(String path) {
return path.replaceAll("^/+", "");
}
public String getUrlFor(String filePath) {
return String.format(Locale.ENGLISH, "http://localhost:%d/%s", getListeningPort(), filePath);
}
}
在 Fragment 中使用
if (fileToPlay != null) {
mTextureView = (TextureView) inflatedView.findViewById(R.id.backgroundAnimation);
mTextureView.setSurfaceTextureListener(new VideoTextureListener());
}
...
private final class VideoTextureListener implements SurfaceTextureListener {
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
mMediaPlayer.release();
mMediaPlayer = null;
return true;
}
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
Surface s = new Surface(surface);
try {
NanoServer server =
(NanoServer) getActivity().getApplication().getSystemService(NanoServer.SYSTEM_SERVICE_NAME);
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setSurface(s);
mMediaPlayer.setDataSource(server.getUrlFor(mHotspotTemplate.animation));
mMediaPlayer.prepareAsync();
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setVolume(0, 0);
mMediaPlayer.setOnErrorListener(new OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
mMediaPlayer.release();
mTextureView.setVisibility(View.INVISIBLE);
return true;
}
});
mMediaPlayer.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mMediaPlayer.start();
}
});
mMediaPlayer.setLooping(true);
mMediaPlayer.setVolume(0, 0);
} catch (Throwable e) {
if (mMediaPlayer != null) {
mMediaPlayer.release();
}
mTextureView.setVisibility(View.INVISIBLE);
}
}
}