程序的功能很简单、就是通过
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("Audio/*");
查询可以播放的音频文件、在onActivityResult中用Uri uri=intent.getData();获得路径、然后播放的功能、
问题:当我仅仅实现play功能的时候没发生问题、当我去掉stop和pause前面的斜杠的时候提示:public class MainActivity extends Activity implements MediaPlayer.OnCompletionListener {
private ImageButton play,pause,stop;
MediaPlayer player=new MediaPlayer();
    private String path="";
    private Button sousuo;
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        play=(ImageButton)findViewById(R.id.play);
        pause=(ImageButton)findViewById(R.id.pause);
        stop=(ImageButton)findViewById(R.id.stop);
        sousuo=(Button)findViewById(R.id.sousuo);
        play.setEnabled(true);
        pause.setEnabled(false);
        stop.setEnabled(false);
        play.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
playing();
}
});
//        stop.setOnClickListener(new OnClickListener() {
//
// @Override
// public void onClick(View v) {
// // TODO Auto-generated method stub
// play.setEnabled(true);
// stop.setEnabled(false);
// pause.setEnabled(false);
// if(player.isPlaying()){
// player.stop();
// //player.reset();
// try {
// player.prepare();
// player.seekTo(0);
// } catch (IllegalStateException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// }
// }
// });
//        pause.setOnClickListener(new OnClickListener() {
//
// @Override
// public void onClick(View v) {
// // TODO Auto-generated method stub
// play.setEnabled(true);
// stop.setEnabled(true);
// pause.setEnabled(false);
// player.pause();
// try {
// player.prepare();
// } catch (IllegalStateException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// }
// });
        sousuo.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("Audio/*");
startActivityForResult(intent, 0);
}
});
      
}
public void onActivityResult(int i,int b,Intent intent){
if(intent!=null){
Uri uri=intent.getData();
String p=uri.toString().substring(52);
System.out.println(p);
path=p;
}
}
public void playing(){

if(path!=null&& !path.equals("")){
play.setEnabled(false);
stop.setEnabled(true);
pause.setEnabled(true);

  try {
player.reset();
File file=new File(path);
FileInputStream fis=new FileInputStream(file);
player.setDataSource(fis.getFD());
System.out.println(fis.getFD().toString());
player.prepare();
player.start();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}
@Override
public void onCompletion(MediaPlayer arg0) {
// TODO Auto-generated method stub
player.release();
play.setEnabled(true);
stop.setEnabled(false);
pause.setEnabled(false);

}
}
XML文件:<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:orientation="vertical"
    >    <TextView
     android:id="@+id/text01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="迷你音乐播放器"
        tools:context=".MainActivity"
        />
    <LinearLayout 
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:orientation="horizontal" 
     >
     <ImageButton 
     android:id="@+id/play"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:src="@drawable/play"
     />
     <ImageButton 
     android:id="@+id/pause"
     android:layout_height="wrap_content"
     android:layout_width="wrap_content"
     android:src="@drawable/pause"
     />
     <ImageButton 
     android:id="@+id/stop"
     android:layout_height="wrap_content"
     android:layout_width="wrap_content"
     android:src="@drawable/stop"
     />
    </LinearLayout>
    <Button 
     android:id="@+id/sousuo"
     android:layout_height="wrap_content"
     android:layout_width="wrap_content"
     android:text="选择歌曲"
     android:layout_gravity="right"
     /></LinearLayout>在线等啊、、、、、

解决方案 »

  1.   

    你是当点击stop或者pause出错? if(player.isPlaying()){ 这个判断出错了。
      

  2.   

    不是点击、、、是直接去掉斜杠在运行就错误了,应该是在play里面出的错、能执行查询功能。
    刚刚接触Mediaplayer。。有点混乱。
      

  3.   

    没明白你的问题,不过看你代码有问题,playing方法改一下
    public void playing(){
         
        if(path!=null&& !path.equals("")){
            play.setEnabled(false);
            stop.setEnabled(true);
            pause.setEnabled(true);
             if(player!=null){
               player.reset();
               player.release();
               player=null
             }
              try {
                   player=new MediaPlayer();
                    File file=new File(path);
                    FileInputStream fis=new FileInputStream(file);
                    player.setDataSource(fis.getFD());
                    System.out.println(fis.getFD().toString());
                    player.prepare();
                    player.start();
                } catch (IllegalArgumentException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IllegalStateException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                 
        }
    }
        @Override
        public void onCompletion(MediaPlayer arg0) {
            // TODO Auto-generated method stub
            player.release();
            play.setEnabled(true);
            stop.setEnabled(false);
            pause.setEnabled(false);
        }
    }
    应该是你从新播放时没有释放资源