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:bala.padio.Player.java

/** Called when MediaPlayer is ready */
public void onPrepared(MediaPlayer player) {
    player.start();
    setStatus(true);
}

From source file:mozilla.voicejam.singwithme.CameraActivity.java

void play() {
    Uri uri = Uri.parse(filePath);//from  w ww  .  j  ava 2s  .co m
    MediaPlayer mediaPlayer = MediaPlayer.create(this, uri);
    mediaPlayer.start();
    mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer MP) {
            MP.release();
        }
    });
}

From source file:de.hshannover.f4.trust.ironcontrol.logic.ResultNotificationManager.java

private void ton() {
    //      Field[] fields = R.raw.class.getFields();
    //      for (Field field : fields) {
    //         int rid = 0;
    //         try {
    //            rid = field.getInt(field);
    //         } catch (IllegalArgumentException e) {
    //            e.printStackTrace();
    //         } catch (IllegalAccessException e) {
    //            e.printStackTrace();
    //         }// www.  j a  v  a 2 s  .com
    //
    //         // Use that if you just need the file name
    //         String filename = field.getName();
    //
    //         System.out.println("rid=" + rid +" name=" + filename);
    //      }
    MediaPlayer test = MediaPlayer.create(context, R.raw.star_struck);
    test.start();
    //      get
}

From source file:com.msohm.blackberry.samples.bdvideoplayback.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //Initialize BlackBerry Dynamics.
    GDAndroid.getInstance().activityInit(this);
    setContentView(R.layout.activity_main);

    copyButton = (Button) findViewById(R.id.copyButton);

    //The copy button is used to copy an existing video file you have into the secure
    //BlackBerry Dynamics file system.  This only needs to be run once.
    copyButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            //Ensure we have permission to read the file being copied.
            int permissionCheck = ContextCompat.checkSelfPermission(v.getContext(),
                    Manifest.permission.READ_EXTERNAL_STORAGE);

            if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
                //Permission isn't set.  Request it.
                ActivityCompat.requestPermissions(MainActivity.this,
                        new String[] { Manifest.permission.READ_EXTERNAL_STORAGE },
                        MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
            } else {
                copyFile();//from ww w . j  a v  a2  s.co m
            }
        }
    });

    playButton = (Button) findViewById(R.id.playButton);

    //Initializes the MediaPlayer.
    playButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            try {
                if (!isPaused) {
                    BDMediaDataSource source = new BDMediaDataSource(FILENAME);
                    mp.setDataSource(source);
                    mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                        @Override
                        public void onPrepared(MediaPlayer mp) {
                            mp.start();
                        }
                    });
                    mp.prepareAsync();
                } else {
                    mp.start();
                }
                isPaused = false;
            } catch (IOException ioex) {
            }

        }
    });

    SurfaceView surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
    SurfaceHolder holder = surfaceView.getHolder();
    holder.addCallback(this);
    mp = new MediaPlayer();
    isPaused = false;
}

From source file:com.ecoplayer.beta.MusicService.java

/** Called when MediaPlayer is ready */
public void onPrepared(MediaPlayer player) {
    player.start();
    sendMusicUpdateToActivity(true, true);
    onPlaying();// w  w  w.ja  v  a2  s  .  c  om
}

From source file:org.thoughtcrime.SMP.notifications.MessageNotifier.java

private static void sendInThreadNotification(Context context, Recipients recipients) {
    try {/*from ww  w  .ja  v a  2 s.co m*/
        if (!TextSecurePreferences.isInThreadNotifications(context)) {
            return;
        }

        Uri uri = recipients.getRingtone();

        if (uri == null) {
            String ringtone = TextSecurePreferences.getNotificationRingtone(context);

            if (ringtone == null) {
                Log.w(TAG, "ringtone preference was null.");
                return;
            } else {
                uri = Uri.parse(ringtone);
            }
        }

        if (uri == null) {
            Log.w(TAG, "couldn't parse ringtone uri " + TextSecurePreferences.getNotificationRingtone(context));
            return;
        }

        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:ru.kudzmi.rajitaku.radioService.RadioService.java

@Override
public void onPrepared(MediaPlayer mediaPlayer) {
    requestAudioFocus();
    mediaPlayer.start();
    startUpdateThread();
}

From source file:de.azapps.mirakel.helper.TaskDialogHelpers.java

public static void playbackFile(final Activity context, final FileMirakel file, final boolean loud) {
    final MediaPlayer mPlayer = new MediaPlayer();
    final AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    if (!loud) {/*from   w ww  .  ja v  a  2  s.  c  om*/
        am.setSpeakerphoneOn(false);
        am.setMode(AudioManager.MODE_IN_CALL);
        context.setVolumeControlStream(AudioManager.STREAM_VOICE_CALL);
    }
    try {
        mPlayer.reset();
        if (!loud) {
            mPlayer.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);
        }
        mPlayer.setDataSource(file.getFileStream(context).getFD());
        mPlayer.prepare();
        mPlayer.start();
        mPlayer.setOnCompletionListener(new OnCompletionListener() {
            @Override
            public void onCompletion(final MediaPlayer mp) {
                audio_playback_dialog.dismiss();
            }
        });
        am.setMode(AudioManager.MODE_NORMAL);
        audio_playback_playing = true;
    } catch (final IOException e) {
        Log.e(TAG, "prepare() failed");
    }
    audio_playback_dialog = new AlertDialog.Builder(context).setTitle(R.string.audio_playback_title)
            .setPositiveButton(R.string.audio_playback_pause, null)
            .setNegativeButton(R.string.audio_playback_stop, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(final DialogInterface dialog, final int which) {
                    mPlayer.release();
                }
            }).setOnCancelListener(new OnCancelListener() {
                @Override
                public void onCancel(final DialogInterface dialog) {
                    mPlayer.release();
                    dialog.cancel();
                }
            }).create();
    audio_playback_dialog.setOnShowListener(new OnShowListener() {
        @Override
        public void onShow(final DialogInterface dialog) {
            final Button button = ((AlertDialog) dialog).getButton(DialogInterface.BUTTON_POSITIVE);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(final View v) {
                    if (audio_playback_playing) {
                        button.setText(R.string.audio_playback_play);
                        mPlayer.pause();
                        audio_playback_playing = false;
                    } else {
                        button.setText(R.string.audio_playback_pause);
                        mPlayer.start();
                        audio_playback_playing = true;
                    }
                }
            });
        }
    });
    audio_playback_dialog.show();
}

From source file:com.joshmoles.byobcontroller.ControlActivity.java

/**
 * Plays raw game audio provided a resource location.
 * @param resource The android resource ID of audio to play.
 *///from  ww w  .ja va2s  .co m
private void playAudio(int resource) {
    MediaPlayer mediaPlayer = MediaPlayer.create(getApplicationContext(), resource);
    mediaPlayer.start();
    while (mediaPlayer.isPlaying())
        android.os.SystemClock.sleep(1000);
    mediaPlayer.release();
    Log.v(Consts.LOAD_FETCH, "Audio released");
}

From source file:org.y20k.transistor.PlayerService.java

@Override
public void onPrepared(MediaPlayer mp) {
    mp.start();
}