Example usage for android.media MediaPlayer MediaPlayer

List of usage examples for android.media MediaPlayer MediaPlayer

Introduction

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

Prototype

public MediaPlayer() 

Source Link

Document

Default constructor.

Usage

From source file:Main.java

public static boolean is3gpFileAudio(String url) {
    int height = 0;
    File mediaFile = new File(url);
    try {/*from  w  w  w  .  j a  v a  2  s .c  om*/
        MediaPlayer mp = new MediaPlayer();
        FileInputStream fs;
        FileDescriptor fd;
        fs = new FileInputStream(mediaFile);
        fd = fs.getFD();
        mp.setDataSource(fd);
        mp.prepare();
        height = mp.getVideoHeight();
        mp.release();
    } catch (Exception e) {
        Log.e("KKIM", "Exception trying to determine if 3gp file is video.", e);
    }
    Log.i("KKIM", "The height of the file is " + height);
    return height == 0;
}

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  a  va 2  s.  com
    try {
        mpObject.setDataSource(DataStream);
        mpObject.prepare();
        mpObject.start();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.dharmaseed.android.TalkPlayerFragment.java

public TalkPlayerFragment() {
    mediaPlayer = new MediaPlayer();
    mediaPlayer.setOnPreparedListener(this);
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    mediaPrepared = false;//w  w  w.  j av  a  2 s . c  om
    userSeekBarPosition = 0;
}

From source file:org.kontalk.ui.AudioFragment.java

public MediaPlayer getPlayer() {
    if (mPlayer == null) {
        mPlayer = new MediaPlayer();
    }
    return mPlayer;
}

From source file:edu.chl.dat255.sofiase.readyforapet.viewcontroller.CreatePetActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.createpet);//from  w w  w . ja v  a2s .c  o  m
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    Button create = (Button) findViewById(R.id.puppy_settings);
    create.setOnClickListener(this);
    //Dog barks when activity is started
    try {
        afd = getAssets().openFd("dogbark.m4a");
        player = new MediaPlayer();
        player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
        //player.setLooping(true);
        player.prepare();
        player.start();

    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:com.qweex.callisto.Live.java

/** Initiates the live player. Can be called across activities. */
static public void LIVE_Init() {
    String TAG = StaticBlob.TAG();
    Log.d(TAG, "Initiating the live player.");
    live_player = new MediaPlayer();
    Log.d(TAG, "Initiating the live player.");
    live_player.setAudioStreamType(AudioManager.STREAM_MUSIC);
    Log.d(TAG, "Initiating the live player.");
    live_player.setOnErrorListener(new MediaPlayer.OnErrorListener() {
        public boolean onError(MediaPlayer mp, int what, int extra) {
            if (LIVE_PreparedListener.pd != null)
                LIVE_PreparedListener.pd.hide();
            String whatWhat = "";
            switch (what) {
            case MediaPlayer.MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK:
                whatWhat = "MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK";
                break;
            case MediaPlayer.MEDIA_ERROR_SERVER_DIED:
                whatWhat = "MEDIA_ERROR_SERVER_DIED";
                break;
            case MediaPlayer.MEDIA_ERROR_UNKNOWN:
                whatWhat = "MEDIA_ERROR_UNKNOWN";
                break;
            default:
                whatWhat = "???";
                return true;
            }/*  w w  w . j a va 2 s .c om*/
            try {
                if (StaticBlob.errorDialog != null)
                    StaticBlob.errorDialog.show();
            } catch (Exception e) {
            }

            try {
                SendErrorReport(whatWhat);
            } catch (Exception e) {
            }
            return true;
        }
    });
    Log.d(TAG, "Initiating the live player.");
}

From source file:se.eliga.aves.songs.BirdSpeciesXenoCantoPlayerFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.audio_player_layout, container, false);

    setHasOptionsMenu(true);// w w  w.  ja v a 2 s  . c om

    mediaPlayer = new MediaPlayer();
    mediaController = new MediaController(getActivity(), false);
    mediaController.setMediaPlayer(this);
    mediaController.setAnchorView(view);

    mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {

        @Override
        public boolean onError(MediaPlayer mp, int what, int extra) {
            if (what == -38) {
                return true; //not pretty.. http://stackoverflow.com/questions/15205855/error-19-0-mediaplayer/15206308#15206308
            } else {
                return false;
            }
        }
    });

    mediaPlayer.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {
        @Override
        public void onBufferingUpdate(MediaPlayer mp, int percent) {
            progressBar.setProgress(percent);
        }
    });
    mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            handler.post(new Runnable() {
                public void run() {
                    progressBar.setVisibility(ProgressBar.GONE);
                    if (mediaController != null) {
                        mediaController.show(0);
                    }
                    if (mediaPlayer != null) {
                        try {
                            mediaPlayer.start();
                        } catch (Exception e) {
                            Log.d(TAG, "Could not start mediaplayer", e);
                        }
                    }
                }
            });
        }
    });
    mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mediaPlayer) {
            if (audioAutoNext.isChecked()) {
                int count = getListAdapter().getCount();
                XenoCantoAudio nextAudio;
                if (currentPosition < (count - 1)) {
                    nextAudio = (XenoCantoAudio) getListAdapter().getItem(++currentPosition);
                } else {
                    nextAudio = (XenoCantoAudio) getListAdapter().getItem(0);
                }
                initiatePlay(nextAudio);
            }
        }
    });
    loadBird(getCurrentBird());
    return view;
}

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

@Override
public int onStartCommand(Intent intent, int flags, int startID) {
    duration = intent.getIntExtra("duration", 0);
    String mediaFile = intent.getStringExtra("media_file");
    try {//from  w w  w  .j a v a  2s .c om
        player = new MediaPlayer();
        player.setAudioStreamType(AudioManager.STREAM_MUSIC);
        player.setDataSource(mediaFile);
        player.setOnPreparedListener(this);
        player.prepareAsync();
        player.setOnErrorListener(this);
    } catch (Exception e) {
        e.printStackTrace();
        sendLocalBroadcast(MainActivity.PLAYER_ERROR);
    }
    return START_STICKY;
}

From source file:com.bczm.widgetcollections.player.MusicPlayer.java

private void initParameter(Context context) {
    mContext = context;/*from w w  w  . j a  v a 2 s .  co m*/

    mMediaPlayer = new MediaPlayer();

    mMediaPlayer.setOnErrorListener(this);
    mMediaPlayer.setOnPreparedListener(this);
    mMediaPlayer.setOnCompletionListener(this);
    mMediaPlayer.setOnBufferingUpdateListener(this);

    mMusicList = new ArrayList<MusicsListEntity>();

    mCurPlayIndex = -1;
    mPlayState = MusicPlayState.MPS_LIST_EMPTY;
    mPLayMode = MusicPlayMode.MPM_LIST_LOOP_PLAY;

    mRandom = new Random();
    mRandom.setSeed(System.currentTimeMillis());
}

From source file:org.sirimangalo.meditationplus.ReceiverAlarm.java

@Override
public void onReceive(Context context, Intent pIntent) {
    Log.v(TAG, "ALARM: received alarm");

    NotificationManager mNM = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    if (player != null) {
        Log.v(TAG, "Releasing media player...");
        try {/*from  w  ww. j  ava  2  s  .com*/
            player.release();
            player = null;
        } catch (Exception e) {
            e.printStackTrace();
            player = null;
        } finally {
            // do nothing
        }
    }

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);

    if (!prefs.getBoolean("set_alarm", false))
        return;

    player = new MediaPlayer();

    int setTime = pIntent.getIntExtra("period_time", 0);
    String medType = pIntent.getStringExtra("type");

    String title = context.getString(R.string.alarm_title);
    String desc = String.format(context.getString(R.string.alarm_desc), medType, setTime);

    Log.v(TAG, "Notification: " + desc);

    // Load the settings
    boolean led = prefs.getBoolean("alarm_led", true);
    boolean vibrate = prefs.getBoolean("alarm_vibrate", true);
    String notificationUri = prefs.getString("notification_uri",
            "android.resource://org.sirimangalo.meditationplus/" + R.raw.bell);

    Log.v(TAG, "notification uri: " + notificationUri);

    if (notificationUri.equals("system"))
        notificationUri = prefs.getString("SystemUri", "");
    else if (notificationUri.equals("file"))
        notificationUri = prefs.getString("FileUri", "");
    else if (notificationUri.equals("tts")) {
        notificationUri = "";
        final String ttsString = prefs.getString("tts_string", desc);
        Intent ttsIntent = new Intent(context, ServiceTTS.class);
        ttsIntent.putExtra("spoken_text", ttsString);
        context.startService(ttsIntent);
    }

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context.getApplicationContext())
            .setSmallIcon(R.drawable.ic_launcher).setContentTitle(title).setContentText(desc);

    Uri uri = Uri.parse(notificationUri);
    mBuilder.setSound(uri);

    // Vibrate
    if (vibrate) {
        mBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
    }
    // Have a light
    if (led) {

        String colorString = prefs.getString("alarm_led_color", "#FFFFFF");

        int color = 0xffffffff;

        try {
            color = Color.parseColor(colorString);
        } catch (Exception e) {

        }

        mBuilder.setLights(color, 300, 1000);
    }

    mBuilder.setAutoCancel(true);

    // Creates an explicit intent for an Activity in your app
    Intent resultIntent = new Intent(context, ActivityMain.class);

    // The stack builder object will contain an artificial back stack for the
    // started Activity.
    // This ensures that navigating backward from the Activity leads out of
    // your application to the Home screen.
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    // Adds the back stack for the Intent (but not the Intent itself)
    stackBuilder.addParentStack(ActivityMain.class);
    // Adds the Intent that starts the Activity to the top of the stack
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setContentIntent(resultPendingIntent);
    NotificationManager mNotificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);

    mNotificationManager.notify(0, mBuilder.build());

}