如何将原始 mp3 从 recyclerview 项目传递到另一个 activity

How to pass raw mp3 from recyclerview item to another activity

在我的 Android 应用程序中,我有一个包含项目列表的回收视图。在 recyclerview activity 中,每个项目都有一个文本视图,另一个 activity 有两个文本视图和一个音频播放按钮。但音频不播放。如何设置 setOnClickListener 并使用 MediaPlayer 播放音频 mp3。这是我的代码-

NameModel.java


public class NameModel {

    private String name,meaning;
    private int audio;

    public NameModel(String name, String meaning, int audio) {

        this.name= name;
        this.meaning= meaning;
        this.audio= audio;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name= name;
    }

    public String getMeaning() {
        return meaning;
    }

    public void setMeaning(String meaning) {
        this.meaning= meaning;
    }

    public int getAudio() {
        return audio;
    }

    public void setAudio(int audio) {
        this.audio = audio;
    }
}

NameAdapter.java


itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int position=getAdapterPosition();
                    NameModel nameModel=nameItemList.get(position);

                    Intent intent=new Intent(context, NameDetailsActivity.class);
                    intent.putExtra("NAME",nameModel.getName());
                    intent.putExtra("MEANING",nameModel.getMeaning());
                    intent.putExtra("AUDIO", nameModel.getAudio());
                    context.startActivity(intent);
                }
            });

MainActivity.java


public class MainActivity extends AppCompatActivity {

    private LinearLayoutManager linearLayoutManager;
    private List<NameModel> nameModelList;
    private NameAdapter nameAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        RecyclerView recyclerView  = (RecyclerView) findViewById(R.id.recyclerViewId);
        nameModelList = new ArrayList<>();
        nameAdapter = new NameAdapter(this,nameModelList);

        linearLayoutManager = new LinearLayoutManager(this);
        recyclerView.setAdapter(nameAdapter);
        recyclerView.setLayoutManager(linearLayoutManager);

        RecyclerView.ItemDecoration itemDecoration=new DividerItemDecoration(getApplicationContext(),DividerItemDecoration.VERTICAL);
        recyclerView.addItemDecoration(itemDecoration);

        nameModelList.add(new NameModel("Mahmud", "The praised one", R.raw.mahmud));
        nameModelList.add(new NameModel("Maaz", "Brave Man", R.raw.maaz));
    }
}

NameDetailsActivity.java


public class NameDetailsActivity extends AppCompatActivity {

    Context context;
    NameModel nameModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_details_name);

        String name = getIntent().getStringExtra("NAME");
        String meaning = getIntent().getStringExtra("MEANING");
        int audio = getIntent().getIntExtra("AUDIO",0);

        TextView nameText = (TextView) findViewById(R.id.tvName);
        TextView meaningText = (TextView) findViewById(R.id.tvMeaning);
        Button playbutton = (Button) findViewById(R.id.playBtn);

        nameText.setText(name);
        meaningText.setText(meaning);

        playbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MediaPlayer mediaPlayer = MediaPlayer.create(context,audio);
                mediaPlayer.start();
            }

        });
    }
}

如有任何帮助,我们将不胜感激。谢谢!

首先,您所做的 getIntent().getStringExtra("AUDIO"); 在这种情况下是不正确的。 您在 intent.putExtra("AUDIO", nameModel.getAudio()); 中设置了 int 值,因此您必须使用 int audio = getIntent().getIntExtra("AUDIO"); 取回 int 值。希望对你有所帮助。

我还注意到一件事。在您的 NameDetailsActivity 中,您在哪里初始化上下文和 NameModel class?实际上我不知道为什么 NameDetailsActivity 中需要 NameModel class,但无论如何......如果你将那个上下文传递给媒体播放器而不初始化,你将得到 nullPointerException。你会得到像 Caused by java.lang.NullPointerException Attempt to invoke virtual method 'int com.tahmid.name.models.NameModel.getAudioFile()' 这样的异常。那是因为你的 NameModel class in NameDetailsActivity 是空的。

你必须做两件事。 如果在NameDetailsActivity中需要NameModelclass,则必须在NameModelclass中创建一个空构造函数。你可以在我下面的回答中找到它。并且您必须在 NameDetailsActivity.

中初始化 context 和 nameModel

所以试试这个代码。

首先在你的 NameModel 中创建一个空的构造函数。

public class NameModel {

private String name,meaning;
private int audio;

public NameModel() {}

public NameModel(String name, String meaning, int audio) {

    this.name= name;
    this.meaning= meaning;
    this.audio= audio;
}

// The same code...

而你的 NameDetailsActivity 你可以做到这一点。

public class NameDetailsActivity extends AppCompatActivity {

Context context;
NameModel nameModel;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_details_name);

    context = getContext();
    nameModel = new NameModel();

    String name = getIntent().getStringExtra("NAME");
    String meaning = getIntent().getStringExtra("MEANING");
    int audio = getIntent().getIntExtra("AUDIO",0);

    TextView nameText = (TextView) findViewById(R.id.tvName);
    TextView meaningText = (TextView) findViewById(R.id.tvMeaning);
    Button playbutton = (Button) findViewById(R.id.playBtn);

    nameText.setText(name);
    meaningText.setText(meaning);

    playbutton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            MediaPlayer mediaPlayer = MediaPlayer.create(context,audio);
            mediaPlayer.start();
        }

    });
}
}