将资源的路径存储在ArrayList中

时间:2019-08-26 11:57:12

标签: java android-studio arraylist uri android-mediaplayer

我正在尝试创建一个单击RecyclerView特定项目上的按钮时播放音频文件的应用程序。实际上,第一项。我正在尝试以最有效的方式做到这一点。

到目前为止,我正在尝试使用我创建的自定义类将文件(R.raw.something)的路径存储在Arraylist中。我只想播放recyclerView中第一项的音频。

// Here's the function I call to add items to the list

public void getSongsContent(){
        if (mExampleList.size()==0){
            mExampleList.add(new ExampleItems("The Lion Sleeps Tonight", "Meme Community",R.raw.thelionsleepstonight ));
            mExampleList.add(new ExampleItems("And I Oop!", "Jasmine Master",R.raw.andioop ));
            mExampleList.add(new ExampleItems("Hoes Mad!","Famous Dex" ,R.raw.hoesmad ));
        }

    }
// Here's the ExampleItems class

public class ExampleItems {
    private String mNameoftheSong;
    private String mNameoftheArtists;
    private int mSongPath;

    public ExampleItems(String NameoftheSong, String NameoftheArtists, int PathofTheSong){
        mNameoftheSong = NameoftheSong;
        mSongPath = PathofTheSong;
        mNameoftheArtists = NameoftheArtists;
    }
    public String getNameoftheSong(){
        return mNameoftheSong;
    }
    public String getNameoftheArtists(){
        return mNameoftheArtists;
    }
    public int getPathtoTheSong(){
        return mSongPath;
    }
}
// And Here's the playAudio function which makes the app crash.

private void playAudio(){
        int path = mExampleList.get(0).getPathtoTheSong();
        if (player==null){
            player = MediaPlayer.create(this,path );
            player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    stopPlayer();
                }
            });
        }
        player.start();
    }

我希望播放正确的音频,具体取决于我在recyclerView中最先放置的项目。我在另一篇文章中看到,不可能将任何资源路径存储到Arraylist中,并且我可能应该将文件移至“ / Assets” 现在,按下按钮会导致应用程序崩溃。我该如何解决?

这是堆栈跟踪:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.awolobes.pavilion.wavedrop, PID: 25947
    android.content.res.Resources$NotFoundException: Resource ID #0x0
        at android.content.res.ResourcesImpl.getValue(ResourcesImpl.java:190)
        at android.content.res.ResourcesImpl.openRawResourceFd(ResourcesImpl.java:287)
        at android.content.res.Resources.openRawResourceFd(Resources.java:1446)
        at android.media.MediaPlayer.create(MediaPlayer.java:941)
        at android.media.MediaPlayer.create(MediaPlayer.java:924)
        at com.awolobes.pavilion.wavedrop.MainActivity.playAudio(MainActivity.java:112)
        at com.awolobes.pavilion.wavedrop.MainActivity.access$400(MainActivity.java:22)
        at com.awolobes.pavilion.wavedrop.MainActivity$5.onButtonClick(MainActivity.java:145)
        at com.awolobes.pavilion.wavedrop.RecyclerViewAdapter$PinnedViewHolder$1.onClick(RecyclerViewAdapter.java:61)
        at android.view.View.performClick(View.java:5675)
        at android.view.View$PerformClick.run(View.java:22641)
        at android.os.Handler.handleCallback(Handler.java:836)
        at android.os.Handler.dispatchMessage(Handler.java:103)
        at android.os.Looper.loop(Looper.java:203)
        at android.app.ActivityThread.main(ActivityThread.java:6251)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)

最后,这是整个MainActivity.java

public class MainActivity extends AppCompatActivity {

    MediaPlayer player;
    // RecyclerView Variables
    private ArrayList<ExampleItems> mExampleList;
    private RecyclerView mRecylerView;
    private RecyclerViewAdapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;


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

    @Override
    protected void onPause() {
        super.onPause();
        saveData();
    }

    public void getSongsContent(){
        if (mExampleList.size()==0){
            mExampleList.add(new ExampleItems("The Lion Sleeps Tonight", "Meme Community",R.raw.thelionsleepstonight ));
            mExampleList.add(new ExampleItems("And I Oop!", "Jasmine Master",R.raw.andioop ));
            mExampleList.add(new ExampleItems("Hoes Mad!","Famous Dex" ,R.raw.hoesmad ));
        }

    }
    private void saveData(){
        SharedPreferences sharedPreferences = getSharedPreferences("shared preferences",MODE_PRIVATE );
        SharedPreferences.Editor editor = sharedPreferences.edit();
        Gson gson = new Gson();
        String json = gson.toJson(mExampleList);
        editor.putString("State",json );
        editor.apply();
    }
    private void loadData(){
        SharedPreferences sharedPreferences = getSharedPreferences("shared preferences",MODE_PRIVATE );
        Gson gson = new Gson();
        String json = sharedPreferences.getString("State",null );
        Type type = new TypeToken<ArrayList<ExampleItems>>() {}.getType();
        mExampleList = gson.fromJson(json,type );

        if (mExampleList == null){
            mExampleList = new ArrayList<>();
        }
    }
    private void playAudio(){
        int path = mExampleList.get(0).getPathtoTheSong();
        if (player==null){
            player = MediaPlayer.create(this,path );
            player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    stopPlayer();
                }
            });
        }
        player.start();
    }
    private void stopPlayer(){
        if (player != null){
            player.release();
            player = null;
        }
    }
    public void createRecyclerView(){
        mRecylerView = findViewById(R.id.Recyclerview);
        mRecylerView.setHasFixedSize(true);
        mLayoutManager = new LinearLayoutManager(this);
        mAdapter = new RecyclerViewAdapter(mExampleList);
        mRecylerView.setLayoutManager(mLayoutManager);
        mRecylerView.setAdapter(mAdapter);

        mAdapter.setOnitemClickListener(new RecyclerViewAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                swapData(position);

            }

            @Override
            public void onButtonClick(int position) {
                playAudio();
            }
        });

    }
    public void swapData(int position){
        mExampleList.add(0,mExampleList.remove(position));
        mAdapter.notifyItemMoved(position,0 );
    }
}

我希望它能提供帮助。

0 个答案:

没有答案