Android Studio音乐播放器and视频播放器

音频

package com.example.day7;

import android.media.AudioManager;
import android.media.SoundPool;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    private SoundPool pool;
    private int sound1Id;
    private int sound2Id;

    private Button play;
    private Button pause;
    private Button stop;

    private int steam1Id;
    private int steam2Id;

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

        initPool();

        play = findViewById(R.id.play);
        pause = findViewById(R.id.pause);
        stop = findViewById(R.id.stop);

    }

    public void initPool(){
        pool = new SoundPool(3, AudioManager.STREAM_MUSIC, 0);
        sound1Id = pool.load(this,R.raw.jiuer,1);
        if (sound1Id<=0){
            Log.d("error","load error");
            finish();
        }
        sound2Id = pool.load(this,R.raw.river,2);
    }

    public void onClickBtn(View view) {
        switch (view.getId()) {
            case R.id.play:
                steam1Id = pool.play(sound1Id,1.0f,1.0f,1,0,1.0f);
                steam2Id = pool.play(sound2Id,1.0f,1.0f,1,0,1.0f);
                break;
            case R.id.pause:
                pool.stop(steam1Id);
                pool.release();//释放资源
                break;
            case R.id.stop:
                pool.stop(steam2Id);
                pool.release();//释放资源
                break;
        }
    }
}

视频

package com.example.day7;

import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.VideoView;

public class Main2Activity extends AppCompatActivity {
    private VideoView video;
    private Button play;
    private Button pause;
    private Button stop;

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

        video = findViewById(R.id.video);
        play = findViewById(R.id.play);
        pause = findViewById(R.id.pause);
        stop = findViewById(R.id.stop);

        video.setMediaController(new MediaController(this));//获得控制器
        Uri uri = Uri.parse("http://ftp.nluug.nl/pub/graphics/blender/demo/movies/Sintel.2010.720p.mkv");
        video.setVideoURI(uri);//设置播放哪个资源
        video.requestFocus();
        video.start();
    }

    public void onClickBtn(View view) {
        switch (view.getId()) {
            case R.id.play:
                video.start();
                break;
            case R.id.pause:
                video.stopPlayback();
                break;
            case R.id.stop:
                video.pause();
                break;
        }
    }
}

你可能感兴趣的:(Android Studio音乐播放器and视频播放器)