Short audio playback and video playback a way

Music player main class

public class Demo extends AppCompatActivity {

    SoundPool pool;
    int idOne;
    int idTwo;
    int playid1;
    int playid2;
    Button start;
    Button pause;
    Button stop;

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

        initpool();

        initView();
    }

    private void initView() {
        start = findViewById(R.id.start);
        pause = findViewById(R.id.pause);
        stop = findViewById(R.id.stop);
    }

    public void initpool(){
        pool = new SoundPool(2,AudioManager.STREAM_MUSIC,0);
        idOne = pool.load(this, R.raw.jiuer, 1);
        if(idOne <= 0){
            Toast.makeText(this, "加载失败", Toast.LENGTH_SHORT).show();
            finish();
        }

        idTwo = pool.load(this,R.raw.river,1);
    }

    public void start_stop(View view) {
        switch (view.getId()){
            case R.id.start:
                playid1 = pool.play(idOne, 1.0f, 1.0f, 1, 0, 1.0f);
                playid2 = pool.play(idTwo,1.0f,1.0f,1,0,1.0f);
                break;
            case R.id.pause:
                pool.pause(playid1);
                pool.pause(playid2);
                break;
            case R.id.stop:
                pool.stop(playid1);
                pool.stop(playid2);
                pool.release();
                break;
                default:
                    break;
        }
    }
}

Music layout file

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent"
    tools:context=".Demo">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="播放"
        android:id="@+id/start"
        android:onClick="start_stop"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="暂停"
        android:id="@+id/pause"
        android:onClick="start_stop"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="停止"
        android:id="@+id/stop"
        android:onClick="start_stop"
        />

</LinearLayout>

Video master class

public class Demo1 extends AppCompatActivity {

    VideoView video;
    Button starts;
    Button pause;
    Button stop;

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

        initview();
    }

    private void initview() {
        video = findViewById(R.id.video);
        starts = findViewById(R.id.starts);
        pause = findViewById(R.id.pause);
        stop = findViewById(R.id.stop);

        video.setMediaController(new MediaController(this));
        Uri uri = Uri.parse("http://vfx.mtime.cn/Video/2019/03/13/mp4/190313094901111138.mp4");
        video.setVideoURI(uri);
        video.requestFocus();
        video.start();
    }

    public void starts_stop(View view) {
        switch (view.getId()){
            case R.id.starts:
                video.start();
                break;
            case R.id.pause:
                video.pause();
                break;
            case R.id.stop:
                video.stopPlayback();
                video.resume();
                break;
                default:
                    break;
        }
    }
}

Video layout file

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".Demo1">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="暂停"
        android:id="@+id/pause"
        android:onClick="starts_stop"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="停止"
        android:id="@+id/stop"
        android:onClick="starts_stop"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="播放"
        android:id="@+id/starts"
        android:onClick="starts_stop"
        />
    <VideoView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/video"
        />

</LinearLayout>

Video Player renderings

Play and pause can be realized drag

Here Insert Picture Description

Music renderings

Can achieve pause playback

Here Insert Picture Description

Guess you like

Origin blog.csdn.net/wangwei_weibo/article/details/92009787