Android Studio learn to write English dictation app (music player) I

Keywords: MediaPlayer Android xml Java

Recently, I fell in love with Android APP. I read some data and wrote while learning. It's just a hobby to pass the time. The tool is Android studio 3.0.1. In order to be lazy, I'm going to write a dictation APP for my children. The idea is to download the audio resource package from APP to SD card at that time. The APP plays and directly reads the audio under the storage path, instead of reading it from Android's own media library ). First, make the simplest path to play and MP3 play in APP resources.

MP3 play in APP resources


XML omitted,
java code:

//If the data source is set through the create() method, the mediaPlayer.prepare() method cannot be written
        final MediaPlayer mp =MediaPlayer.create(this, R.raw.yes);
        //Add listening event for button play
        play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mp.start();
                Log.v("0001","Main01");
            }
        });

Play MP3 with specified path

Using the Genymotion simulator, this is a copy of MP3 files

This path is used when setDataSource is used to specify the path in the code,

mp.setDataSource("/storage/emulated/0/mp3/002.mp3")

In Android Device Monitor, the path is: mnt/shell/emulated/0/mp3/001.mp3 can't find audio with this path, but when I use the real machine, I can find audio files with the actual address in Android Device Monitor, I don't understand.
XML is the three function keys. The following is the java code:

public class bao_cun extends AppCompatActivity {
    /**
     * Isstart Is the play status variable, 0 is stop, 1 is play, 2 is pause
     * currentPlayPosition In order to record the playback time variable when pausing, the playback starts from this variable time when the playback continues.
     */
    //Instantiate MediaPlayer and set the static variable mp
    private MediaPlayer mp = new MediaPlayer();
    public static MediaPlayer playe = null;
    //Play status
    private int Isstart  = 0;
    //Read tag variables after music pause
    private int currentPlayPosition;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Register XML
        setContentView(R.layout.activity_bao_cun);

        TextView fanHui = (TextView) findViewById(R.id.fanHui_01);
        Button sd_boFang = (Button) findViewById(R.id.sD_bofang);
        Button sd_tingZhi = (Button) findViewById(R.id.sD_tingZhi);
        Button sd_zanTing = (Button) findViewById(R.id.sD_zanTing);

        //Back to previous page
        fanHui.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(bao_cun.this, MainActivity.class);
                startActivity(intent);
            }
        });

        //suspend
        sd_zanTing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(Isstart == 1){
                    //isPlaying() returns boolean, whether it is playing
                if(mp != null && mp.isPlaying()){
                    //Record when paused
                    currentPlayPosition = mp.getCurrentPosition();
                    mp.pause();
                    Isstart = 2;
                }}else {
                    //Time to read and jump to recorded pause
                    mp.seekTo(currentPlayPosition);
                    mp.start();
                    Isstart = 1;
                }


            }
        });

        //Stop it
        sd_tingZhi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mp != null){
                    mp.stop();
                    mp.reset();
                    Isstart = 0;
                }
            }
        });

        //Play button monitor
        sd_boFang.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
             //  Jump
                kaibo1();
                //Output log
                Log.v("0001","yunxing");
            }
        });


    }

    private void kaibo1() {
        //If it's stopped, start playing again
        if(Isstart == 0) {
            //Error proofing
            try {
                //mp3 actual address (Samsung Mobile)
                // mp.setDataSource("/storage/extSdCard/001.mp3");
                //Simulator address
                mp.setDataSource("/storage/emulated/0/mp3/002.mp3");
                //Output debugging information
                System.out.println("OK001");
                //The prepare method is to synchronously cache resources into memory, which can be used to load local smaller resources
                //If the data source is set through the create() method, the mediaPlayer.prepare() method cannot be written
                mp.prepare();
                System.out.println("OK002");
                //Start playing
                mp.start();
                Isstart = 1;
                //Error type
            } catch (IOException e) {
                //Error handling occurred
                e.printStackTrace();
                System.out.println("OK003");
            }
            //If it's paused, continue playing
        }else if(Isstart == 2){
            //Time to read and jump to recorded pause
            mp.seekTo(currentPlayPosition);
            mp.start();
            Isstart = 1;
        }else {//No action during playback
             }

    }



}

Here we mainly learn from other people's codes, through train: https://blog.csdn.net/u012156341/article/details/78979835
In fact, I've done a lot of coding and figured out their functions. I don't think learning things is as fast as I actually do.

Posted by gerardg on Thu, 19 Dec 2019 09:05:50 -0800