本文介绍了播放listview中的歌曲到android中的媒体播放器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 嗨朋友们, 我一直在开发一个应用程序,它是在listview中存储歌曲并在我们的媒体播放器中播放相应的歌曲。我生成了一个代码,但一个问题是我可以一次播放一首歌。那就是如果我选择一首要播放的歌曲,就会播放。然后我选择其他歌曲同时播放两首歌。我把我的歌曲从数据库带到Listview。你可以帮我解决我的问题。 我的代码是 公共类ListDownloadedItem扩展活动{ BookModel bm; private int currentPosition = 0; BookAdapter bookadpt; MediaPlayer mp; ListView列表簿; // BookAdapter bookadpt; / **该活动首先创建。 * / @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.listdownbook); listbooks =(ListView)findViewById(R.id.down); bookadpt = new BookAdapter(ListDownloadedItem.this); // bookadpt.createDatabase(); bookadpt.open(); final ArrayList< bookmodel> values = bookadpt.selectAll(); ArrayList< bookmodel> bookModels = new ArrayList< bookmodel>(); listbooks.setAdapter(新的ArrayAdapter< bookmodel>(this, android.R.layout.simple_list_item_1,值)); bookadpt.close(); listbooks.setTextFilterEnabled(true); listbooks.setOnItemClickListener(new OnItemClickListener(){ @Override public void onItemClick(AdapterView int position,long id ){ bm = values.get(position); playSong(Utilis.DB_PATH + bm.getPartName()); } }); } private void playSong(String songPath){ boolean flag = false; mp = new MediaPlayer( ); // if(flag == true) mp.stop(); mp .reset(); mp.release(); 尝试{ mp.setDataSource(songPath ); mp.prepare(); mp.start(); } catch(IllegalArgumentException e){ // TODO自动生成的捕获块 e.printStackTrace(); } catch(SecurityException e){ // TODO自动生成的捕获块 e.printStackTrace(); } catch(IllegalStateException e){ // TODO自动生成的捕获块 e.printStackTrace(); } catch(IOException e){ // TODO自动生成的捕获块 e.printStackTrace(); } } @Override protected void onDestroy(){ super.onDestroy(); mp.stop(); mp.reset(); mp.release(); } }Hi Friends,i have been developing an application that is store songs in listview and play corresponding songs in our media player. i generated a code, but one problem is that i can to play a song at a time. that is if i select one song to play , that will playing. then i selected other songs both songs are playing concurrently.I took my songs from database to Listview.Can you help me to solve my problem.My Code ispublic class ListDownloadedItem extends Activity{BookModel bm;private int currentPosition=0; BookAdapter bookadpt; MediaPlayer mp;ListView listbooks;//BookAdapter bookadpt; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.listdownbook); listbooks = (ListView) findViewById(R.id.down); bookadpt = new BookAdapter(ListDownloadedItem.this); // bookadpt.createDatabase(); bookadpt.open(); final ArrayList<bookmodel> values = bookadpt.selectAll(); ArrayList<bookmodel>bookModels=new ArrayList<bookmodel>(); listbooks.setAdapter(new ArrayAdapter<bookmodel>(this, android.R.layout.simple_list_item_1, values)); bookadpt.close(); listbooks.setTextFilterEnabled(true); listbooks.setOnItemClickListener(new OnItemClickListener() {@Overridepublic void onItemClick(AdapterViewint position, long id) {bm=values.get(position);playSong(Utilis.DB_PATH + bm.getPartName());}}); } private void playSong(String songPath) { boolean flag=false; mp=new MediaPlayer(); //if(flag==true) mp.stop(); mp.reset(); mp.release(); try {mp.setDataSource(songPath); mp.prepare(); mp.start();} catch (IllegalArgumentException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (SecurityException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalStateException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} } @Override protected void onDestroy() { super.onDestroy(); mp.stop(); mp.reset(); mp.release(); } }推荐答案当你关闭你的活动时..只需添加一个简单的行代码。 finish();when you close your activity .. just add a simple line code.finish(); 这篇关于播放listview中的歌曲到android中的媒体播放器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-30 23:28