新bie:为什么这段代码无法从firebase数据库中读取任何内容

时间:2018-02-26 07:33:09

标签: java firebase firebase-realtime-database firebase-storage

以下是我的主要活动,在另一个活动中,用户可以登录并将音频文件上传到firebase存储;之后,它以相反的顺序将链接和当前日期时间保存在firebase数据库中。 数据库的结构是Audios> 2月26,2018> " http://链接到firebase存储" 我想在此活动中尝试实现的是从数据库中读取链接>将值存储在字符串值>中并在媒体播放器上播放,但以下代码无法从数据库中读取链接为我的生活请帮助我,我是一个新的bie

package com.shresthawebworks.dailydevotional;



public class MainActivity extends AppCompatActivity implements   MediaPlayer.OnPreparedListener{
private MediaPlayer mMediaplayer;
private ImageButton dplayButton;
private ImageButton dpauseButton;
private ImageButton dstopButton;
private SeekBar seekBar;
private Handler handler;
private Runnable runnable;
private TextView durl;
private TextView drealurl;

private String currentDateTimeString =         DateFormat.getDateInstance().format(new Date());
private static final String TAG = null;
private String value = "Udated";
FirebaseDatabase database = FirebaseDatabase.getInstance();






    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    dplayButton = findViewById(R.id.playButton);
    dpauseButton = findViewById(R.id.pauseButton);
    dstopButton = findViewById(R.id.stopButton);
    durl = findViewById(R.id.dateString);
    drealurl = findViewById(R.id.urlText);

    handler = new Handler();
    seekBar = findViewById(R.id.seekBar1);

    mMediaplayer = new MediaPlayer();
    mMediaplayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    DatabaseReference myRef = database.getReference("Audios");

    // Read from the database
    myRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            value = (String)    dataSnapshot.child(currentDateTimeString).getValue();
            // do your stuff here with value

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
   fetchAudioUrlFromFirebase();
//.setDataSource();
    seekBar.setOnSeekBarChangeListener(new  SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean input) {
        if (input){
            mMediaplayer.seekTo(progress);
        }
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }

    });

    configureNextButton();}
    public void playCycle(){
    seekBar.setProgress(mMediaplayer.getCurrentPosition());
    if (mMediaplayer.isPlaying()){
        runnable =new Runnable() {
            @Override
            public void run() {
            playCycle();
            }
        };
        handler.postDelayed(runnable,1000);
    }
    }

@Override
protected void onResume() {
    super.onResume();


   // mMediaplayer.resu;
    playCycle();

}

@Override
protected void onPause() {
    super.onPause();
    mMediaplayer.pause();
    playCycle();
}

@Override
protected void onDestroy() {
    super.onDestroy();
   mMediaplayer.release();
    handler.removeCallbacks(runnable);
}





private void fetchAudioUrlFromFirebase() {
    final FirebaseStorage storage = FirebaseStorage.getInstance();
    durl.setText(currentDateTimeString);
    drealurl.setText(value);

    // Create a storage reference from our app
    StorageReference storageRef = storage.getReferenceFromUrl("https://firebasestorage.googleapis.com/v0/b/dailydevotional-9f982.appspot.com/o/audios%2Ftwoprincess.mp3?alt=media&token=e3f98223-68e8-461a-8452-54eca96af4fd");
    storageRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
        @Override
        public void onSuccess(Uri uri) {
            try {
                // Download url of file
                final String url = uri.toString();
                mMediaplayer.setDataSource(url);
                // wait for media player to get prepare
                mMediaplayer.setOnPreparedListener(MainActivity.this);
                mMediaplayer.prepareAsync();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    })
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Log.i("TAG", e.getMessage());
                }
            });

}
@Override
public void onPrepared(final MediaPlayer mp) {

    dplayButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            seekBar.setMax(mp.getDuration());




            mp.start();
            playCycle();

        }
    });
    dpauseButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mp.pause();
        }
    });
    dstopButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            playCycle();
            mp.stop();
            mp.reset();
            //fetchAudioUrlFromFirebase();

        }
    });
}


private void configureNextButton(){

    ImageButton dButton= (ImageButton) findViewById(R.id.starLogin);
    dButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startActivity(new Intent(MainActivity.this, UploadActivity.class));
        }
    });
}

}

0 个答案:

没有答案