gpt4 book ai didi

android - 将视频和图像从本地存储加载到 Google ChromeCast android

转载 作者:太空狗 更新时间:2023-10-29 14:49:20 26 4
gpt4 key购买 nike

如何将本地视频 URI/路径/文件设置为 chromecast 的 MediaInfo.Builder()?另外,任何人都可以帮我提供一个代码 fragment ,说明如何从 android 存储本地加载图像和视频并将其转换到 google chrome cast?也阅读其他问题,但在那里已经提到在应用程序上创建本地服务器。如果我们真的需要这样做,那么如何实现呢?我正在使用 https://github.com/googlecast/CastVideos-android 上可用的代码但是那边的视频是从 url 获取和播放的。我想从本地存储加载视频我也想显示图像。

更新:现在我已经使用“NanoHttpD”创建了一个本地服务器,但无法获取所选本地文件的 url。下面是我的 Activity 代码 fragment :

public class VideoBrowserFragment extends Fragment implements VideoListAdapter.ItemClickListener,
LoaderManager.LoaderCallbacks<List<MediaInfo>> {

private static final String TAG = "zz VideoBrowserFragment";
private static final int REQUEST_TAKE_GALLERY_VIDEO = 123;
private static final String CATALOG_URL =
"http://commondatastorage.googleapis.com/gtv-videos-bucket/CastVideos/f.json";
private RecyclerView mRecyclerView;
private VideoListAdapter mAdapter;
private View mEmptyView;
private View mLoadingView;
private VideoCastManager mCastManager;
private VideoCastConsumerImpl mCastConsumer;
private View btnLocalVideo;
LocalServer server;
String chosenExt = "";
String localFileName = "";
String localFilePath = "";
String formatedIpAddress = "";
Uri selectedImageUri;

public VideoBrowserFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
Toast.makeText(getActivity(), "VideoBrowserFragment onCreateView", Toast.LENGTH_SHORT).show();
Log.d(TAG, "VideoBrowserFragment onCreateView");
return inflater.inflate(R.layout.video_browser_fragment, container, false);
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);

Toast.makeText(getActivity(), "VideoBrowserFragment onViewCreated", Toast.LENGTH_SHORT).show();
Log.d(TAG, "VideoBrowserFragment onViewCreated");

mRecyclerView = (RecyclerView) getView().findViewById(R.id.list);
mEmptyView = getView().findViewById(R.id.empty_view);
mLoadingView = getView().findViewById(R.id.progress_indicator);
btnLocalVideo = getView().findViewById(R.id.btnLocalVideo);
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(layoutManager);
mAdapter = new VideoListAdapter(this);
mRecyclerView.setAdapter(mAdapter);
getLoaderManager().initLoader(0, null, this);
mCastManager = VideoCastManager.getInstance();
mCastConsumer = new VideoCastConsumerImpl() {
@Override
public void onConnected() {
super.onConnected();
mAdapter.notifyDataSetChanged();
}

@Override
public void onDisconnected() {
super.onDisconnected();
mAdapter.notifyDataSetChanged();
}
};
mCastManager.addVideoCastConsumer(mCastConsumer);


//fetch local video
btnLocalVideo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

Intent intent = new Intent();
intent.setType("video/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Video"), REQUEST_TAKE_GALLERY_VIDEO);


}
});
}


//get data of locally fetched videos....
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == getActivity().RESULT_OK) {
if (requestCode == REQUEST_TAKE_GALLERY_VIDEO) {
selectedImageUri = data.getData();

Toast.makeText(getActivity(), "Sending local video data", Toast.LENGTH_SHORT).show();
MediaMetadata movieMetadata = new MediaMetadata(MediaMetadata.MEDIA_TYPE_MOVIE);

movieMetadata.putString(MediaMetadata.KEY_SUBTITLE, "Local Subtitle");
movieMetadata.putString(MediaMetadata.KEY_TITLE, "Local Video is playing");

JSONObject jsonObj = null;
try {
jsonObj = new JSONObject();
jsonObj.put("description", "Sample Descrioption over here");
} catch (JSONException e) {
Log.e(TAG, "Failed to add description to the json object", e);
}
String filePath = getPath(selectedImageUri);
chosenExt = filePath.substring(filePath.lastIndexOf(".") + 1);
startLocalServer();
String localUrl = formatedIpAddress + localFilePath;
Log.d(TAG, "MediaInfo urlLocal: " + localUrl);
MediaInfo item = new MediaInfo.Builder(localUrl)
.setStreamType(MediaInfo.STREAM_TYPE_BUFFERED)
.setContentType("video/mp4")
.setMetadata(movieMetadata)
.setStreamDuration(333 * 1000)
.build();
Log.d(TAG, "VideoBrowserFragment itemClicked media :" + Utils.mediaInfoToBundle(item));

Intent intent = new Intent(getActivity(), LocalPlayerActivity.class);
intent.putExtra("media", Utils.mediaInfoToBundle(item));
intent.putExtra("shouldStart", false);
intent.putExtra("isLocal", true);
intent.putExtra("videoUri", selectedImageUri.toString());
/* Pair<View, String> imagePair = Pair
.create((View) viewHolder.getImageView(), transitionName);*/
ActivityOptionsCompat options = ActivityOptionsCompat
.makeSceneTransitionAnimation(getActivity());
ActivityCompat.startActivity(getActivity(), intent, options.toBundle());

}
}
}


//get path of video fetched...
public String getPath(Uri uri) {
Log.d(TAG, "uri is " + uri);
String[] proj = {MediaStore.Images.Media.DATA};
CursorLoader loader = new CursorLoader(getActivity(), uri, proj, null, null, null);
Cursor cursor = loader.loadInBackground();
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();

if (cursor.moveToFirst()) {
//int column_index1 = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);//Instead of "MediaStore.Images.Media.DATA" can be used "_data"
Uri filePathUri = Uri.parse(cursor.getString(column_index));
String file_name = filePathUri.getLastPathSegment().toString();
String file_path = filePathUri.getPath();
localFileName = file_name;
localFilePath = file_path;
Toast.makeText(getActivity(), "File Name & PATH are:" + file_name + "\n" + file_path, Toast.LENGTH_LONG).show();
Log.d("zz fileData: ", "fp: " + file_path + " :and: fn: " + file_name);
}
String result = cursor.getString(column_index);
cursor.close();
return result;
}

@Override
public void onDetach() {
Toast.makeText(getActivity(), "VideoBrowserFragment onDetach", Toast.LENGTH_SHORT).show();
Log.d(TAG, "VideoBrowserFragment onDetach");
mCastManager.removeVideoCastConsumer(mCastConsumer);
super.onDetach();
}

@Override
public void itemClicked(View view, MediaInfo item, int position) {
Toast.makeText(getActivity(), "VideoBrowserFragment itemClicked", Toast.LENGTH_SHORT).show();
Log.d(TAG, "VideoBrowserFragment itemClicked");

if (view instanceof ImageButton) {
Log.d(TAG, "menu was clicked");
com.scriptlanes.cast2.utils.Utils.showQueuePopup(getActivity(), view, item);
} else {
String transitionName = getString(R.string.transition_image);
VideoListAdapter.ViewHolder viewHolder =
(VideoListAdapter.ViewHolder) mRecyclerView.findViewHolderForPosition(position);
Pair<View, String> imagePair = Pair
.create((View) viewHolder.getImageView(), transitionName);
ActivityOptionsCompat options = ActivityOptionsCompat
.makeSceneTransitionAnimation(getActivity(), imagePair);

Intent intent = new Intent(getActivity(), LocalPlayerActivity.class);
Log.d(TAG, "VideoBrowserFragment itemClicked media :" + Utils.mediaInfoToBundle(item));

intent.putExtra("media", Utils.mediaInfoToBundle(item));
intent.putExtra("shouldStart", false);
//isUrl=true, isLocal=false
intent.putExtra("isLocal", false);
ActivityCompat.startActivity(getActivity(), intent, options.toBundle());
}
}

@Override
public Loader<List<MediaInfo>> onCreateLoader(int id, Bundle args) {
Toast.makeText(getActivity(), "VideoBrowserFragment onCreateLoader", Toast.LENGTH_SHORT).show();
Log.d(TAG, "VideoBrowserFragment onCreateLoader");

return new VideoItemLoader(getActivity(), CATALOG_URL);
}

@Override
public void onLoadFinished(Loader<List<MediaInfo>> loader, List<MediaInfo> data) {
Toast.makeText(getActivity(), "VideoBrowserFragment onLoadFinished", Toast.LENGTH_SHORT).show();
Log.d(TAG, "VideoBrowserFragment onLoadFinished");
mAdapter.setData(data);
mLoadingView.setVisibility(View.GONE);
mEmptyView.setVisibility(null == data || data.isEmpty() ? View.VISIBLE : View.GONE);
}

@Override
public void onLoaderReset(Loader<List<MediaInfo>> loader) {
Toast.makeText(getActivity(), "VideoBrowserFragment onLoaderReset", Toast.LENGTH_SHORT).show();
Log.d(TAG, "VideoBrowserFragment onLoaderReset");
mAdapter.setData(null);
}



/*temp code nanohttpd*/


public class LocalServer extends NanoHTTPD {

private final Logger LOG = Logger.getLogger(LocalServer.class.getName());

public void main(String[] args) {
ServerRunner.run(LocalServer.class);
}
public LocalServer() {
super(3000);
Log.d("zz nanaHttpd: ", "STARTED");

}

@Override
public Response serve(String uri, Method method, Map<String, String> header, Map<String, String> parameters, Map<String, String> files) {
//String mediasend = getExtension(chosenFile);

Log.d("zz nanaHttpd: ", "uri: " + uri+" , method: "+method);
FileInputStream fis = null;
File file = new File(localFilePath);
Log.e("Creando imputStream", "Size: ");
try {
fis = new FileInputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}

return new NanoHTTPD.Response(Response.Status.OK, "video/mp4", fis);
}

}

private void startLocalServer() {
server = new LocalServer();
try {
server.start();
} catch (IOException ioe) {
Log.d("zz Httpd", "The server could not start.");
}
Log.d("zz Httpd", "Web server initialized.");
WifiManager wifiManager = (WifiManager) getActivity().getSystemService(Context.WIFI_SERVICE);
int ipAddress = wifiManager.getConnectionInfo().getIpAddress();
Log.d("zz Httpd IP U: ", ipAddress + "");
String formatedIpAddress1 = String.format("%d.%d.%d.%d", (ipAddress & 0xff), (ipAddress >> 8 & 0xff),
(ipAddress >> 16 & 0xff), (ipAddress >> 24 & 0xff));
formatedIpAddress = "http://" + formatedIpAddress1 + ":" + server.getListeningPort();
Log.d("zz Httpd IP: ", "Please access! " + formatedIpAddress);


}

@Override
public void onDestroy() {
super.onDestroy();
//server.stop();
}
}

最佳答案

正如您在其他帖子中看到的,您需要向您的应用程序添加一个嵌入式网络服务器,并在该网络服务器中公开您的本地内容。然后你需要将这些本地内容的 url 发送到你的 chromecast。如果您不想构建自己的微型 http 服务器,可以使用许多开源小型嵌入式 http 服务器,您可以在 google 上搜索此类嵌入式解决方案。

关于android - 将视频和图像从本地存储加载到 Google ChromeCast android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36738143/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com