从实时数据库播放URL到Exoplayer

时间:2019-12-08 15:33:12

标签: android exoplayer

对于一般的Android开发人员,尤其是对Exoplayer来说,我真的是新手。我已经设置了所有实时数据库检索URL的要求。我使Exoplayer正常工作的最后一次尝试是,我创建了一个新类来处理实时数据库,并将其存储在TextView中,以便解决firebase和android studio之间的链接问题。它显示URL,但是当我尝试与Exoplayer一起使用时,它不会显示。非常感谢您的帮助。

下面是我的代码

    import androidx.annotation.NonNull;
    import androidx.appcompat.app.AppCompatActivity;

    import android.content.Intent;
    import android.os.Bundle;
    import android.widget.Button;
    import android.widget.TextView;

    import com.google.firebase.database.DataSnapshot;
    import com.google.firebase.database.DatabaseError;
    import com.google.firebase.database.DatabaseReference;
    import com.google.firebase.database.FirebaseDatabase;
    import com.google.firebase.database.ValueEventListener;

    public class lounge extends AppCompatActivity {
        TextView mFireText;
        Button mButton;
        String link;
        DatabaseReference mrootRef = FirebaseDatabase.getInstance().getReference();
        DatabaseReference mlinkRef = mrootRef.child("Test_URL");
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_lounge);

            mFireText = (TextView)findViewById(R.id.textView);
            mButton = (Button)findViewById(R.id.testButton);
        }

    public void storeLink(){
        Intent lIntent = new Intent(this, lounge.class);
        lIntent.putExtra("EXTRA_LINK",link);
    }
        @Override
        protected void onStart() {
            super.onStart();
            mlinkRef.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    String link  = dataSnapshot.getValue(String.class);
                    mFireText.setText(link);
                    link = mFireText.getText().toString();



                }

//and Exoplayer code:

    public class MediaPlayerActivity extends AppCompatActivity {

        PlayerView playerView;
        SimpleExoPlayer exoPlayer;





        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setFullScreen ();
            setContentView(R.layout.activity_media_player);
            hideActionbar();
            Intent intent =getIntent();
            String link =intent.getStringExtra("EXTRA_LINK");

            try {

                playerView = findViewById(R.id.exo_player);
                exoPlayer = ExoPlayerFactory.newSimpleInstance(this);
                playerView.setPlayer(exoPlayer);

                DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(this,Util.getUserAgent(this, "appname"));
                HlsMediaSource videoSource = new HlsMediaSource.Factory(dataSourceFactory)
                        .createMediaSource(Uri.parse(link));
                exoPlayer.prepare(videoSource);
                exoPlayer.setPlayWhenReady(true);

1 个答案:

答案 0 :(得分:0)

您正在MediaPlayerActivity中使用exoplayer,因此您必须从当前活动开始该活动,并通过意图传递链接。喜欢

 String link;
 public void storeLink(){
    Intent lIntent = new Intent(this, MediaPlayerActivity.class);
    lIntent.putExtra("EXTRA_LINK",link);
}
    @Override
    protected void onStart() {
        super.onStart();
        mlinkRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                link  = dataSnapshot.getValue(String.class);
                mFireText.setText(link);
                link = mFireText.getText().toString();
                storeLink();
            }
          }
       }