2014-01-16 2 views
0

У меня есть приложение, которое воспроизводит mp3 с URL-адреса. Проблема в том, что приостановление, остановка & резюме не работает должным образом. Я пробовал весь код и рассматривал каждый подходящий вопрос. но ничего works.so пожалуйста, дайте решение для этого, большое спасибоКак приостановить и остановить поток mp3 url

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); 

И попробовать это.

Update:

Попробуйте кнопку слушателя на этом пути:

Play1.setOnClickListener(new View.OnClickListener() { 

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

и для всех кнопок.

Ваша проблема с кодом находится здесь. Вы только что определили свой объект MediaPlayer глобально, то почему r u создал еще один объект New MediaPlayer в ваш метод PlayOnlineUrl(). Вы просто добавляете, как показано ниже:

mediaPlayer = new MediaPlayer(); 
+0

Я попробовал это, но снова пауза, резюме и остановка не работают. sp пожалуйста, дайте предложение о паузе, прекратите работать должным образом, спасибо – user3129072

+0

@ user3129072 см. мое обновление. –

+0

спасибо, это действительно работает ........ – user3129072

Смежные вопросы