读取raw文件夹下的音乐
1.音乐播放简单布局文件:
 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"
     android:layout_width="match_parent" android:layout_height="match_parent"
     android:paddingBottom="@dimen/activity_vertical_margin"
     android:paddingLeft="@dimen/activity_horizontal_margin"
     android:paddingRight="@dimen/activity_horizontal_margin"
     android:paddingTop="@dimen/activity_vertical_margin"
     android:orientation="vertical"
     tools:context="com.asus.android_26_mediaplayer.MainActivity">


     <SeekBar
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:id="@+id/seekbar_main"
         />


     <ImageButton
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:src="@android:drawable/ic_media_play"
         android:onClick="isPlay"
         android:layout_gravity="center_horizontal"
         />


 </LinearLayout>


 2.MainActivity:
 package com.asus.android_26_mediaplayer;


 import android.media.MediaPlayer;
 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.view.View;
 import android.widget.ImageButton;
 import android.widget.SeekBar;


 public class MainActivity extends AppCompatActivity {


     private MediaPlayer mediaPlayer;
     private SeekBar seekbar_main;


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


         seekbar_main = (SeekBar) findViewById(R.id.seekbar_main);


         //给滚动条设置滑动监听事件
         seekbar_main.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
             @Override
             public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {


             }


             @Override
             public void onStartTrackingTouch(SeekBar seekBar) {


             }


             @Override
             public void onStopTrackingTouch(SeekBar seekBar) {
                 //获取拖动结束时的位置
                 int progress=seekbar_main.getProgress();
                 //音乐随滚动条进度进行播放
                 mediaPlayer.seekTo(progress);
             }
         });


     }


     public void isPlay(View view){


         ImageButton imageButton= (ImageButton) view;


         if (mediaPlayer==null){
             //实例化MediaPlayer
             mediaPlayer = MediaPlayer.create(this, R.raw.flowerdance);
             //播放音乐
             mediaPlayer.start();


             //将播放图标变为暂停图标
             imageButton.setImageResource(android.R.drawable.ic_media_pause);


             //获取音乐总时长
             int duration=mediaPlayer.getDuration();
             //设置进度条最大值为音乐总时长
             seekbar_main.setMax(duration);


             new MyThread().start();
         }else if (mediaPlayer.isPlaying()){
             //暂停音乐
             mediaPlayer.pause();
             //将暂停图标变为播放图标
             imageButton.setImageResource(android.R.drawable.ic_media_play);
         }else {
             mediaPlayer.start();
             //将播放图标变为暂停图标
             imageButton.setImageResource(android.R.drawable.ic_media_pause);
         }
     }


     //写一个线程,让进度条随音乐进度动起来
     class MyThread extends Thread{
         @Override
         public void run() {
             super.run();
             while (seekbar_main.getProgress()<=seekbar_main.getMax()){
                 //获取当前音乐播放位置
                 int currentPosition=mediaPlayer.getCurrentPosition();
                 //让进度条动起来
                 seekbar_main.setProgress(currentPosition);
             }
         }
     }


 }
读取内存卡音乐
 1.读取内存卡需添加权限,在AndroidManifest.xml中添加
     <!--读取系统文件权限-->
     <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>


 2.只需把MediaPlayer音源修改即可,如下:
 if (mediaPlayer==null){
             //实例化MediaPlayer
             //1.播放应用内音乐
             //mediaPlayer = MediaPlayer.create(this, R.raw.flowerdance);
             //2.播放sd卡内音乐
             mediaPlayer = new MediaPlayer();
             //设置类型
             mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
             try {
                 //设置音源
                 mediaPlayer.setDataSource(this, Uri.parse("file://mnt/sdcard/Music/flowerdance.mp3"));
                 //准备一下
                 mediaPlayer.prepare();
             } catch (IOException e) {
                 e.printStackTrace();
             }
读取网络音乐
 我们用Tomcat模拟网络音乐读取
 1.找到Tomcat安装目录下面的bin,双击startup.bat




 2.打开命令窗口,执行ipconfig,
 找到IPv4 地址 
 复制IPv4地址,在电脑浏览器和手机浏览器上打开192.168.**.**:8080即可出现Tomcat首页
 就说明连接成功<手机和电脑必须连的是同一个局域网>


 3.以上成功后,说明环境已搭好,我们就可以写代码了:
 只需将以上音乐播放的音源修改,异步准备,设置准备完成的监听即可<注:我的音乐文件是放在webapps/ROOT目录下>:
 if (mediaPlayer==null){
             //实例化MediaPlayer
             //1.播放应用内音乐
         //mediaPlayer = MediaPlayer.create(this, R.raw.flowerdance);
             //2.播放sd卡内音乐
             mediaPlayer = new MediaPlayer();
             //设置类型
             mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
             try {
                 //设置音源
                 //mediaPlayer.setDataSource(this, Uri.parse("file://mnt/sdcard/Music/flowerdance.mp3"));
                 //准备一下
                 //mediaPlayer.prepare();
                 //播放网络音乐
                 mediaPlayer.setDataSource(this, Uri.parse("http://192.168.**.**:8080/flowerdance.mp3"));


                 //异步准备
                 mediaPlayer.prepareAsync();
             } catch (IOException e) {
                 e.printStackTrace();
             }


             //监听,准备完成的监听
             mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                 @Override
                 public void onPrepared(MediaPlayer mp) {
                     //播放音乐
                     mediaPlayer.start();


                     //将播放图标变为暂停图标
                     imageButton.setImageResource(android.R.drawable.ic_media_pause);


                     //获取音乐总时长
                     int duration=mediaPlayer.getDuration();
                     //设置进度条最大值为音乐总时长
                     seekbar_main.setMax(duration);


                     new MyThread().start();
                 }
             });




         }


 4.添加网络权限即可:
 <!-- 联网的权限 -->
     <uses-permission android:name="android.permission.INTERNET" />
音乐池播放<读取raw目录下的音乐文件>
 1.布局文件
 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
     android:id="@+id/activity_sound_pool"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:paddingBottom="@dimen/activity_vertical_margin"
     android:paddingLeft="@dimen/activity_horizontal_margin"
     android:paddingRight="@dimen/activity_horizontal_margin"
     android:paddingTop="@dimen/activity_vertical_margin"
     android:orientation="vertical"
     tools:context="com.asus.android_26_mediaplayer.SoundPoolActivity">


     <Button
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:onClick="playOne"
         android:text="音效鸡" />
     <Button
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:onClick="playTwo"
         android:text="音效二" />
     <Button
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:onClick="playThree"
         android:text="音效三" />
     <Button
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:onClick="playFour"
         android:text="音效四" />
     <Button
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:onClick="playDog"
         android:text="音效狗" />


 </LinearLayout>


 2.SoundPoolActivity:
 package com.asus.android_26_mediaplayer;


 import android.media.AudioManager;
 import android.media.SoundPool;
 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.view.View;


 public class SoundPoolActivity extends AppCompatActivity {


     private SoundPool soundPool;


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


         soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC,0);


         //给音频池设置加载完成的监听
         soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
             @Override
             public void onLoadComplete(SoundPool soundPool, int i, int i1) {
                 soundPool.play(i,1,1,1,-1,1);
             }
         });


     }


     public void playOne(View view){
         soundPool.load(this,R.raw.rooster,1);
     }
     public void playTwo(View view){
         soundPool.load(this,R.raw.chimp,1);
     }
     public void playThree(View view){
         soundPool.load(this,R.raw.crickets,1);
     }
     public void playFour(View view){
         soundPool.load(this,R.raw.roar,1);
     }
     public void playDog(View view){
         soundPool.load(this,R.raw.dogbark,1);
     }


     @Override
     protected void onDestroy() {
         super.onDestroy();
         if(soundPool!=null){
   //退出时,自动释放资源
             soundPool.release();
   //将音乐池清空
             soundPool=null;
         }
     }


 }