在APP中播放音频文件

时间:2022-06-03 19:43:33

播放音频文件

     String myUriStr="/sdcard/sss.mp3";
           try {  
           mMediaPlayer.setDataSource(myUriStr);
           } catch (IOException e) {  
               e.printStackTrace();  
           }
           if(mMediaPlayer == null){  
                Log.d("Alarm", "该铃声不存在,请重新选择");
               return -1;  
               }  
       mMediaPlayer.setLooping(false);  
       try {  
           mMediaPlayer.prepare();  
       } catch (IllegalStateException e) {  
           e.printStackTrace();  
       } catch (IOException e) {  
           e.printStackTrace();  
       }  
 
       mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {  
           @Override  
           public void onPrepared(MediaPlayer mediaPlayer) {  
               mMediaPlayer.start();  
           }  
       }); 


AndroidManifest.xml中增加读取SD卡的权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>