Example usage for android.media MediaPlayer start

List of usage examples for android.media MediaPlayer start

Introduction

In this page you can find the example usage for android.media MediaPlayer start.

Prototype

public void start() throws IllegalStateException 

Source Link

Document

Starts or resumes playback.

Usage

From source file:util.mediamanager.PlaylistUtils.java

public static void PlayMusic(String DataStream) {
    MediaPlayer mpObject = new MediaPlayer();
    if (DataStream == null)
        return;/*from  ww  w .j av a 2s .c om*/
    try {
        mpObject.setDataSource(DataStream);
        mpObject.prepare();
        mpObject.start();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * Play an alarm sound on the device/*  w  w w.j  a va2  s  . c  o m*/
 *
 * @param context The context
 * @return MediaPlayer
 */
public static MediaPlayer playAlarmSound(Context context) {
    MediaPlayer mediaPlayer = new MediaPlayer();
    try {
        mediaPlayer.setDataSource(context, getAlarmUri());
        final AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
            mediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
            mediaPlayer.prepare();
            mediaPlayer.start();
        }

        return mediaPlayer;
    } catch (IOException e) {
        return null;
    }
}

From source file:org.artoolkit.ar.samples.ARSimple.ARSimple.java

private static void play(final MediaPlayer media, CountDownTimer timer) {

    if (media != null) {
        media.start();

        /*t1 = new CountDownTimer(30000, 1000) {
           @Override//from  w  w  w.j a v a2  s . c o m
           public void onTick(long l) {
              //Toast.makeText(app, "seconds remaining: " + l / 1000, Toast.LENGTH_SHORT).show();
           }
                
           @Override
           public void onFinish() {
              //media.start();
           }
        }.start();
        */
        //timer = app.new MyTimer(30000, 1000);
    } else {
        Log.w("MediaPlayer : ", " l'instance n'a pas t initialise");
    }
}

From source file:com.metinkale.prayerapp.vakit.AlarmReceiver.java

public static MediaPlayer play(Context c, String sound) throws IOException {
    Uri uri = Uri.parse(sound);/*from w  ww. j a v  a2  s .  c o  m*/

    MediaPlayer mp = new MediaPlayer();
    mp.setLooping(false);
    mp.setDataSource(c, uri);
    mp.setAudioStreamType(getStreamType(c));

    mp.prepare();
    mp.start();
    return mp;
}

From source file:com.gm.goldencity.util.Utils.java

/**
 * Play a sound/*from  w  w  w .j a  v a 2 s  . c om*/
 *
 * @param context Application context
 * @param rawID   Raw integer id in resource
 */
public static void playSound(Context context, int rawID) {
    MediaPlayer mp = MediaPlayer.create(context, rawID);
    mp.start();
}

From source file:mobi.sherif.example.imageuploader.ImageUploaderActivitySupportVideo.java

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.choosevideo);
    uploadEngine = new MediaEngine.Builder(this, savedInstanceState).setLoadingListener(this).build();
    ((VideoView) findViewById(R.id.video)).setOnPreparedListener(new OnPreparedListener() {
        @Override//  ww  w  . j a  v  a  2  s.co  m
        public void onPrepared(MediaPlayer mp) {
            mp.start();
        }
    });
}

From source file:org.fdroid.enigtext.notifications.MessageNotifier.java

private static void sendInThreadNotification(Context context) {
    try {/*from  w  ww . ja  v a2s. c  om*/
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);

        if (!sp.getBoolean(ApplicationPreferencesActivity.IN_THREAD_NOTIFICATION_PREF, true)) {
            return;
        }

        String ringtone = sp.getString(ApplicationPreferencesActivity.RINGTONE_PREF, null);

        if (ringtone == null)
            return;

        Uri uri = Uri.parse(ringtone);
        MediaPlayer player = new MediaPlayer();
        player.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);
        player.setDataSource(context, uri);
        player.setLooping(false);
        player.setVolume(0.25f, 0.25f);
        player.prepare();

        final AudioManager audioManager = ((AudioManager) context.getSystemService(Context.AUDIO_SERVICE));

        audioManager.requestAudioFocus(null, AudioManager.STREAM_NOTIFICATION,
                AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK);

        player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                audioManager.abandonAudioFocus(null);
            }
        });

        player.start();
    } catch (IOException ioe) {
        Log.w("MessageNotifier", ioe);
    }
}

From source file:mobi.sherif.example.imageuploader.ImageUploaderFragmentSupportVideo.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.choosevideo, container, false);
    mProgress = v.findViewById(R.id.progress);
    mVideoView = (VideoView) v.findViewById(R.id.video);
    mTextView = (TextView) v.findViewById(R.id.textvideo);
    v.findViewById(R.id.button).setOnClickListener(new OnClickListener() {
        @Override/*from   ww w  . j  a  v  a 2 s  .c o m*/
        public void onClick(View v) {
            uploadEngine.performVideoAsk(R.string.app_name, R.string.newvideo, R.string.oldvideo,
                    R.string.choosevideo, R.drawable.ic_launcher);
        }
    });
    ((VideoView) v.findViewById(R.id.video)).setOnPreparedListener(new OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            mp.start();
        }
    });
    return v;
}

From source file:com.dudka.rich.streamingmusicplayer.ServiceMusicPlayer.java

@Override
public void onPrepared(MediaPlayer mp) {
    mp.start();
    mp.setOnCompletionListener(this);
    sendLocalBroadcast(MainActivity.PLAYER_STARTED);
}

From source file:com.securecomcode.text.notifications.MessageNotifier.java

private static void sendInThreadNotification(Context context) {
    try {// w  ww.  j av  a  2s .  c o  m
        if (!TextSecurePreferences.isInThreadNotifications(context)) {
            return;
        }

        String ringtone = TextSecurePreferences.getNotificationRingtone(context);

        if (ringtone == null)
            return;

        Uri uri = Uri.parse(ringtone);
        MediaPlayer player = new MediaPlayer();
        player.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);
        player.setDataSource(context, uri);
        player.setLooping(false);
        player.setVolume(0.25f, 0.25f);
        player.prepare();

        final AudioManager audioManager = ((AudioManager) context.getSystemService(Context.AUDIO_SERVICE));

        audioManager.requestAudioFocus(null, AudioManager.STREAM_NOTIFICATION,
                AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK);

        player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                audioManager.abandonAudioFocus(null);
            }
        });

        player.start();
    } catch (IOException ioe) {
        Log.w("MessageNotifier", ioe);
    }
}