• Android Mediaplay 音乐播放器(项目中的音乐)


    布局XML文件
     1 <LinearLayout android:layout_width="match_parent"
     2     android:layout_height="match_parent"
     3     xmlns:android="http://schemas.android.com/apk/res/android">
     4     <Button
     5         android:id="@+id/bt1"
     6         android:text="开始"
     7         android:layout_width="wrap_content"
     8         android:layout_height="wrap_content"/>
     9     <Button
    10         android:id="@+id/bt2"
    11         android:text="暂停"
    12         android:layout_width="wrap_content"
    13         android:layout_height="wrap_content"/>
    14     <Button
    15         android:id="@+id/bt3"
    16         android:text="停止"
    17         android:layout_width="wrap_content"
    18         android:layout_height="wrap_content"/>
    19 
    20 </LinearLayout>
    View Code

    mainactivity

      1 package com.android.booktest;
      2 
      3 import androidx.appcompat.app.AppCompatActivity;
      4 
      5 import android.Manifest;
      6 import android.content.pm.PackageManager;
      7 import android.media.MediaPlayer;
      8 import android.os.Build;
      9 import android.os.Bundle;
     10 import android.view.View;
     11 import android.widget.Button;
     12 
     13 import java.io.IOException;
     14 
     15 
     16 public class MainActivity extends AppCompatActivity {
     17 
     18     private Button bt1;
     19     private Button bt2;
     20     private Button bt3;
     21     private MediaPlayer mediaPlayer;
     22 
     23     @Override
     24     protected void onCreate(Bundle savedInstanceState) {
     25         super.onCreate(savedInstanceState);
     26         setContentView(R.layout.activity_main);
     27         mediaPlayer=new MediaPlayer();
     28         bt1=findViewById(R.id.bt1);
     29         bt2=findViewById(R.id.bt2);
     30         bt3=findViewById(R.id.bt3);
     31         regist();
     32         bt1.setOnClickListener(new startclick());
     33         bt2.setOnClickListener(new pauseclick());
     34         bt3.setOnClickListener(new stopclick());
     35     }
     36 
     37 
     38     private class stopclick implements View.OnClickListener {
     39         @Override
     40         public void onClick(View v) {
     41             if (mediaPlayer.isPlaying()){
     42                 mediaPlayer.reset();
     43                 mediaPlayer.release();
     44             }
     45         }
     46     }
     47 
     48     private class startclick implements View.OnClickListener {
     49         @Override
     50         public void onClick(View v) {
     51             mediaPlayer.reset();
     52             mediaPlayer=MediaPlayer.create(MainActivity.this,R.raw.test);
     53             mediaPlayer.start();
     54 
     55         }
     56     }
     57 
     58     private class pauseclick implements View.OnClickListener {
     59         @Override
     60         public void onClick(View v) {
     61             if(mediaPlayer.isPlaying()){
     62                 mediaPlayer.pause();
     63             }else{
     64                 mediaPlayer.start();
     65             }
     66         }
     67     }
     68 
     69 
     70     private void play_Music(){
     71         try {
     72             //重置播放
     73             mediaPlayer.reset();
     74             //设置播放资源
     75             mediaPlayer.setDataSource("");
     76             //播放准备(若是Mediaplay.creat方法设置资源,则不需要prepare)
     77             mediaPlayer.prepare();
     78             //播放
     79             mediaPlayer.start();
     80         } catch (IOException e) {
     81             e.printStackTrace();
     82         }
     83 
     84     }
     85 
     86     //动态获取SD卡读写权限
     87     private void regist(){
     88         if(Build.VERSION.SDK_INT>=23){
     89             int REQUEST_CODE_CONTACT=101;
     90             final int REQUEST_EXTERNAL_STORAGE=1;
     91             String []PERMISSION_STORAGE={
     92                     Manifest.permission.READ_EXTERNAL_STORAGE,
     93                     Manifest.permission.WRITE_EXTERNAL_STORAGE
     94             };
     95 
     96             for (String str:PERMISSION_STORAGE){
     97                 if (this.checkSelfPermission(str)!= PackageManager.PERMISSION_GRANTED){
     98                     this.requestPermissions(PERMISSION_STORAGE,REQUEST_CODE_CONTACT);
     99                 }
    100             }
    101         }
    102         return;
    103     }
    104 }
  • 相关阅读:
    Spring IoC详解
    Hibernate 和Mybatis的区别
    Nand Flash 驱动框架
    Nor Flash 驱动框架
    USB驱动框架
    输入子系统框架
    module_init 内核调用过程
    平台设备驱动框架
    LCD驱动框架
    嵌入式-开篇
  • 原文地址:https://www.cnblogs.com/xiaowangdatie/p/14046640.html
Copyright © 2020-2023  润新知