1 /* 2 * Copyright (C) 2009 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.example.android.apis.media; 18 19 import android.app.Activity; 20 import android.media.MediaPlayer; 21 import android.os.Bundle; 22 import android.util.Log; 23 import android.widget.TextView; 24 import android.widget.Toast; 25 26 import com.example.android.apis.R; 27 28 public class MediaPlayerDemo_Audio extends Activity { 29 30 private static final String TAG = "MediaPlayerDemo"; 31 private MediaPlayer mMediaPlayer; 32 private static final String MEDIA = "media"; 33 private static final int LOCAL_AUDIO = 1; 34 private static final int STREAM_AUDIO = 2; 35 private static final int RESOURCES_AUDIO = 3; 36 private static final int LOCAL_VIDEO = 4; 37 private static final int STREAM_VIDEO = 5; 38 private String path; 39 40 private TextView tx; 41 42 @Override onCreate(Bundle icicle)43 public void onCreate(Bundle icicle) { 44 super.onCreate(icicle); 45 tx = new TextView(this); 46 setContentView(tx); 47 Bundle extras = getIntent().getExtras(); 48 playAudio(extras.getInt(MEDIA)); 49 } 50 playAudio(Integer media)51 private void playAudio(Integer media) { 52 try { 53 switch (media) { 54 case LOCAL_AUDIO: 55 /** 56 * TODO: Set the path variable to a local audio file path. 57 */ 58 path = ""; 59 if (path == "") { 60 // Tell the user to provide an audio file URL. 61 Toast 62 .makeText( 63 MediaPlayerDemo_Audio.this, 64 "Please edit MediaPlayer_Audio Activity, " 65 + "and set the path variable to your audio file path." 66 + " Your audio file must be stored on sdcard.", 67 Toast.LENGTH_LONG).show(); 68 69 } 70 mMediaPlayer = new MediaPlayer(); 71 mMediaPlayer.setDataSource(path); 72 mMediaPlayer.prepare(); 73 mMediaPlayer.start(); 74 break; 75 case RESOURCES_AUDIO: 76 /** 77 * TODO: Upload a audio file to res/raw folder and provide 78 * its resid in MediaPlayer.create() method. 79 */ 80 mMediaPlayer = MediaPlayer.create(this, R.raw.test_cbr); 81 mMediaPlayer.start(); 82 83 } 84 tx.setText("Playing audio..."); 85 86 } catch (Exception e) { 87 Log.e(TAG, "error: " + e.getMessage(), e); 88 } 89 90 } 91 92 @Override onDestroy()93 protected void onDestroy() { 94 super.onDestroy(); 95 // TODO Auto-generated method stub 96 if (mMediaPlayer != null) { 97 mMediaPlayer.release(); 98 mMediaPlayer = null; 99 } 100 101 } 102 } 103