首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

android Mediaplayer的容易程序

2013-01-11 
android Mediaplayer的简单程序程序的功能很简单、就是通过intent.setAction(Intent.ACTION_GET_CONTENT)i

android Mediaplayer的简单程序
程序的功能很简单、就是通过
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("Audio/*");
查询可以播放的音频文件、在onActivityResult中用Uri uri=intent.getData();获得路径、然后播放的功能、
问题:当我仅仅实现play功能的时候没发生问题、当我去掉stop和pause前面的斜杠的时候提示:
android Mediaplayer的容易程序

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>


在线等啊、、、、、android Mediaplayer的容易程序
[解决办法]
没明白你的问题,不过看你代码有问题,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);
    }
}


应该是你从新播放时没有释放资源

热点排行