2014-01-16 48 views
0

我已经建立了一个应用程序,从MP3播放的MP3。问题是,暂停,停止&简历无法正常工作。我尝试了所有的代码并查看了每个相关的问题。但没有works.so请给这个解决方案,三江源如何暂停和停止MP3的网址流

import java.io.IOException; 

import java.util.Timer; 
import java.util.TimerTask; 
me 
import android.app.Activity; 
import android.media.AudioManager; 
import android.media.MediaPlayer; 
import android.media.MediaPlayer.OnErrorListener; 
import android.media.MediaPlayer.OnPreparedListener; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.SeekBar; 

public class MainActivity extends Activity implements OnClickListener { 
    private SeekBar mBar; 
    MediaPlayer mediaPlayer; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     findViewById(R.id.playOnline).setOnClickListener(this); 
     findViewById(R.id.playOnline2).setOnClickListener(this); 
     findViewById(R.id.pause).setOnClickListener(this); 
     findViewById(R.id.button3).setOnClickListener(this); 
     findViewById(R.id.stop).setOnClickListener(this); 
     mBar = (SeekBar) findViewById(R.id.progress); 
    } 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 
    @Override 
    public void onClick(View v) { 
     switch (v.getId()) { 


     case R.id.playOnline: 
      PlayOnlineUrl(); 
      break; 

     case R.id.playOnline2: 
      PlayOnlineUrl2(); 
      break; 
     case R.id.pause: 
      if (mediaPlayer != null) { 
       pause = true; 
       mediaPlayer.pause(); 
      } 
      break; 
     case R.id.button3: 
      if (mediaPlayer != null) { 
       pause = false; 
       mediaPlayer.start(); 
      } 
      break; 
     case R.id.stop: 
      if (mediaPlayer != null) { 
       mediaPlayer.stop(); 
       mediaPlayer = null; 
       timer.cancel(); 
       task.cancel(); 
      } 
      break; 

     default: 
      break; 
     } 
    } 



    @Override 
    public void onBackPressed() { 
     if(mediaPlayer.isPlaying()) { 
      mediaPlayer.stop(); 
     } 
     finish(); 
    } 

    private void PlayOnlineUrl2() { 
String url = "http://dj-videos.us/Music/XclusiveSinGleTrack/320%20Kbps/November%202013/Yo%20Yo%20Honey%20Singh%20-%20Blue%20Eyes-[DJKANG.Com].mp3"; // your URL here 

     MediaPlayer mediaPlayer = new MediaPlayer(); 
     mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); 
     try { 
      mediaPlayer.setDataSource(url); 
      } 

     catch (IllegalArgumentException e) { 
      e.printStackTrace(); 
     } catch (SecurityException e) { 
      e.printStackTrace(); 
     } catch (IllegalStateException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     mediaPlayer.prepareAsync(); 
     // You can show progress dialog here untill it prepared to play 
     mediaPlayer.setOnPreparedListener(new OnPreparedListener() { 
      @Override 
      public void onPrepared(MediaPlayer mp) { 
       // Now dismis progress dialog, Media palyer will start playing 
       mp.start(); 
      } 
     }); 
     mediaPlayer.setOnErrorListener(new OnErrorListener() { 
      @Override 
      public boolean onError(MediaPlayer mp, int what, int extra) { 
       // dissmiss progress bar here. It will come here when 
       // MediaPlayer 
       // is not able to play file. You can show error message to user 
       return false; 
      } 
     }); 
     } 
    public void onPrepared(MediaPlayer mp) { 
     // Now dismis progress dialog, Media palyer will start playing 
     mp.start(); 
     int duration = mp.getDuration(); 
     int period = duration/1000; 
     task = new TimerTask() { 

      @Override 
      public void run() { 
       mBar.post(new Runnable() { 
        @Override 
        public void run() { 
         if (!pause) { 
          progresss++; 
          mBar.setProgress(progresss); 
         } 
        } 
       }); 
      } 
     }; 
     timer = new Timer(); 
     timer.schedule(task, 0, period * 10); 

    } 



private boolean pause = false; 
int progresss = 0; 
private TimerTask task; 
private Timer timer = null; 

    private void PlayOnlineUrl() { 
     String url = "http://starmirchi.com/upload_file/Mp3%20Songs/3605/Yo!%20Yo!%20Honey%20Singh%20_%20Blue%20Eyes%20(DJ%20AKS%20Remix)-(StarMirchi.com).mp3"; // your URL here 

     MediaPlayer mediaPlayer = new MediaPlayer(); 
     mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); 
     try { 
      mediaPlayer.setDataSource(url); 
      } 

     catch (IllegalArgumentException e) { 
      e.printStackTrace(); 
     } catch (SecurityException e) { 
      e.printStackTrace(); 
     } catch (IllegalStateException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     mediaPlayer.prepareAsync(); 
     // You can show progress dialog here untill it prepared to play 
     mediaPlayer.setOnPreparedListener(new OnPreparedListener() { 
      @Override 
      public void onPrepared(MediaPlayer mp) { 
       // Now dismis progress dialog, Media palyer will start playing 
       mp.start(); 
      } 
     }); 
     mediaPlayer.setOnErrorListener(new OnErrorListener() { 
      @Override 
      public boolean onError(MediaPlayer mp, int what, int extra) { 
       // dissmiss progress bar here. It will come here when 
       // MediaPlayer 
       // is not able to play file. You can show error message to user 
       return false; 
      } 
     }); 
     } 


} 

这是错误日志

01-15 23:27:18.257: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/KeypressStandard.ogg 
    01-15 23:27:18.267: E/SoundPool(381): error loading /system/media/audio/ui/KeypressSpacebar.ogg 
    01-15 23:27:18.267: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/KeypressSpacebar.ogg 
    01-15 23:27:18.267: E/SoundPool(381): error loading /system/media/audio/ui/KeypressDelete.ogg 
    01-15 23:27:18.267: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/KeypressDelete.ogg 
    01-15 23:27:18.277: E/SoundPool(381): error loading /system/media/audio/ui/KeypressReturn.ogg 
    01-15 23:27:18.277: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/KeypressReturn.ogg 
    01-15 23:27:18.277: E/SoundPool(381): error loading /system/media/audio/ui/KeypressInvalid.ogg 
    01-15 23:27:18.277: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/KeypressInvalid.ogg 
    01-15 23:27:18.277: W/AudioService(381): onLoadSoundEffects(), Error -1 while loading samples 
    01-15 23:27:18.367: E/AudioSink(52): received unknown event type: 1 inside CallbackWrapper ! 
    01-15 23:27:20.097: E/SoundPool(381): error loading /system/media/audio/ui/Effect_Tick.ogg 
    01-15 23:27:20.097: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg 
    01-15 23:27:20.107: E/SoundPool(381): error loading /system/media/audio/ui/Effect_Tick.ogg 
    01-15 23:27:20.107: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg 
    01-15 23:27:20.127: E/SoundPool(381): error loading /system/media/audio/ui/Effect_Tick.ogg 
    01-15 23:27:20.127: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg 
    01-15 23:27:20.137: E/SoundPool(381): error loading /system/media/audio/ui/Effect_Tick.ogg 
    01-15 23:27:20.137: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg 
    01-15 23:27:20.137: E/SoundPool(381): error loading /system/media/audio/ui/Effect_Tick.ogg 
    01-15 23:27:20.197: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg 
    01-15 23:27:20.197: E/SoundPool(381): error loading /system/media/audio/ui/KeypressStandard.ogg 
    01-15 23:27:20.207: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/KeypressStandard.ogg 
    01-15 23:27:20.207: E/SoundPool(381): error loading /system/media/audio/ui/KeypressSpacebar.ogg 
    01-15 23:27:20.207: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/KeypressSpacebar.ogg 
    01-15 23:27:20.207: E/SoundPool(381): error loading /system/media/audio/ui/KeypressDelete.ogg 
    01-15 23:27:20.207: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/KeypressDelete.ogg 
    01-15 23:27:20.217: E/SoundPool(381): error loading /system/media/audio/ui/KeypressReturn.ogg 
    01-15 23:27:20.217: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/KeypressReturn.ogg 
    01-15 23:27:20.217: E/SoundPool(381): error loading /system/media/audio/ui/KeypressInvalid.ogg 
    01-15 23:27:20.217: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/KeypressInvalid.ogg 
+0

您的声音是否播放。 –

+0

声音正在播放,但暂停,恢复,停止不起作用 – user3129072

+0

我在您的代码中遇到问题并检查我的新更新答案。 –

回答

2

如果我没有错,那么你只需更换这一点:

findViewById(R.id.playOnline).setOnClickListener(this); 
    findViewById(R.id.playOnline2).setOnClickListener(this); 
    findViewById(R.id.pause).setOnClickListener(this); 
    findViewById(R.id.button3).setOnClickListener(this); 
    findViewById(R.id.stop).setOnClickListener(this); 

用以下方式:全局定义所有按钮

Button Play1,Play2,Pause,button2,stop; 

及以下加入到OnCreate()

Play1=(Button)findViewById(R.id.playOnline); 
    Play2=(Button)findViewById(R.id.playOnline2); 
    Pause=(Button)findViewById(R.id.pause); 
    button2=(Button)findViewById(R.id.button3); 
    stop=(Button)findViewById(R.id.stop); 

    Play1.setOnClickListener(this); 
    Play2.setOnClickListener(this); 
    Pause.setOnClickListener(this); 
    button2.setOnClickListener(this); 
    stop.setOnClickListener(this); 

,并尝试了这一点。

更新:

尝试在这条路上你的按钮侦听:

Play1.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
      //Here your code 
     } 
    }); 

,并为所有的按钮。

你的代码问题在这里。您刚刚在全局范围内定义了MediaPlayer对象,那么为什么您将另一个New MediaPlayer对象创建为您的PlayOnlineUrl()方法。您只需添加如下:

mediaPlayer = new MediaPlayer(); 
+0

我试过这个,但再次暂停,恢复和停止不起作用。 sp请给出关于暂停的建议,停止正常工作,谢谢 – user3129072

+0

@ user3129072看到我的更新。 –

+0

谢谢,它真的在工作........ – user3129072