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

android的音乐播放器 出了点有关问题不知道是什么原因,有时候出现死循环

2012-08-22 
android的音乐播放器 出了点问题不知道是什么原因,有时候出现死循环12-17 12:04:26.415: INFO/Stagefright

android的音乐播放器 出了点问题不知道是什么原因,有时候出现死循环

12-17 12:04:26.415: INFO/StagefrightPlayer(34): setDataSource('/mnt/sdcard/WonderGirls_TellMe.mp3')

12-17 12:04:26.415: ERROR/MediaPlayer(365): start called in state 2

12-17 12:04:26.415: ERROR/MediaPlayer(365): error (-38, 0)

12-17 12:04:26.415: ERROR/MediaPlayer(365): Error (-38,0)

12-17 12:04:26.425: WARN/InputManagerService(60): Starting input on non-focused client ???com.android.internal.view.IInputMethodClient$Stub$Proxy@43e388e8 (uid=10032 pid=365)

12-17 12:04:26.445: INFO/StagefrightPlayer(34): setDataSource('/mnt/sdcard/Trouble is a friend.mp3')

12-17 12:04:26.445: ERROR/MediaPlayer(365): start called in state 2

12-17 12:04:26.445: ERROR/MediaPlayer(365): error (-38, 0)

12-17 12:04:26.445: ERROR/MediaPlayer(365): Error (-38,0)

12-17 12:04:26.455: WARN/InputManagerService(60): Starting input on non-focused client ??com.android.internal.view.IInputMethodClient$Stub$Proxy@43e388e8 (uid=10032 pid=365)

12-17 12:04:26.545: INFO/StagefrightPlayer(34): setDataSource('/mnt/sdcard/sweet dream.mp3')

12-17 12:04:26.675: ERROR/MediaPlayer(365): start called in state 2

12-17 12:04:26.675: ERROR/MediaPlayer(365): error (-38, 0)

12-17 12:04:26.675: ERROR/MediaPlayer(365): Error (-38,0)

12-17 12:04:26.685: WARN/InputManagerService(60): Starting input on non-focused client ??com.android.internal.view.IInputMethodClient$Stub$Proxy@43e388e8 (uid=10032 pid=365)

12-17 12:04:26.705: INFO/StagefrightPlayer(34): setDataSource('/mnt/sdcard/WonderGirls_TellMe.mp3')

12-17 12:04:26.705: ERROR/MediaPlayer(365): start called in state 2

12-17 12:04:26.715: ERROR/MediaPlayer(365): error (-38, 0)

12-17 12:04:26.715: ERROR/MediaPlayer(365): Error (-38,0)

12-17 12:04:26.745: WARN/InputManagerService(60): Starting input on non-focused client ??com.android.internal.view.IInputMethodClient$Stub$Proxy@43e388e8 (uid=10032 pid=365)

12-17 12:04:26.755: INFO/StagefrightPlayer(34): setDataSource('/mnt/sdcard/Trouble is a friend.mp3')

12-17 12:04:26.765: ERROR/MediaPlayer(365): start called in state 2

?

package fly.mediaplayer;import java.io.File;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.Random;import android.app.Activity;import android.media.MediaPlayer;import android.os.Bundle;import android.os.Environment;import android.os.Handler;import android.os.Message;import android.view.View;import android.view.View.OnClickListener;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.ListView;import android.widget.SeekBar;import android.widget.SimpleAdapter;import android.widget.Toast;/** * 音乐播放器 * */public class MainActivity extends Activity {private ListView listView;private EditText title;SeekBar bar;// 进度条private Map<String, String> listpath = new HashMap<String, String>();// 存储音乐名称和绝对路径MediaPlayer mediaPlayer = new MediaPlayer();private Handler handler = new UIHandler();private boolean pause;AdapterView<?> parent;int position = -1;int model = 0;// 播放模式 :0 随机 :1 顺序:2 单曲循环List<Map<String, Object>> paths = null;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);// 获得视图listView = (ListView) this.findViewById(R.id.list);title = (EditText) this.findViewById(R.id.title);bar = (SeekBar) this.findViewById(R.id.lenghtBar);Button pause = (Button) this.findViewById(R.id.pause);Button reset = (Button) this.findViewById(R.id.reset);Button stop = (Button) this.findViewById(R.id.stop);Button down = (Button) this.findViewById(R.id.down);Button up = (Button) this.findViewById(R.id.up);Button model = (Button) this.findViewById(R.id.model);// 按钮添加事件pause.setOnClickListener(new ButtonClickListener());reset.setOnClickListener(new ButtonClickListener());stop.setOnClickListener(new ButtonClickListener());down.setOnClickListener(new ButtonClickListener());up.setOnClickListener(new ButtonClickListener());model.setOnClickListener(new ButtonClickListener());bar.setOnSeekBarChangeListener(new SeekBarChangeListener());paths = getList();// 获得音乐文件列表SimpleAdapter adapter = new SimpleAdapter(this, paths, R.layout.list,new String[] { "path", "lenght" }, new int[] { R.id.path,R.id.lenght });listView.setAdapter(adapter);listView.setOnItemClickListener(new ListOnClickListener());// 添加点击事件new Thread(new BarThread()).start();}/** * SeekBar重绘 * */private class UIHandler extends Handler {public void handleMessage(Message msg) {// 运行在主线程中int size = msg.getData().getInt("seek");bar.setProgress(size);}}/** * 进度条拖拉监听 * */private class SeekBarChangeListener implementsSeekBar.OnSeekBarChangeListener {@Overridepublic void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {}@Overridepublic void onStartTrackingTouch(SeekBar seekBar) {}@Overridepublic void onStopTrackingTouch(SeekBar seekBar) {if (mediaPlayer.isPlaying()) {mediaPlayer.stop();mediaPlayer.seekTo(seekBar.getProgress());play();}}}/** * 进度条线程 * */private class BarThread implements Runnable {public void run() {bar.setMax(mediaPlayer.getDuration());while (mediaPlayer.getCurrentPosition() <= mediaPlayer.getDuration()) {Message ms = new Message();ms.getData().putInt("seek", mediaPlayer.getCurrentPosition());handler.sendMessage(ms);try {Thread.sleep(2000);} catch (InterruptedException e) {e.printStackTrace();}}}}/** * 按钮事件监听器 * */private final class ButtonClickListener implements OnClickListener {@Overridepublic void onClick(View v) {if (position >= 0) {switch (v.getId()) {case R.id.pause:pause();break;case R.id.reset:reset();break;case R.id.stop:stop();break;case R.id.down:down();break;case R.id.up:up();break;case R.id.model:if (model == 0) {model = 1;((Button) v).setText(R.string.order);} else if (model == 1) {model = 2;((Button) v).setText(R.string.loop);} else {model = 0;((Button) v).setText(R.string.random);}break;}} else {Toast.makeText(getApplicationContext(), R.string.choice,Toast.LENGTH_LONG).show();}}/** * 下一首 * */private void down() {if (position + 1 < paths.size()) {position++;initMediaplayer(parent, position);play();} else {stop();Toast.makeText(getApplicationContext(), R.string.last,Toast.LENGTH_LONG).show();}}/** * 上一首 * */private void up() {if (position == 0) {stop();Toast.makeText(getApplicationContext(), R.string.first,Toast.LENGTH_LONG).show();} else {position = position - 1;initMediaplayer(parent, position);play();}}}/** * ListView点击事件监听器 * */private final class ListOnClickListener implements OnItemClickListener {@Overridepublic void onItemClick(AdapterView<?> parent, View view, int position,long id) {initMediaplayer(parent, position);mediaPlayer.setOnCompletionListener(new CompletionListener(parent,position));setViewAndPosition(parent, position);play();// 播放}}/** * 存储当前的视图和位置 * */private void setViewAndPosition(AdapterView<?> parent, int position) {this.parent = parent;this.position = position;}/** * 获得随即播放点 * */private int getRandom(AdapterView<?> parent, int position) {position = new Random().nextInt(parent.getChildCount());return position;}/** * 获得顺序播放点 * */private int getDown(AdapterView<?> parent, int position) {position += 1;if (position > parent.getChildCount() - 1) {position = 0;}return position;}/** * 播放完后 0.随即播放 1.顺序播放 2.单曲循环 * */private class CompletionListener implementsMediaPlayer.OnCompletionListener {private int position;private AdapterView<?> parent;public CompletionListener(AdapterView<?> parent, int position) {super();this.position = position;this.parent = parent;}@Overridepublic void onCompletion(MediaPlayer mp) {switch (model) {case 0:position = getRandom(parent, position);break;case 1:position = getDown(parent, position);break;case 2:break;}setViewAndPosition(parent, position);initMediaplayer(parent, position);play();}}/** * 初始化 MediaPlayer * */private void initMediaplayer(AdapterView<?> parent, int position) {@SuppressWarnings("unchecked")Map<String, String> map = (Map<String, String>) parent.getItemAtPosition(position);String path = map.get("path");// 获得触发事件的Itemtitle.setText(path);// 设置标题String realPath = listpath.get(path);// 获得绝对路径mediaPlayer.reset();try {mediaPlayer.setDataSource(realPath);} catch (Exception e) {e.printStackTrace();}}/** * 重置 * */private void reset() {if (mediaPlayer.isPlaying()) {mediaPlayer.seekTo(0);} else {if (title != null)play();}}/** * 暂停 * */private void pause() {if (mediaPlayer.isPlaying()) {mediaPlayer.pause();pause = true;} else {play();}}/** * 停止 * */private void stop() {if (mediaPlayer.isPlaying()) {mediaPlayer.stop();pause = false;}}/** * 播放 * */private void play() {try {if (!pause) {mediaPlayer.prepare();}mediaPlayer.start();} catch (Exception e) {e.printStackTrace();}}/** * 获取音乐列表 * */private List<Map<String, Object>> getList() {File file = new File(Environment.getExternalStorageDirectory().toString());// 获得SDCardFile[] files = file.listFiles();int lenght = 0;int m = 0;int s = 0;List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();// 获得所有的音乐文件for (File filepath : files) {if (filepath.getName().endsWith(".mp3")) {Map<String, Object> lists = new HashMap<String, Object>();try {mediaPlayer.reset();mediaPlayer.setDataSource(filepath.toString());mediaPlayer.prepare();lenght = mediaPlayer.getDuration();lenght = lenght / 1000;m = lenght / 60;s = lenght - m * 60;} catch (Exception e) {e.printStackTrace();}lists.put("path", filepath.getName());lists.put("lenght",String.format("%02d", m) + ":"+ String.format("%02d", s));list.add(lists);listpath.put(filepath.getName(), filepath.toString());}}return list;}}

?

1 楼 zhangxpower 2011-08-05   我也遇到了这个问题。。。不知道怎么解决 。。还是说 要用mediaplayer之前要在 manifest.xml中申请 权限啊?

抱着个错:start called in state 2

热点排行