我在收集下载的数据并进入本地目录"目录"时遇到问题。然后,我可以在应用程序中使用本地媒体播放器代码。
我已阅读在线指南,并查看了android文档。我知道有一种方法,但它没有意义。我并不是要求它逐字逐句,但也许更好的在线资源可以帮助我更好地理解如何在下载文件后处理这些文件。
import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.storage.FileDownloadTask;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
/**
* Created by Meglio on 3/15/17.
*/
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {
private List<Item_Sermon> itemsList;
public File localFile = null;
public Context context;
public Uri localFilePath;
private FirebaseStorage storage = FirebaseStorage.getInstance();
private StorageReference url;
public class MyViewHolder extends RecyclerView.ViewHolder {
//Create Variables
private TextView mTimeStamp;
private TextView mTitle;
private ImageButton mDownloadButton;
private ImageButton mMediaPlay;
private ProgressBar mProgress;
private MyViewHolder(View view) {
super(view);
// Setup Views and initiate them.
mTimeStamp = (TextView)view.findViewById(R.id.item_textview_timestamp);
mTitle = (TextView)view.findViewById(R.id.item_textview_title);
mDownloadButton = (ImageButton)view.findViewById(R.id.item_button_download);
mMediaPlay = (ImageButton)view.findViewById(R.id.item_button_play);
mProgress = (ProgressBar)view.findViewById(R.id.loadingPanel);
}
}
public RecyclerViewAdapter(List<Item_Sermon> itemsList) {
this.itemsList = itemsList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.sermon_list_item, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
final Item_Sermon item = itemsList.get(position);
//Create holders example below
//Convert long timestamp to string
String dateString = new SimpleDateFormat("MM/dd/yyyy h:mm a").format(new Date(item.getTimestamp()));
holder.mTimeStamp.setText(dateString);
holder.mTitle.setText(item.getTitle());
//setup download button
holder.mDownloadButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View view) {
Log.d("Adapter", "Download Button was clicked!");
loading(holder);
String mFilePath = item.getFilePath();
try {
localFile = File.createTempFile("audio", "mp3");
} catch (IOException e) {
e.printStackTrace();
}
url = storage.getReferenceFromUrl(mFilePath);
url.getFile(localFile)
.addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
@Override
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
// Successfully downloaded data to local file
loadingComplete(holder);
Log.i("Adapter","Download Complete!");
Log.i("Adapter", "Gathering file..");
// How do I cache the files in it's own local
// local directory??
// android online guides aren't helping
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle failed download
loadingFailed(holder);
Log.e("Adapter","Download Failed");
}
});
}
});
holder.mMediaPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Play the audio file with the local media player or create one.
}
});
}
private void loadingComplete(final MyViewHolder holder){
holder.mDownloadButton.setVisibility(View.GONE);
holder.mProgress.setVisibility(View.GONE);
}
private void loading(final MyViewHolder holder){
holder.mDownloadButton.setVisibility(View.GONE);
holder.mProgress.setVisibility(View.VISIBLE);
}
private void loadingFailed(final MyViewHolder holder){
holder.mDownloadButton.setVisibility(View.VISIBLE);
holder.mProgress.setVisibility(View.GONE);
}
@Override
public int getItemCount() {
return itemsList.size();
}
}