Advertisement
aunkang

Sound.java

May 18th, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.81 KB | None | 0 0
  1. package com.example.analogy.fragment.Fragment;
  2.  
  3. import android.media.AudioManager;
  4. import android.media.MediaPlayer;
  5.  
  6. import java.io.IOException;
  7.  
  8. /**
  9.  * Created by Analogy on 4/28/2017.
  10.  */
  11.  
  12. public class Sound {
  13.  
  14.  
  15.  
  16.     //private String url = "http://192.168.1.6/dashboard/senior-project/uploads/audio/3_sound1.mp3"; // your URL here
  17.     MediaPlayer mediaPlayer;
  18.     private boolean dup = false;
  19.     private int length=0;
  20.     public void prepareToPlay(String url){
  21.         dup = false;
  22.         String url2 = "http://192.168.1.6/dashboard/senior-project/uploads/audio/" + url;
  23.         mediaPlayer = new MediaPlayer();
  24.         mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
  25.         try {
  26.             mediaPlayer.setDataSource(url2);
  27.             mediaPlayer.prepare(); //
  28.         } catch (IOException e) {
  29.             e.printStackTrace();
  30.         }
  31.  
  32.     }
  33.  
  34.     public void restart(){
  35.         if(dup){
  36.             dup = false;
  37.             mediaPlayer.seekTo(length);
  38.             mediaPlayer.start();
  39.             length = 0;
  40.         }
  41.         else{
  42.             dup = true;
  43.         }
  44.     }
  45.     public void pause() throws IOException {
  46.             if(mediaPlayer.isPlaying()){
  47.                 mediaPlayer.pause();
  48.                 length = mediaPlayer.getCurrentPosition();
  49.             }else{
  50.  
  51.             }
  52.  
  53.  
  54.  
  55.     }
  56.     public void play(){
  57.         mediaPlayer.start();
  58.     }
  59.     public void playForFifth(){
  60.         if(mediaPlayer.isPlaying()){
  61.             length = mediaPlayer.getCurrentPosition();
  62.             mediaPlayer.pause();
  63.         }else{
  64.             mediaPlayer.seekTo(length);
  65.             mediaPlayer.start();
  66.         }
  67.     }
  68.     public void stop(){
  69.         if(mediaPlayer.isPlaying()){
  70.             mediaPlayer.stop();
  71.         }
  72.         length = 0;
  73.  
  74.     }
  75.  
  76.  
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement