对于任何面临类似问题的人来说,这对我们有用。
我们仍然不确定为什么无法使用该应用播放嵌入式视频。我们尝试了 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)) {
// TODO Maybe we should check if the server is alive and create a new one if it is not.
// How often can the server crash?
return mNanoServer;
}
return super.getSystemService(name);
}
}
NanoServer 类
/*
* TODO Document this.
*/
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);
}
}
在片段中使用它
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);
}
}
}