我有2个按钮,可以下载并播放 当我单击下载按钮时,它应该下载我已经在FireBase存储上上传的mp3文件。
当我单击“播放”按钮时,它应该播放下载的音频。
我查看了一些教程,但没有成功。 该代码未显示任何错误,但也未下载音频文件。
请帮助并提供一个非常简单的解决方案,这非常重要。
public class MainActivity extends AppCompatActivity {
StorageReference storageReference,ref;
FirebaseStorage firebaseStorage;
Button down;;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
down=findViewById(R.id.down);
FirebaseApp.initializeApp(this);
down.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
download();
}
});
}
public void download(){
storageReference=firebaseStorage.getInstance().getReference();
ref=storageReference.child("testaudio.mp3");
ref.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) { String url = uri.toString();
downloadFiles(MainActivity.this,"testaudiofile",".mp3",DIRECTORY_DOWNLOADS,url);
// downloadFile();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
}
});
}
public void downloadFiles(Context context,String fileName,String fileExtension,String destinationDirectory,String url) {
DownloadManager downloadManager = (DownloadManager)context.getSystemService(Context.DOWNLOAD_SERVICE);
Uri uri = Uri.parse(url);
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalFilesDir(context,destinationDirectory,fileName+fileExtension);
downloadManager.enqueue(request);
}
public void Play(){
}
}
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android" >
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/down"
android:text="Download"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Play"
android:onClick="Play"/>
</LinearLayout>