AudioAttachmentView.java revision 79bf6f70c54d08dc9c3481b8461a3a46a3cefb83
1/* 2 * Copyright (C) 2008 Esmertec AG. 3 * Copyright (C) 2008 The Android Open Source Project 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18package com.android.mms.ui; 19 20import com.android.mms.R; 21 22import android.content.Context; 23import android.content.res.Resources; 24import android.graphics.Bitmap; 25import android.media.AudioManager; 26import android.media.MediaPlayer; 27import android.media.MediaPlayer.OnCompletionListener; 28import android.media.MediaPlayer.OnErrorListener; 29import android.net.Uri; 30import android.util.AttributeSet; 31import android.util.Log; 32import android.widget.LinearLayout; 33import android.widget.TextView; 34 35import java.util.Map; 36 37/** 38 * This class provides an embedded editor/viewer of audio attachment. 39 */ 40public class AudioAttachmentView extends LinearLayout implements 41 SlideViewInterface { 42 private static final String TAG = "AudioAttachmentView"; 43 44 private final Resources mRes; 45 private TextView mNameView; 46 private TextView mAlbumView; 47 private TextView mArtistView; 48 private TextView mErrorMsgView; 49 private Uri mAudioUri; 50 private MediaPlayer mMediaPlayer; 51 private boolean mIsPlaying; 52 53 public AudioAttachmentView(Context context) { 54 super(context); 55 mRes = context.getResources(); 56 } 57 58 public AudioAttachmentView(Context context, AttributeSet attrs) { 59 super(context, attrs); 60 mRes = context.getResources(); 61 } 62 63 @Override 64 protected void onFinishInflate() { 65 mNameView = (TextView) findViewById(R.id.audio_name); 66 mAlbumView = (TextView) findViewById(R.id.album_name); 67 mArtistView = (TextView) findViewById(R.id.artist_name); 68 mErrorMsgView = (TextView) findViewById(R.id.audio_error_msg); 69 } 70 71 private void onPlaybackError() { 72 Log.e(TAG, "Error occurred while playing audio."); 73 showErrorMessage(mRes.getString(R.string.cannot_play_audio)); 74 stopAudio(); 75 } 76 77 private void cleanupMediaPlayer() { 78 if (mMediaPlayer != null) { 79 try { 80 mMediaPlayer.stop(); 81 mMediaPlayer.release(); 82 } finally { 83 mMediaPlayer = null; 84 } 85 } 86 } 87 88 synchronized public void startAudio() { 89 if (!mIsPlaying && (mAudioUri != null)) { 90 mMediaPlayer = MediaPlayer.create(mContext, mAudioUri); 91 if (mMediaPlayer != null) { 92 mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); 93 mMediaPlayer.setOnCompletionListener(new OnCompletionListener() { 94 public void onCompletion(MediaPlayer mp) { 95 stopAudio(); 96 } 97 }); 98 mMediaPlayer.setOnErrorListener(new OnErrorListener() { 99 public boolean onError(MediaPlayer mp, int what, int extra) { 100 onPlaybackError(); 101 return true; 102 } 103 }); 104 105 mIsPlaying = true; 106 mMediaPlayer.start(); 107 } 108 } 109 } 110 111 public void startVideo() { 112 // TODO Auto-generated method stub 113 114 } 115 116 public void setAudio(Uri audio, String name, Map<String, ?> extras) { 117 synchronized (this) { 118 mAudioUri = audio; 119 } 120 121 mNameView.setText(name); 122 mAlbumView.setText((String) extras.get("album")); 123 mArtistView.setText((String) extras.get("artist")); 124 } 125 126 public void setImage(String name, Bitmap bitmap) { 127 // TODO Auto-generated method stub 128 129 } 130 131 public void setImageRegionFit(String fit) { 132 // TODO Auto-generated method stub 133 134 } 135 136 public void setImageVisibility(boolean visible) { 137 // TODO Auto-generated method stub 138 139 } 140 141 public void setText(String name, String text) { 142 // TODO Auto-generated method stub 143 144 } 145 146 public void setTextVisibility(boolean visible) { 147 // TODO Auto-generated method stub 148 149 } 150 151 public void setVideo(String name, Uri video) { 152 // TODO Auto-generated method stub 153 154 } 155 156 public void setVideoThumbnail(String name, Bitmap bitmap) { 157 } 158 159 public void setVideoVisibility(boolean visible) { 160 // TODO Auto-generated method stub 161 162 } 163 164 synchronized public void stopAudio() { 165 try { 166 cleanupMediaPlayer(); 167 } finally { 168 mIsPlaying = false; 169 } 170 } 171 172 public void stopVideo() { 173 // TODO Auto-generated method stub 174 175 } 176 177 public void reset() { 178 synchronized (this) { 179 if (mIsPlaying) { 180 stopAudio(); 181 } 182 } 183 mErrorMsgView.setVisibility(GONE); 184 } 185 186 public void setVisibility(boolean visible) { 187 // TODO Auto-generated method stub 188 189 } 190 191 private void showErrorMessage(String msg) { 192 mErrorMsgView.setText(msg); 193 mErrorMsgView.setVisibility(VISIBLE); 194 } 195 196 public void pauseAudio() { 197 // TODO Auto-generated method stub 198 199 } 200 201 public void pauseVideo() { 202 // TODO Auto-generated method stub 203 204 } 205 206 public void seekAudio(int seekTo) { 207 // TODO Auto-generated method stub 208 209 } 210 211 public void seekVideo(int seekTo) { 212 // TODO Auto-generated method stub 213 214 } 215} 216