Android Studio Lesson 11 Network Programming 3 (Learn to punch Day17)

πŸ“‹ About the author: Hello friends, I am the Chaos Cape Hammer, one of the sophomore students
πŸ’– Author homepage: The personal blog page of the Chaos Cape Hammer. [Jump to my profile]
πŸ”₯ I like to edit like my friends and I type code every day 🀭, addicted to study, losing weight day by day. I am honored to share what I have learned with you and make progress with you. If the article is wrong, welcome to correct it in the comment area. So start learning today!
πŸŽ‰ Support me: LikeπŸ‘+ Favorite ⭐️+ Comment πŸ“

Android Studio play music

One, build [gradle] guide package

Add code in build gradle

viewBinding{
        enabled=true
    }

2. Open network permissions

Add the following code files to AndroidManifest.xml:

<uses-permission android:/>
 android:usesCleartextTraffic="true"

3. Add and play music files

Create a folder as shown

Fourth, the main code

package com.hnucm.network3;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.hnucm.network3.databinding.ActivityMainBinding;

import java.io.IOException;

public class MainActivity extends AppCompatActivity {

    //All layout files are automatically generated java class name is layout file name + Binding
    ActivityMainBinding activityMainBinding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityMainBinding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(activityMainBinding.getRoot());
        MediaPlayer mediaPlayer = new MediaPlayer();

        activityMainBinding.button.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("NewApi")
            @Override
            public void onClick(View view) {
//           2     activityMainBinding.drawerlayout.openDrawer(Gravity.LEFT);
                try {
                    mediaPlayer.setDataSource(getAssets().openFd("a1.mp3"));
                    mediaPlayer.prepare();
                    mediaPlayer.start();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });

        activityMainBinding.button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mediaPlayer != null && mediaPlayer.isPlaying()) {
                    mediaPlayer.pause();
                }
            }
        });
        activityMainBinding.button4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    mediaPlayer.setDataSource("http://121.4.44.56/a1.mp3\n");
                    mediaPlayer.prepareAsync();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                    @Override
                    public void onPrepared(MediaPlayer mediaPlayer) {

                        mediaPlayer.start();
                    }
                });
            }
        });
    }
}

Summarize

Alright, that’s it for today’s study. Friends who think it is good can give a follow, like or favorite! Thank you friends for your support. The following code hopes that you guys can check it yourself, after all, the hands-on operation makes the memory more profound.

Leave a Comment

Your email address will not be published. Required fields are marked *