Lolik

not404

nothing
x
bilibili
github
telegram

recyclerView Jump and musicserver

Activity Lifecycle#

Trigger onCreate-> onStart->onResume upon entry

29255bbf1bb9423f8702866bcca2f9f3

Trigger onPause->onStop->onDestroy upon exit

a75fc07912164517a7c979f58199eac0

Summary

  • onCreate()
    It is triggered when the system first creates the Activity. The Activity enters the "created" state after creation.
  • onStart()
    When the Activity enters the "started" state, the system calls this callback. The onStart() method completes quickly, just like the "created" state, and the Activity does not remain in the "started" state. Once this callback is finished, the Activity enters the "resumed" state and the system calls the onResume() method.
  • onResume()
    The Activity comes to the foreground when it enters the "resumed" state, and then the system calls the onResume() callback. This is the state where the application interacts with the user. The application will remain in this state until certain events occur that take the focus away from the application. Such events include receiving a phone call, the user navigating to another Activity, or the device screen turning off.
  • onPause()
    The system considers this method as the first indication that the user is about to leave your Activity. This method indicates that the Activity is no longer in the foreground. If the Activity returns from the "paused" state to the "resumed" state, the system will call the onResume() method again.
  • onStop()
    If the Activity is no longer visible to the user, it means that it has entered the "stopped" state, and the application should release or adjust any unnecessary resources when the application is not visible to the user.
  • onDestroy()
    Before destroying the Activity, the system will first call onDestroy(). The system calls this callback when:
    1. The Activity is about to finish (either because the user completely closes the Activity or because the system calls finish() for the Activity), or
    2. The Activity is temporarily destroyed due to a configuration change (such as device rotation or multi-window mode)

recyclerView Jump#

Implement a music list, click to enter the details page to play the corresponding music

7984d4d0e9374f2298b94537918ad798

81f024c2cb6842acac23d5b0e6552991

recyclerView Part (musicAdapter)#

private void Viewinit() {
        context = this.getActivity();

        myadapter = new musicAdapter(context);
        //Set LayoutManager
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setHasFixedSize(true);
        recyclerView.setAdapter(myadapter);
        myadapter.list(namelist,artistlist,urllist,coverlist);
    }

Define a member function in musicadapter to get the song name, artist, MP3 link, and cover link

 	private List<String> namelist;
    private List<String> artistlist;
    private List<String> urllist;
    private List<String> coverlist;


    public musicAdapter(Context context) {

        this.context = context;
    }

    public void list(List<String> namelist,List<String> artistlist,List<String> urllist,List<String> coverlist) {
        this.namelist=  namelist;
        this.artistlist= artistlist;
        this.urllist= urllist;
        this.coverlist= coverlist;
        notifyDataSetChanged();

    }

The song list JSON file contains "name", "artist", "url", "cover", and "lrc"

["url", "cover", "lrc"] are all web addresses, concatenated with https://cdn.unrun.top/blog/music/ to obtain the URL
output.json is placed in the raw folder and the JSON format is as follows

{"data":[{
    "name": "花の塔",
    "artist": "酸欠少女さユり",
    "url": "45.mp3",
    "cover": "45.jpg",
    "lrc": "45.lrc"
},
{
    "name": "星と君が消えた日",
    "artist": "泠鸢yousa",
    "url": "59.mp3",
    "cover": "59.jpg",
    "lrc": "59.lrc"
}]}

Define a function to parse the song list JSON file, read it line by line as a string, and then parse it

private void getjson(){
        InputStream stream=getResources().openRawResource(R.raw.output);
        BufferedReader reader=new BufferedReader(new InputStreamReader(stream));
        String jsonStr="",line="";
        try {
            while ((line=reader.readLine())!=null){
                jsonStr+=line;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            JSONObject jsonObject1 = new JSONObject(jsonStr);
            JSONArray jsonArray = jsonObject1.getJSONArray("data");
            for(int i=0;i<jsonArray.length();i++){
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                String name = jsonObject.getString("name");
                String artist = jsonObject.getString("artist");
                String url = jsonObject.getString("url");
                String cover = jsonObject.getString("cover");
                String lrc = jsonObject.getString("lrc");
                namelist.add(name);
                artistlist.add(artist);
                urllist.add(url);
                coverlist.add(cover);
//                lrclist.add(lrc);
//                Log.d("f4", String.valueOf(list));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    

Instantiate musicadapter and pass the song information to it

private void Viewinit() {
        context = this.getActivity();
        myadapter = new musicAdapter(context);
        //Set LayoutManager
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setHasFixedSize(true);
        recyclerView.setAdapter(myadapter);
        myadapter.list(namelist,artistlist,urllist,coverlist);
    }

The list function in musicadapter gets the information (written above)

 public void list(List<String> namelist... ){}
unbindService(connection);
            }
        });

In the server file
Inherit a custom class from Binder, and write the playback method myplay()

 class Mybinder extends Binder{
        public  void  myplay(){
            mediaPlayer=MediaPlayer.create(getApplicationContext(),R.raw.flower);
            mediaPlayer.start();
        }
    }

Demonstration#

df7241aa0d05481fa3255f60b29e73c4

Click to bind and start playing the song, click to unbind and pause the song

Complete music list functionality
e3199e1e61cc4484badc75024b7430d9

Code Repository#

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.