媒体播放器播放两次 [英] Mediaplayer plays twice

查看:174
本文介绍了媒体播放器播放两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个媒体播放器,但是当选择另一个文件继续打老文件,新的之一,所以在播放两个文件同时这里是我的onCreate方法

 私人的MediaPlayer媒体播放器=新的MediaPlayer();
私人处理程序处理程序=新的处理程序();
@覆盖
保护无效的onCreate(捆绑savedInstanceState){// TODO自动生成方法存根
super.onCreate(savedInstanceState);
的setContentView(R.layout.songplaying);//获取我们的额外意图从
 束名称= getIntent()getExtras()。//获取特定数据 PATH = names.getString(pathkeyword);
 //开始播放器
 尝试{
    playAudio(路径);}赶上(例外五){
   e.printStackTrace();

}

和这是播放的声音的方法

 私人无效playAudio(字符串URL)抛出异常{  mediaplayer.release();
  mediaplayer.setDataSource(URL);
  媒体播放器prepare()。
  mediaplayer.start();


解决方案

当你开始播放歌曲,检查是打还是不停止它,如果它正在播放。

 如果(player.isPlaying())
      {
        mediaplayer.stop();
       }
        mediaplayer.reset();  mediaplayer.setDataSource(URL);
  媒体播放器prepare()。
  mediaplayer.start();

无需释放player.player.release()时,玩家不再需要时才使用。

和必须使用的stop()和release()方法,每当活动destroys.Otherwise让很多玩家都在后台运行。

I have a media player but when another file is selected it continues to play the old file and new one so it is playing two files at once here is my onCreate method

private MediaPlayer mediaplayer = new MediaPlayer();
private Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.songplaying);

// Getting Our Extras From Intent
 Bundle names = getIntent().getExtras();

// Getting Specific Data

 path = names.getString("pathkeyword");


 //Start Player
 try {
    playAudio(path);

} catch (Exception e) {
   e.printStackTrace();

}

and this is the method that plays the audio

 private void playAudio(String url) throws Exception{

  mediaplayer.release();
  mediaplayer.setDataSource(url);
  mediaplayer.prepare();
  mediaplayer.start();

解决方案

When you are start to play the song ,check it is playing or not and stop it if it is currently playing.

     if(player.isPlaying())
      {  
        mediaplayer.stop();
       } 
        mediaplayer.reset();

  mediaplayer.setDataSource(url);
  mediaplayer.prepare();
  mediaplayer.start();

no need to release the player.player.release() used only when player no longer needed .

And you have to use stop() and release() methods whenever activity destroys.Otherwise so many players are running in background.

这篇关于媒体播放器播放两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆