`
hududumo
  • 浏览: 237886 次
文章分类
社区版块
存档分类
最新评论

Android Dev:VideoView源代码浅析及拓展应用

 
阅读更多

Android DevVideoView源代码浅析及拓展应用

Android开发不免要涉及到编写媒体播放器,对于初学者来说用MediaPlayer实现一个具有基本功能的播放器(有进度条,可以通过进度条上的按钮进行控制)还是有一定难度的,幸好Android还提供了一个VideoView类,借用该类可以快速实现简单的媒体播放功能,其源代码如下(单击右边那个箭头展开):

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533

534

535

536

537

538

539

540

541

542

543

544

545

546

547

548

549

550

551

552

553

554

555

556

557

558

559

560

561

562

563

564

565

566

567

568

569

570

571

572

573

574

575

576

577

578

579

580

581

582

583

584

585

586

587

588

589

590

591

592

593

594

595

596

597

598

599

600

601

602

603

604

605

606

607

608

609

610

611

612

613

614

615

616

617

618

619

620

621

622

623

624

625

626

627

628

629

630

631

632

633

634

635

636

637

638

639

640

641

642

643

644

645

646

647

648

649

650

651

652

653

654

655

656

657

658

659

660

661

662

663

664

665

666

667

668

669

670

671

672

673

674

675

/*

* Copyright (C) 2006 The Android Open Source Project

*

* Licensed under the Apache License, Version 2.0 (the "License");

* you may not use this file except in compliance with the License.

* You may obtain a copy of the License at

*

* http://www.apache.org/licenses/LICENSE-2.0

*

* Unless required by applicable law or agreed to in writing, software

* distributed under the License is distributed on an "AS IS" BASIS,

* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

* See the License for the specific language governing permissions and

* limitations under the License.

*/

package android.widget;

import android.app.AlertDialog;

import android.content.Context;

import android.content.DialogInterface;

import android.content.Intent;

import android.content.res.Resources;

import android.media.AudioManager;

import android.media.MediaPlayer;

import android.media.Metadata;

import android.media.MediaPlayer.OnCompletionListener;

import android.media.MediaPlayer.OnErrorListener;

import android.net.Uri;

import android.os.PowerManager;

import android.util.AttributeSet;

import android.util.Log;

import android.view.KeyEvent;

import android.view.MotionEvent;

import android.view.SurfaceHolder;

import android.view.SurfaceView;

import android.view.View;

import android.widget.MediaController.*;

import java.io.IOException;

import java.util.Map;

/**

* Displays a video file. The VideoView class

* can load images from various sources (such as resources or content

* providers), takes care of computing its measurement from the video so that

* it can be used in any layout manager, and provides various display options

* such as scaling and tinting.

*/

public class VideoView extends SurfaceView implements MediaPlayerControl {

private String TAG = "VideoView";

// settable by the client

private Uri mUri;

private Map<string, string=""> mHeaders;

private int mDuration;

// all possible internal states

private static final int STATE_ERROR = -1;

private static final int STATE_IDLE = 0;

private static final int STATE_PREPARING = 1;

private static final int STATE_PREPARED = 2;

private static final int STATE_PLAYING = 3;

private static final int STATE_PAUSED = 4;

private static final int STATE_PLAYBACK_COMPLETED = 5;

private static final int STATE_SUSPEND = 6;

private static final int STATE_RESUME = 7;

private static final int STATE_SUSPEND_UNSUPPORTED = 8;

// mCurrentState is a VideoView object&#39;s current state.

// mTargetState is the state that a method caller intends to reach.

// For instance, regardless the VideoView object&#39;s current state,

// calling pause() intends to bring the object to a target state

// of STATE_PAUSED.

private int mCurrentState = STATE_IDLE;

private int mTargetState = STATE_IDLE;

// All the stuff we need for playing and showing a video

private SurfaceHolder mSurfaceHolder = null;

private MediaPlayer mMediaPlayer = null;

private int mVideoWidth;

private int mVideoHeight;

private int mSurfaceWidth;

private int mSurfaceHeight;

private MediaController mMediaController;

private OnCompletionListener mOnCompletionListener;

private MediaPlayer.OnPreparedListener mOnPreparedListener;

private int mCurrentBufferPercentage;

private OnErrorListener mOnErrorListener;

private int mSeekWhenPrepared; // recording the seek position while preparing

private boolean mCanPause;

private boolean mCanSeekBack;

private boolean mCanSeekForward;

private int mStateWhenSuspended; //state before calling suspend()

public VideoView(Context context) {

super(context);

initVideoView();

}

public VideoView(Context context, AttributeSet attrs) {

this(context, attrs, 0);

initVideoView();

}

public VideoView(Context context, AttributeSet attrs, int defStyle) {

super(context, attrs, defStyle);

initVideoView();

}

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

//Log.i(&quot;@@@@&quot;, &quot;onMeasure&quot;);

int width = getDefaultSize(mVideoWidth, widthMeasureSpec);

int height = getDefaultSize(mVideoHeight, heightMeasureSpec);

if (mVideoWidth &gt; 0 &amp;&amp; mVideoHeight &gt; 0) {

if ( mVideoWidth * height &gt; width * mVideoHeight ) {

//Log.i(&quot;@@@&quot;, &quot;image too tall, correcting&quot;);

height = width * mVideoHeight / mVideoWidth;

} else if ( mVideoWidth * height &lt; width * mVideoHeight ) {

//Log.i(&quot;@@@&quot;, &quot;image too wide, correcting&quot;);

width = height * mVideoWidth / mVideoHeight;

} else {

//Log.i(&quot;@@@&quot;, &quot;aspect ratio is correct: &quot; +

//width+&quot;/&quot;+height+&quot;=&quot;+

//mVideoWidth+&quot;/&quot;+mVideoHeight);

}

}

//Log.i(&quot;@@@@@@@@@@&quot;, &quot;setting size: &quot; + width + &#39;x&#39; + height);

setMeasuredDimension(width, height);

}

public int resolveAdjustedSize(int desiredSize, int measureSpec) {

int result = desiredSize;

int specMode = MeasureSpec.getMode(measureSpec);

int specSize = MeasureSpec.getSize(measureSpec);

switch (specMode) {

case MeasureSpec.UNSPECIFIED:

/* Parent says we can be as big as we want. Just don&#39;t be larger

* than max size imposed on ourselves.

*/

result = desiredSize;

break;

case MeasureSpec.AT_MOST:

/* Parent says we can be as big as we want, up to specSize.

* Don&#39;t be larger than specSize, and don&#39;t be larger than

* the max size imposed on ourselves.

*/

result = Math.min(desiredSize, specSize);

break;

case MeasureSpec.EXACTLY:

// No choice. Do what we are told.

result = specSize;

break;

}

return result;

}

private void initVideoView() {

mVideoWidth = 0;

mVideoHeight = 0;

getHolder().addCallback(mSHCallback);

getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

setFocusable(true);

setFocusableInTouchMode(true);

requestFocus();

mCurrentState = STATE_IDLE;

mTargetState = STATE_IDLE;

}

public void setVideoPath(String path) {

setVideoURI(Uri.parse(path));

}

public void setVideoURI(Uri uri) {

setVideoURI(uri, null);

}

/**

* @hide

*/

public void setVideoURI(Uri uri, Map<string, string=""> headers) {

mUri = uri;

mHeaders = headers;

mSeekWhenPrepared = 0;

openVideo();

requestLayout();

invalidate();

}

public void stopPlayback() {

if (mMediaPlayer != null) {

mMediaPlayer.stop();

mMediaPlayer.release();

mMediaPlayer = null;

mCurrentState = STATE_IDLE;

mTargetState = STATE_IDLE;

}

}

private void openVideo() {

if (mUri == null || mSurfaceHolder == null) {

// not ready for playback just yet, will try again later

return;

}

// Tell the music playback service to pause

// TODO: these constants need to be published somewhere in the framework.

Intent i = new Intent(&quot;com.android.music.musicservicecommand&quot;);

i.putExtra(&quot;command&quot;, &quot;pause&quot;);

mContext.sendBroadcast(i);

// we shouldn&#39;t clear the target state, because somebody might have

// called start() previously

release(false);

try {

mMediaPlayer = new MediaPlayer();

mMediaPlayer.setOnPreparedListener(mPreparedListener);

mMediaPlayer.setOnVideoSizeChangedListener(mSizeChangedListener);

mDuration = -1;

mMediaPlayer.setOnCompletionListener(mCompletionListener);

mMediaPlayer.setOnErrorListener(mErrorListener);

mMediaPlayer.setOnBufferingUpdateListener(mBufferingUpdateListener);

mCurrentBufferPercentage = 0;

mMediaPlayer.setDataSource(mContext, mUri, mHeaders);

mMediaPlayer.setDisplay(mSurfaceHolder);

mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

mMediaPlayer.setScreenOnWhilePlaying(true);

mMediaPlayer.prepareAsync();

// we don&#39;t set the target state here either, but preserve the

// target state that was there before.

mCurrentState = STATE_PREPARING;

attachMediaController();

} catch (IOException ex) {

Log.w(TAG, &quot;Unable to open content: &quot; + mUri, ex);

mCurrentState = STATE_ERROR;

mTargetState = STATE_ERROR;

mErrorListener.onError(mMediaPlayer, MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);

return;

} catch (IllegalArgumentException ex) {

Log.w(TAG, &quot;Unable to open content: &quot; + mUri, ex);

mCurrentState = STATE_ERROR;

mTargetState = STATE_ERROR;

mErrorListener.onError(mMediaPlayer, MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);

return;

}

}

public void setMediaController(MediaController controller) {

if (mMediaController != null) {

mMediaController.hide();

}

mMediaController = controller;

attachMediaController();

}

private void attachMediaController() {

if (mMediaPlayer != null &amp;&amp; mMediaController != null) {

mMediaController.setMediaPlayer(this);

View anchorView = this.getParent() instanceof View ?

(View)this.getParent() : this;

mMediaController.setAnchorView(anchorView);

mMediaController.setEnabled(isInPlaybackState());

}

}

MediaPlayer.OnVideoSizeChangedListener mSizeChangedListener =

new MediaPlayer.OnVideoSizeChangedListener() {

public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {

mVideoWidth = mp.getVideoWidth();

mVideoHeight = mp.getVideoHeight();

if (mVideoWidth != 0 &amp;&amp; mVideoHeight != 0) {

getHolder().setFixedSize(mVideoWidth, mVideoHeight);

}

}

};

MediaPlayer.OnPreparedListener mPreparedListener = new MediaPlayer.OnPreparedListener() {

public void onPrepared(MediaPlayer mp) {

mCurrentState = STATE_PREPARED;

// Get the capabilities of the player for this stream

Metadata data = mp.getMetadata(MediaPlayer.METADATA_ALL,

MediaPlayer.BYPASS_METADATA_FILTER);

if (data != null) {

mCanPause = !data.has(Metadata.PAUSE_AVAILABLE)

|| data.getBoolean(Metadata.PAUSE_AVAILABLE);

mCanSeekBack = !data.has(Metadata.SEEK_BACKWARD_AVAILABLE)

|| data.getBoolean(Metadata.SEEK_BACKWARD_AVAILABLE);

mCanSeekForward = !data.has(Metadata.SEEK_FORWARD_AVAILABLE)

|| data.getBoolean(Metadata.SEEK_FORWARD_AVAILABLE);

} else {

mCanPause = mCanSeekBack = mCanSeekForward = true;

}

if (mOnPreparedListener != null) {

mOnPreparedListener.onPrepared(mMediaPlayer);

}

if (mMediaController != null) {

mMediaController.setEnabled(true);

}

mVideoWidth = mp.getVideoWidth();

mVideoHeight = mp.getVideoHeight();

int seekToPosition = mSeekWhenPrepared; // mSeekWhenPrepared may be changed after seekTo() call

if (seekToPosition != 0) {

seekTo(seekToPosition);

}

if (mVideoWidth != 0 &amp;&amp; mVideoHeight != 0) {

//Log.i(&quot;@@@@&quot;, &quot;video size: &quot; + mVideoWidth +&quot;/&quot;+ mVideoHeight);

getHolder().setFixedSize(mVideoWidth, mVideoHeight);

if (mSurfaceWidth == mVideoWidth &amp;&amp; mSurfaceHeight == mVideoHeight) {

// We didn&#39;t actually change the size (it was already at the size

// we need), so we won&#39;t get a &quot;surface changed&quot; callback, so

// start the video here instead of in the callback.

if (mTargetState == STATE_PLAYING) {

start();

if (mMediaController != null) {

mMediaController.show();

}

} else if (!isPlaying() &amp;&amp;

(seekToPosition != 0 || getCurrentPosition() &gt; 0)) {

if (mMediaController != null) {

// Show the media controls when we&#39;re paused into a video and make &#39;em stick.

mMediaController.show(0);

}

}

}

} else {

// We don&#39;t know the video size yet, but should start anyway.

// The video size might be reported to us later.

if (mTargetState == STATE_PLAYING) {

start();

}

}

}

};

private MediaPlayer.OnCompletionListener mCompletionListener =

new MediaPlayer.OnCompletionListener() {

public void onCompletion(MediaPlayer mp) {

mCurrentState = STATE_PLAYBACK_COMPLETED;

mTargetState = STATE_PLAYBACK_COMPLETED;

if (mMediaController != null) {

mMediaController.hide();

}

if (mOnCompletionListener != null) {

mOnCompletionListener.onCompletion(mMediaPlayer);

}

}

};

private MediaPlayer.OnErrorListener mErrorListener =

new MediaPlayer.OnErrorListener() {

public boolean onError(MediaPlayer mp, int framework_err, int impl_err) {

Log.d(TAG, &quot;Error: &quot; + framework_err + &quot;,&quot; + impl_err);

mCurrentState = STATE_ERROR;

mTargetState = STATE_ERROR;

if (mMediaController != null) {

mMediaController.hide();

}

/* If an error handler has been supplied, use it and finish. */

if (mOnErrorListener != null) {

if (mOnErrorListener.onError(mMediaPlayer, framework_err, impl_err)) {

return true;

}

}

/* Otherwise, pop up an error dialog so the user knows that

* something bad has happened. Only try and pop up the dialog

* if we&#39;re attached to a window. When we&#39;re going away and no

* longer have a window, don&#39;t bother showing the user an error.

*/

if (getWindowToken() != null) {

Resources r = mContext.getResources();

int messageId;

if (framework_err == MediaPlayer.MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK) {

messageId = com.android.internal.R.string.VideoView_error_text_invalid_progressive_playback;

} else {

messageId = com.android.internal.R.string.VideoView_error_text_unknown;

}

new AlertDialog.Builder(mContext)

.setTitle(com.android.internal.R.string.VideoView_error_title)

.setMessage(messageId)

.setPositiveButton(com.android.internal.R.string.VideoView_error_button,

new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int whichButton) {

/* If we get here, there is no onError listener, so

* at least inform them that the video is over.

*/

if (mOnCompletionListener != null) {

mOnCompletionListener.onCompletion(mMediaPlayer);

}

}

})

.setCancelable(false)

.show();

}

return true;

}

};

private MediaPlayer.OnBufferingUpdateListener mBufferingUpdateListener =

new MediaPlayer.OnBufferingUpdateListener() {

public void onBufferingUpdate(MediaPlayer mp, int percent) {

mCurrentBufferPercentage = percent;

}

};

/**

* Register a callback to be invoked when the media file

* is loaded and ready to go.

*

* @param l The callback that will be run

*/

public void setOnPreparedListener(MediaPlayer.OnPreparedListener l)

{

mOnPreparedListener = l;

}

/**

* Register a callback to be invoked when the end of a media file

* has been reached during playback.

*

* @param l The callback that will be run

*/

public void setOnCompletionListener(OnCompletionListener l)

{

mOnCompletionListener = l;

}

/**

* Register a callback to be invoked when an error occurs

* during playback or setup. If no listener is specified,

* or if the listener returned false, VideoView will inform

* the user of any errors.

*

* @param l The callback that will be run

*/

public void setOnErrorListener(OnErrorListener l)

{

mOnErrorListener = l;

}

SurfaceHolder.Callback mSHCallback = new SurfaceHolder.Callback()

{

public void surfaceChanged(SurfaceHolder holder, int format,

int w, int h)

{

mSurfaceWidth = w;

mSurfaceHeight = h;

boolean isValidState = (mTargetState == STATE_PLAYING);

boolean hasValidSize = (mVideoWidth == w &amp;&amp; mVideoHeight == h);

if (mMediaPlayer != null &amp;&amp; isValidState &amp;&amp; hasValidSize) {

if (mSeekWhenPrepared != 0) {

seekTo(mSeekWhenPrepared);

}

start();

if (mMediaController != null) {

mMediaController.show();

}

}

}

public void surfaceCreated(SurfaceHolder holder)

{

mSurfaceHolder = holder;

//resume() was called before surfaceCreated()

if (mMediaPlayer != null &amp;&amp; mCurrentState == STATE_SUSPEND

&amp;&amp; mTargetState == STATE_RESUME) {

mMediaPlayer.setDisplay(mSurfaceHolder);

resume();

} else {

openVideo();

}

}

public void surfaceDestroyed(SurfaceHolder holder)

{

// after we return from this we can&#39;t use the surface any more

mSurfaceHolder = null;

if (mMediaController != null) mMediaController.hide();

if (mCurrentState != STATE_SUSPEND) {

release(true);

}

}

};

/*

* release the media player in any state

*/

private void release(boolean cleartargetstate) {

if (mMediaPlayer != null) {

mMediaPlayer.reset();

mMediaPlayer.release();

mMediaPlayer = null;

mCurrentState = STATE_IDLE;

if (cleartargetstate) {

mTargetState = STATE_IDLE;

}

}

}

@Override

public boolean onTouchEvent(MotionEvent ev) {

if (isInPlaybackState() &amp;&amp; mMediaController != null) {

toggleMediaControlsVisiblity();

}

return false;

}

@Override

public boolean onTrackballEvent(MotionEvent ev) {

if (isInPlaybackState() &amp;&amp; mMediaController != null) {

toggleMediaControlsVisiblity();

}

return false;

}

@Override

public boolean onKeyDown(int keyCode, KeyEvent event)

{

boolean isKeyCodeSupported = keyCode != KeyEvent.KEYCODE_BACK &amp;&amp;

keyCode != KeyEvent.KEYCODE_VOLUME_UP &amp;&amp;

keyCode != KeyEvent.KEYCODE_VOLUME_DOWN &amp;&amp;

keyCode != KeyEvent.KEYCODE_MENU &amp;&amp;

keyCode != KeyEvent.KEYCODE_CALL &amp;&amp;

keyCode != KeyEvent.KEYCODE_ENDCALL;

if (isInPlaybackState() &amp;&amp; isKeyCodeSupported &amp;&amp; mMediaController != null) {

if (keyCode == KeyEvent.KEYCODE_HEADSETHOOK ||

keyCode == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE) {

if (mMediaPlayer.isPlaying()) {

pause();

mMediaController.show();

} else {

start();

mMediaController.hide();

}

return true;

} else if (keyCode == KeyEvent.KEYCODE_MEDIA_STOP

&amp;&amp; mMediaPlayer.isPlaying()) {

pause();

mMediaController.show();

} else {

toggleMediaControlsVisiblity();

}

}

return super.onKeyDown(keyCode, event);

}

private void toggleMediaControlsVisiblity() {

if (mMediaController.isShowing()) {

mMediaController.hide();

} else {

mMediaController.show();

}

}

public void start() {

if (isInPlaybackState()) {

mMediaPlayer.start();

mCurrentState = STATE_PLAYING;

}

mTargetState = STATE_PLAYING;

}

public void pause() {

if (isInPlaybackState()) {

if (mMediaPlayer.isPlaying()) {

mMediaPlayer.pause();

mCurrentState = STATE_PAUSED;

}

}

mTargetState = STATE_PAUSED;

}

public void suspend() {

if (isInPlaybackState()) {

if (mMediaPlayer.suspend()) {

mStateWhenSuspended = mCurrentState;

mCurrentState = STATE_SUSPEND;

mTargetState = STATE_SUSPEND;

} else {

release(false);

mCurrentState = STATE_SUSPEND_UNSUPPORTED;

Log.w(TAG, &quot;Unable to suspend video. Release MediaPlayer.&quot;);

}

}

}

public void resume() {

if (mSurfaceHolder == null &amp;&amp; mCurrentState == STATE_SUSPEND){

mTargetState = STATE_RESUME;

return;

}

if (mMediaPlayer != null &amp;&amp; mCurrentState == STATE_SUSPEND) {

if (mMediaPlayer.resume()) {

mCurrentState = mStateWhenSuspended;

mTargetState = mStateWhenSuspended;

} else {

Log.w(TAG, &quot;Unable to resume video&quot;);

}

return;

}

if (mCurrentState == STATE_SUSPEND_UNSUPPORTED) {

openVideo();

}

}

// cache duration as mDuration for faster access

public int getDuration() {

if (isInPlaybackState()) {

if (mDuration &gt; 0) {

return mDuration;

}

mDuration = mMediaPlayer.getDuration();

return mDuration;

}

mDuration = -1;

return mDuration;

}

public int getCurrentPosition() {

if (isInPlaybackState()) {

return mMediaPlayer.getCurrentPosition();

}

return 0;

}

public void seekTo(int msec) {

if (isInPlaybackState()) {

mMediaPlayer.seekTo(msec);

mSeekWhenPrepared = 0;

} else {

mSeekWhenPrepared = msec;

}

}

public boolean isPlaying() {

return isInPlaybackState() &amp;&amp; mMediaPlayer.isPlaying();

}

public int getBufferPercentage() {

if (mMediaPlayer != null) {

return mCurrentBufferPercentage;

}

return 0;

}

private boolean isInPlaybackState() {

return (mMediaPlayer != null &amp;&amp;

mCurrentState != STATE_ERROR &amp;&amp;

mCurrentState != STATE_IDLE &amp;&amp;

mCurrentState != STATE_PREPARING);

}

public boolean canPause() {

return mCanPause;

}

public boolean canSeekBackward() {

return mCanSeekBack;

}

public boolean canSeekForward() {

return mCanSeekForward;

}

}

</string,></string,>

VideoView这个类写得非常漂亮,尤其是它的实行机制。在第58-67行定义了一些常量,通过变量名可以看出这些常量表示的是一些状态,在第7475行分别有两个变量mCurrnetStatemTargetState,这两个变量的分别用来记录当前的状态,和目标状态。VideoView类就是围绕这这两个状态来进行相应的操作,通读VideoView源代码我们可以很清楚地认识到这一点,同时我们也能发现Google显然考虑了播放多媒体文件时的各种情况以保证播放的顺利进行,虽然本文题目是VideoView浅析,但是我不想对该类做很多讲解,一是我还不能完全理解每个部分的具体作用,二是这个类写得并不复杂,细心多读几遍很容易就能了解个大概,所以希望各位在往下阅读前先通读一遍VideoView的源代码。

如果要实现简单的播放器我们直接调用这个类就可以了,但是要对这个类做一些修改来满足我们的需求首先要做的就是把这个类变成我们自己的。新建一个Class,取名为MyVideoView,然后就是把VideoView源代码Copy下来,覆盖原来的MyVideoView类,此时肯定会有很多错误,按照下列方法解决(注意最好先自己查找错误再在下面的修改方法中查找对应的解决方法):

1.如果你没有更改类名,把类名更改为MyVideoView

2.更改三个构造函数名为MyVideoView

3.openVideo()中显示“mContext cannot be resolved”。在变量定义部分添加一个:private ContextmContext; 并在三个构造函数中加上:mContext =context;。关于Context,如果不理解请自行解决。

4.openVideo()中,mMediaPlayer.setDataSource(mContext, mUri,mHeaders);错误,通过源代码可以看出在三个参数的setDataSource上面的注释中有这样一句:@hide pendingAPI council,表示这个方法还没有开放,我们只能使用两个参数的版本,所以把这一句改为:mMediaPlayer.setDataSource(mContext,mUri);

5.onPrepared(MediaPlayer mp)中有大量关于Metadata的错误,而又无法查看到Metadata类的源代码,我不了解其具体实现方法(有高手知道的话还请指点),但是我们可以分析出这一段是在判断相应的媒体文件播放时能否暂停、快进/退,暂时就把整个有错误的部分替换成:mCanPause =mCanSeekBack = mCanSeekForward = true;,即默认媒体文件可以支持暂停等功能。

6.onError(MediaPlayer mp, int framework_err,int impl_err) 中的两个messageId,这两个显然是出错时的错误代码,我没有查找其具体值,暂时分别改为123456(不要笑,希望有能力的去找一下这两个值具体是什么)。

7.onError(MediaPlayer mp, int framework_err,int impl_err) 中的setTitlesetPositiveButton后面的参数我也没有去找具体的字符串是什么,暂时就分别改为"error!!!""OK"

8.suspend()中的mMediaPlayer.suspend()错误,同样是因为@hide的问题,由于这是一个public的函数,VideoView的其他部分也没有调用这个函数,处于方便,直接将其注释掉,如有需要,我们要自己实现这一功能。

9.resume() 中同8.一样的问题,没办法这个我们也先注释了,另外在surfaceCreated(SurfaceHolderholder)中调用了这个函数,所以我们只保留surfaceCreatedelse部分。

修改后的代码如下(单击右边那个箭头展开):

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533

534

535

536

537

538

539

540

541

542

543

544

545

546

547

548

549

550

551

552

553

554

555

556

557

558

559

560

561

562

563

564

565

566

567

568

569

570

571

572

573

574

575

576

577

578

579

580

581

582

583

584

585

586

587

588

589

590

591

592

593

594

595

596

597

598

599

600

601

602

603

604

605

606

607

608

609

610

611

612

613

614

615

616

617

618

619

620

621

622

623

624

625

626

627

628

629

630

631

632

633

634

635

636

637

638

639

640

641

642

643

644

645

646

647

648

649

650

651

652

653

654

655

656

package com.ltnc.tempvideoview;

import java.io.IOException;

import java.util.Map;

import android.app.AlertDialog;

import android.content.Context;

import android.content.DialogInterface;

import android.content.Intent;

import android.content.res.Resources;

import android.media.AudioManager;

import android.media.MediaPlayer;

import android.media.MediaPlayer.OnCompletionListener;

import android.media.MediaPlayer.OnErrorListener;

import android.net.Uri;

import android.util.AttributeSet;

import android.util.Log;

import android.view.KeyEvent;

import android.view.MotionEvent;

import android.view.SurfaceHolder;

import android.view.SurfaceView;

import android.view.View;

import android.widget.MediaController;

import android.widget.MediaController.MediaPlayerControl;

public class MyVideoView extends SurfaceView implements MediaPlayerControl {

private String TAG = &quot;VideoView&quot;;

// settable by the client

private Uri mUri;

private Map<string, string=""> mHeaders;

private int mDuration;

// all possible internal states

private static final int STATE_ERROR = -1;

private static final int STATE_IDLE = 0;

private static final int STATE_PREPARING = 1;

private static final int STATE_PREPARED = 2;

private static final int STATE_PLAYING = 3;

private static final int STATE_PAUSED = 4;

private static final int STATE_PLAYBACK_COMPLETED = 5;

private static final int STATE_SUSPEND = 6;

private static final int STATE_RESUME = 7;

private static final int STATE_SUSPEND_UNSUPPORTED = 8;

// mCurrentState is a VideoView object&#39;s current state.

// mTargetState is the state that a method caller intends to reach.

// For instance, regardless the VideoView object&#39;s current state,

// calling pause() intends to bring the object to a target state

// of STATE_PAUSED.

private int mCurrentState = STATE_IDLE;

private int mTargetState = STATE_IDLE;

// All the stuff we need for playing and showing a video

private SurfaceHolder mSurfaceHolder = null;

private MediaPlayer mMediaPlayer = null;

private int mVideoWidth;

private int mVideoHeight;

private int mSurfaceWidth;

private int mSurfaceHeight;

private MediaController mMediaController;

private OnCompletionListener mOnCompletionListener;

private MediaPlayer.OnPreparedListener mOnPreparedListener;

private int mCurrentBufferPercentage;

private OnErrorListener mOnErrorListener;

private int mSeekWhenPrepared; // recording the seek position while preparing

private boolean mCanPause;

private boolean mCanSeekBack;

private boolean mCanSeekForward;

private int mStateWhenSuspended; //state before calling suspend()

private Context mContext;

public MyVideoView(Context context) {

super(context);

mContext = context;

initVideoView();

}

public MyVideoView(Context context, AttributeSet attrs) {

this(context, attrs, 0);

mContext = context;

initVideoView();

}

public MyVideoView(Context context, AttributeSet attrs, int defStyle) {

super(context, attrs, defStyle);

mContext = context;

initVideoView();

}

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

//Log.i(&quot;@@@@&quot;, &quot;onMeasure&quot;);

int width = getDefaultSize(mVideoWidth, widthMeasureSpec);

int height = getDefaultSize(mVideoHeight, heightMeasureSpec);

if (mVideoWidth &gt; 0 &amp;&amp; mVideoHeight &gt; 0) {

if ( mVideoWidth * height &gt; width * mVideoHeight ) {

//Log.i(&quot;@@@&quot;, &quot;image too tall, correcting&quot;);

height = width * mVideoHeight / mVideoWidth;

} else if ( mVideoWidth * height &lt; width * mVideoHeight ) {

//Log.i(&quot;@@@&quot;, &quot;image too wide, correcting&quot;);

width = height * mVideoWidth / mVideoHeight;

} else {

//Log.i(&quot;@@@&quot;, &quot;aspect ratio is correct: &quot; +

//width+&quot;/&quot;+height+&quot;=&quot;+

//mVideoWidth+&quot;/&quot;+mVideoHeight);

}

}

//Log.i(&quot;@@@@@@@@@@&quot;, &quot;setting size: &quot; + width + &#39;x&#39; + height);

setMeasuredDimension(width, height);

}

public int resolveAdjustedSize(int desiredSize, int measureSpec) {

int result = desiredSize;

int specMode = MeasureSpec.getMode(measureSpec);

int specSize = MeasureSpec.getSize(measureSpec);

switch (specMode) {

case MeasureSpec.UNSPECIFIED:

/* Parent says we can be as big as we want. Just don&#39;t be larger

* than max size imposed on ourselves.

*/

result = desiredSize;

break;

case MeasureSpec.AT_MOST:

/* Parent says we can be as big as we want, up to specSize.

* Don&#39;t be larger than specSize, and don&#39;t be larger than

* the max size imposed on ourselves.

*/

result = Math.min(desiredSize, specSize);

break;

case MeasureSpec.EXACTLY:

// No choice. Do what we are told.

result = specSize;

break;

}

return result;

}

private void initVideoView() {

mVideoWidth = 0;

mVideoHeight = 0;

getHolder().addCallback(mSHCallback);

getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

setFocusable(true);

setFocusableInTouchMode(true);

requestFocus();

mCurrentState = STATE_IDLE;

mTargetState = STATE_IDLE;

}

public void setVideoPath(String path) {

setVideoURI(Uri.parse(path));

}

public void setVideoURI(Uri uri) {

setVideoURI(uri, null);

}

/**

* @hide

*/

public void setVideoURI(Uri uri, Map<string, string=""> headers) {

mUri = uri;

mHeaders = headers;

mSeekWhenPrepared = 0;

openVideo();

requestLayout();

invalidate();

}

public void stopPlayback() {

if (mMediaPlayer != null) {

mMediaPlayer.stop();

mMediaPlayer.release();

mMediaPlayer = null;

mCurrentState = STATE_IDLE;

mTargetState = STATE_IDLE;

}

}

private void openVideo() {

if (mUri == null || mSurfaceHolder == null) {

// not ready for playback just yet, will try again later

return;

}

// Tell the music playback service to pause

// TODO: these constants need to be published somewhere in the framework.

Intent i = new Intent(&quot;com.android.music.musicservicecommand&quot;);

i.putExtra(&quot;command&quot;, &quot;pause&quot;);

mContext.sendBroadcast(i);

// we shouldn&#39;t clear the target state, because somebody might have

// called start() previously

release(false);

try {

mMediaPlayer = new MediaPlayer();

mMediaPlayer.setOnPreparedListener(mPreparedListener);

mMediaPlayer.setOnVideoSizeChangedListener(mSizeChangedListener);

mDuration = -1;

mMediaPlayer.setOnCompletionListener(mCompletionListener);

mMediaPlayer.setOnErrorListener(mErrorListener);

mMediaPlayer.setOnBufferingUpdateListener(mBufferingUpdateListener);

mCurrentBufferPercentage = 0;

mMediaPlayer.setDataSource(mContext, mUri);

mMediaPlayer.setDisplay(mSurfaceHolder);

mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

mMediaPlayer.setScreenOnWhilePlaying(true);

mMediaPlayer.prepareAsync();

// we don&#39;t set the target state here either, but preserve the

// target state that was there before.

mCurrentState = STATE_PREPARING;

attachMediaController();

} catch (IOException ex) {

Log.w(TAG, &quot;Unable to open content: &quot; + mUri, ex);

mCurrentState = STATE_ERROR;

mTargetState = STATE_ERROR;

mErrorListener.onError(mMediaPlayer, MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);

return;

} catch (IllegalArgumentException ex) {

Log.w(TAG, &quot;Unable to open content: &quot; + mUri, ex);

mCurrentState = STATE_ERROR;

mTargetState = STATE_ERROR;

mErrorListener.onError(mMediaPlayer, MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);

return;

}

}

public void setMediaController(MediaController controller) {

if (mMediaController != null) {

mMediaController.hide();

}

mMediaController = controller;

attachMediaController();

}

private void attachMediaController() {

if (mMediaPlayer != null &amp;&amp; mMediaController != null) {

mMediaController.setMediaPlayer(this);

View anchorView = this.getParent() instanceof View ?

(View)this.getParent() : this;

mMediaController.setAnchorView(anchorView);

mMediaController.setEnabled(isInPlaybackState());

}

}

MediaPlayer.OnVideoSizeChangedListener mSizeChangedListener =

new MediaPlayer.OnVideoSizeChangedListener() {

public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {

mVideoWidth = mp.getVideoWidth();

mVideoHeight = mp.getVideoHeight();

if (mVideoWidth != 0 &amp;&amp; mVideoHeight != 0) {

getHolder().setFixedSize(mVideoWidth, mVideoHeight);

}

}

};

MediaPlayer.OnPreparedListener mPreparedListener = new MediaPlayer.OnPreparedListener() {

public void onPrepared(MediaPlayer mp) {

mCurrentState = STATE_PREPARED;

// Get the capabilities of the player for this stream

/*Metadata data = mp.getMetadata(MediaPlayer.METADATA_ALL,

MediaPlayer.BYPASS_METADATA_FILTER);

if (data != null) {

mCanPause = !data.has(Metadata.PAUSE_AVAILABLE)

|| data.getBoolean(Metadata.PAUSE_AVAILABLE);

mCanSeekBack = !data.has(Metadata.SEEK_BACKWARD_AVAILABLE)

|| data.getBoolean(Metadata.SEEK_BACKWARD_AVAILABLE);

mCanSeekForward = !data.has(Metadata.SEEK_FORWARD_AVAILABLE)

|| data.getBoolean(Metadata.SEEK_FORWARD_AVAILABLE);

} else {*/

mCanPause = mCanSeekBack = mCanSeekForward = true;

/*}*/

if (mOnPreparedListener != null) {

mOnPreparedListener.onPrepared(mMediaPlayer);

}

if (mMediaController != null) {

mMediaController.setEnabled(true);

}

mVideoWidth = mp.getVideoWidth();

mVideoHeight = mp.getVideoHeight();

int seekToPosition = mSeekWhenPrepared; // mSeekWhenPrepared may be changed after seekTo() call

if (seekToPosition != 0) {

seekTo(seekToPosition);

}

if (mVideoWidth != 0 &amp;&amp; mVideoHeight != 0) {

//Log.i(&quot;@@@@&quot;, &quot;video size: &quot; + mVideoWidth +&quot;/&quot;+ mVideoHeight);

getHolder().setFixedSize(mVideoWidth, mVideoHeight);

if (mSurfaceWidth == mVideoWidth &amp;&amp; mSurfaceHeight == mVideoHeight) {

// We didn&#39;t actually change the size (it was already at the size

// we need), so we won&#39;t get a &quot;surface changed&quot; callback, so

// start the video here instead of in the callback.

if (mTargetState == STATE_PLAYING) {

start();

if (mMediaController != null) {

mMediaController.show();

}

} else if (!isPlaying() &amp;&amp;

(seekToPosition != 0 || getCurrentPosition() &gt; 0)) {

if (mMediaController != null) {

// Show the media controls when we&#39;re paused into a video and make &#39;em stick.

mMediaController.show(0);

}

}

}

} else {

// We don&#39;t know the video size yet, but should start anyway.

// The video size might be reported to us later.

if (mTargetState == STATE_PLAYING) {

start();

}

}

}

};

private MediaPlayer.OnCompletionListener mCompletionListener =

new MediaPlayer.OnCompletionListener() {

public void onCompletion(MediaPlayer mp) {

mCurrentState = STATE_PLAYBACK_COMPLETED;

mTargetState = STATE_PLAYBACK_COMPLETED;

if (mMediaController != null) {

mMediaController.hide();

}

if (mOnCompletionListener != null) {

mOnCompletionListener.onCompletion(mMediaPlayer);

}

}

};

private MediaPlayer.OnErrorListener mErrorListener =

new MediaPlayer.OnErrorListener() {

public boolean onError(MediaPlayer mp, int framework_err, int impl_err) {

Log.d(TAG, &quot;Error: &quot; + framework_err + &quot;,&quot; + impl_err);

mCurrentState = STATE_ERROR;

mTargetState = STATE_ERROR;

if (mMediaController != null) {

mMediaController.hide();

}

/* If an error handler has been supplied, use it and finish. */

if (mOnErrorListener != null) {

if (mOnErrorListener.onError(mMediaPlayer, framework_err, impl_err)) {

return true;

}

}

/* Otherwise, pop up an error dialog so the user knows that

* something bad has happened. Only try and pop up the dialog

* if we&#39;re attached to a window. When we&#39;re going away and no

* longer have a window, don&#39;t bother showing the user an error.

*/

if (getWindowToken() != null) {

Resources r = mContext.getResources();

int messageId;

if (framework_err == MediaPlayer.MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK) {

messageId = 123;

} else {

messageId = 456;

}

new AlertDialog.Builder(mContext)

.setTitle(&quot;error!!!&quot;)

.setMessage(messageId)

.setPositiveButton(&quot;OK&quot;,

new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int whichButton) {

/* If we get here, there is no onError listener, so

* at least inform them that the video is over.

*/

if (mOnCompletionListener != null) {

mOnCompletionListener.onCompletion(mMediaPlayer);

}

}

})

.setCancelable(false)

.show();

}

return true;

}

};

private MediaPlayer.OnBufferingUpdateListener mBufferingUpdateListener =

new MediaPlayer.OnBufferingUpdateListener() {

public void onBufferingUpdate(MediaPlayer mp, int percent) {

mCurrentBufferPercentage = percent;

}

};

/**

* Register a callback to be invoked when the media file

* is loaded and ready to go.

*

* @param l The callback that will be run

*/

public void setOnPreparedListener(MediaPlayer.OnPreparedListener l)

{

mOnPreparedListener = l;

}

/**

* Register a callback to be invoked when the end of a media file

* has been reached during playback.

*

* @param l The callback that will be run

*/

public void setOnCompletionListener(OnCompletionListener l)

{

mOnCompletionListener = l;

}

/**

* Register a callback to be invoked when an error occurs

* during playback or setup. If no listener is specified,

* or if the listener returned false, VideoView will inform

* the user of any errors.

*

* @param l The callback that will be run

*/

public void setOnErrorListener(OnErrorListener l)

{

mOnErrorListener = l;

}

SurfaceHolder.Callback mSHCallback = new SurfaceHolder.Callback()

{

public void surfaceChanged(SurfaceHolder holder, int format,

int w, int h)

{

mSurfaceWidth = w;

mSurfaceHeight = h;

boolean isValidState = (mTargetState == STATE_PLAYING);

boolean hasValidSize = (mVideoWidth == w &amp;&amp; mVideoHeight == h);

if (mMediaPlayer != null &amp;&amp; isValidState &amp;&amp; hasValidSize) {

if (mSeekWhenPrepared != 0) {

seekTo(mSeekWhenPrepared);

}

start();

if (mMediaController != null) {

mMediaController.show();

}

}

}

public void surfaceCreated(SurfaceHolder holder)

{

mSurfaceHolder = holder;

//resume() was called before surfaceCreated()

/*if (mMediaPlayer != null &amp;&amp; mCurrentState == STATE_SUSPEND

&amp;&amp; mTargetState == STATE_RESUME) {

mMediaPlayer.setDisplay(mSurfaceHolder);

resume();

} else {*/

openVideo();

/*}*/

}

public void surfaceDestroyed(SurfaceHolder holder)

{

// after we return from this we can&#39;t use the surface any more

mSurfaceHolder = null;

if (mMediaController != null) mMediaController.hide();

if (mCurrentState != STATE_SUSPEND) {

release(true);

}

}

};

/*

* release the media player in any state

*/

private void release(boolean cleartargetstate) {

if (mMediaPlayer != null) {

mMediaPlayer.reset();

mMediaPlayer.release();

mMediaPlayer = null;

mCurrentState = STATE_IDLE;

if (cleartargetstate) {

mTargetState = STATE_IDLE;

}

}

}

@Override

public boolean onTouchEvent(MotionEvent ev) {

if (isInPlaybackState() &amp;&amp; mMediaController != null) {

toggleMediaControlsVisiblity();

}

return false;

}

@Override

public boolean onTrackballEvent(MotionEvent ev) {

if (isInPlaybackState() &amp;&amp; mMediaController != null) {

toggleMediaControlsVisiblity();

}

return false;

}

@Override

public boolean onKeyDown(int keyCode, KeyEvent event)

{

boolean isKeyCodeSupported = keyCode != KeyEvent.KEYCODE_BACK &amp;&amp;

keyCode != KeyEvent.KEYCODE_VOLUME_UP &amp;&amp;

keyCode != KeyEvent.KEYCODE_VOLUME_DOWN &amp;&amp;

keyCode != KeyEvent.KEYCODE_MENU &amp;&amp;

keyCode != KeyEvent.KEYCODE_CALL &amp;&amp;

keyCode != KeyEvent.KEYCODE_ENDCALL;

if (isInPlaybackState() &amp;&amp; isKeyCodeSupported &amp;&amp; mMediaController != null) {

if (keyCode == KeyEvent.KEYCODE_HEADSETHOOK ||

keyCode == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE) {

if (mMediaPlayer.isPlaying()) {

pause();

mMediaController.show();

} else {

start();

mMediaController.hide();

}

return true;

} else if (keyCode == KeyEvent.KEYCODE_MEDIA_STOP

&amp;&amp; mMediaPlayer.isPlaying()) {

pause();

mMediaController.show();

} else {

toggleMediaControlsVisiblity();

}

}

return super.onKeyDown(keyCode, event);

}

private void toggleMediaControlsVisiblity() {

if (mMediaController.isShowing()) {

mMediaController.hide();

} else {

mMediaController.show();

}

}

public void start() {

if (isInPlaybackState()) {

mMediaPlayer.start();

mCurrentState = STATE_PLAYING;

}

mTargetState = STATE_PLAYING;

}

public void pause() {

if (isInPlaybackState()) {

if (mMediaPlayer.isPlaying()) {

mMediaPlayer.pause();

mCurrentState = STATE_PAUSED;

}

}

mTargetState = STATE_PAUSED;

}

/*public void suspend() {

if (isInPlaybackState()) {

if (mMediaPlayer.suspend()) {

mStateWhenSuspended = mCurrentState;

mCurrentState = STATE_SUSPEND;

mTargetState = STATE_SUSPEND;

} else {

release(false);

mCurrentState = STATE_SUSPEND_UNSUPPORTED;

Log.w(TAG, &quot;Unable to suspend video. Release MediaPlayer.&quot;);

}

}

}*/

/*public void resume() {

if (mSurfaceHolder == null &amp;&amp; mCurrentState == STATE_SUSPEND){

mTargetState = STATE_RESUME;

return;

}

if (mMediaPlayer != null &amp;&amp; mCurrentState == STATE_SUSPEND) {

if (mMediaPlayer.resume()) {

mCurrentState = mStateWhenSuspended;

mTargetState = mStateWhenSuspended;

} else {

Log.w(TAG, &quot;Unable to resume video&quot;);

}

return;

}

if (mCurrentState == STATE_SUSPEND_UNSUPPORTED) {

openVideo();

}

}*/

// cache duration as mDuration for faster access

public int getDuration() {

if (isInPlaybackState()) {

if (mDuration &gt; 0) {

return mDuration;

}

mDuration = mMediaPlayer.getDuration();

return mDuration;

}

mDuration = -1;

return mDuration;

}

public int getCurrentPosition() {

if (isInPlaybackState()) {

return mMediaPlayer.getCurrentPosition();

}

return 0;

}

public void seekTo(int msec) {

if (isInPlaybackState()) {

mMediaPlayer.seekTo(msec);

mSeekWhenPrepared = 0;

} else {

mSeekWhenPrepared = msec;

}

}

public boolean isPlaying() {

return isInPlaybackState() &amp;&amp; mMediaPlayer.isPlaying();

}

public int getBufferPercentage() {

if (mMediaPlayer != null) {

return mCurrentBufferPercentage;

}

return 0;

}

private boolean isInPlaybackState() {

return (mMediaPlayer != null &amp;&amp;

mCurrentState != STATE_ERROR &amp;&amp;

mCurrentState != STATE_IDLE &amp;&amp;

mCurrentState != STATE_PREPARING);

}

public boolean canPause() {

return mCanPause;

}

public boolean canSeekBackward() {

return mCanSeekBack;

}

public boolean canSeekForward() {

return mCanSeekForward;

}

}

</string,></string,>

现在MyVideoView中已经没有任何错误了,我们可以直接像VideoView那样使用了,至于XML部分只需将VideoView替换为你自己的类的,如com.XXX.XXX.MyVdieoView。如果需要进行一些修改也变得很容易了,比如我们要实现这样一个功能:播放音频文件时显示一张图片,点击图片可以显示控制条并可进行相应操作。

新建一个Class取名为MyAudioView,把继承由原来的SurfaceView改为ImageView,代码如下(单击右边那个箭头展开):

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533

534

535

536

537

538

539

540

541

542

543

544

545

546

547

548

549

550

551

552

553

554

555

556

557

558

559

560

561

562

package com.ltnc.tempvideoview;

import java.io.IOException;

import java.util.Map;

import android.app.AlertDialog;

import android.content.Context;

import android.content.DialogInterface;

import android.content.Intent;

import android.content.res.Resources;

import android.media.AudioManager;

import android.media.MediaPlayer;

import android.media.MediaPlayer.OnCompletionListener;

import android.media.MediaPlayer.OnErrorListener;

import android.net.Uri;

import android.util.AttributeSet;

import android.util.Log;

import android.view.KeyEvent;

import android.view.MotionEvent;

import android.view.SurfaceHolder;

import android.view.View;

import android.view.View.MeasureSpec;

import android.widget.ImageView;

import android.widget.MediaController;

import android.widget.MediaController.MediaPlayerControl;

public class MyAudiView extends ImageView implements MediaPlayerControl {

private String TAG = &quot;VideoView&quot;;

// settable by the client

private Uri mUri;

private Map<string, string=""> mHeaders;

private int mDuration;

// all possible internal states

private static final int STATE_ERROR = -1;

private static final int STATE_IDLE = 0;

private static final int STATE_PREPARING = 1;

private static final int STATE_PREPARED = 2;

private static final int STATE_PLAYING = 3;

private static final int STATE_PAUSED = 4;

private static final int STATE_PLAYBACK_COMPLETED = 5;

private static final int STATE_SUSPEND = 6;

private static final int STATE_RESUME = 7;

private static final int STATE_SUSPEND_UNSUPPORTED = 8;

// mCurrentState is a VideoView object&#39;s current state.

// mTargetState is the state that a method caller intends to reach.

// For instance, regardless the VideoView object&#39;s current state,

// calling pause() intends to bring the object to a target state

// of STATE_PAUSED.

private int mCurrentState = STATE_IDLE;

private int mTargetState = STATE_IDLE;

// All the stuff we need for playing and showing a video

private SurfaceHolder mSurfaceHolder = null;

private MediaPlayer mMediaPlayer = null;

private int mVideoWidth;

private int mVideoHeight;

private int mSurfaceWidth;

private int mSurfaceHeight;

private MediaController mMediaController;

private OnCompletionListener mOnCompletionListener;

private MediaPlayer.OnPreparedListener mOnPreparedListener;

private int mCurrentBufferPercentage;

private OnErrorListener mOnErrorListener;

private int mSeekWhenPrepared; // recording the seek position while preparing

private boolean mCanPause;

private boolean mCanSeekBack;

private boolean mCanSeekForward;

private int mStateWhenSuspended; //state before calling suspend()

private Context mContext;

public MyAudiView(Context context) {

super(context);

mContext = context;

initVideoView();

}

public MyAudiView(Context context, AttributeSet attrs) {

this(context, attrs, 0);

mContext = context;

initVideoView();

}

public MyAudiView(Context context, AttributeSet attrs, int defStyle) {

super(context, attrs, defStyle);

mContext = context;

initVideoView();

}

private void initVideoView() {

Log.i(TAG, &quot;initVideoView&quot;);

mCurrentState = STATE_IDLE;

mTargetState = STATE_IDLE;

}

public void setVideoPath(String path) {

Log.i(TAG, &quot;setVideoPath&quot;);

setVideoURI(Uri.parse(path));

}

public void setVideoURI(Uri uri) {

Log.i(TAG, &quot;setVideoURI&quot;);

mUri = uri;

mSeekWhenPrepared = 0;

openVideo();

requestLayout();

invalidate();

}

public void stopPlayback() {

Log.i(TAG, &quot;stopPlayback&quot;);

if (mMediaPlayer != null) {

mMediaPlayer.stop();

mMediaPlayer.release();

mMediaPlayer = null;

mCurrentState = STATE_IDLE;

mTargetState = STATE_IDLE;

}

}

private void openVideo() {

Log.i(TAG, &quot;openVideo&quot;);

if (mUri == null) {

// not ready for playback just yet, will try again later

return;

}

// Tell the music playback service to pause

// TODO: these constants need to be published somewhere in the

// framework.

Intent i = new Intent(&quot;com.android.music.musicservicecommand&quot;);

i.putExtra(&quot;command&quot;, &quot;pause&quot;);

mContext.sendBroadcast(i);

// we shouldn&#39;t clear the target state, because somebody might have

// called start() previously

release(false);

try {

mMediaPlayer = new MediaPlayer();

mMediaPlayer.setOnPreparedListener(mPreparedListener);

mDuration = -1;

mMediaPlayer.setOnCompletionListener(mCompletionListener);

mMediaPlayer.setOnErrorListener(mErrorListener);

mMediaPlayer.setOnBufferingUpdateListener(mBufferingUpdateListener);

mCurrentBufferPercentage = 0;

mMediaPlayer.setDataSource(mContext, mUri);

// mMediaPlayer.setDisplay(mSurfaceHolder);

mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

mMediaPlayer.setScreenOnWhilePlaying(true);

mMediaPlayer.prepareAsync();

// we don&#39;t set the target state here either, but preserve the

// target state that was there before.

mCurrentState = STATE_PREPARING;

attachMediaController();

} catch (IOException ex) {

Log.w(TAG, &quot;Unable to open content: &quot; + mUri, ex);

mCurrentState = STATE_ERROR;

mTargetState = STATE_ERROR;

mErrorListener.onError(mMediaPlayer,

MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);

return;

} catch (IllegalArgumentException ex) {

Log.w(TAG, &quot;Unable to open content: &quot; + mUri, ex);

mCurrentState = STATE_ERROR;

mTargetState = STATE_ERROR;

mErrorListener.onError(mMediaPlayer,

MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);

return;

}

}

public void setMediaController(MediaController controller) {

Log.i(TAG, &quot;setMediaController&quot;);

if (mMediaController != null) {

mMediaController.hide();

}

mMediaController = controller;

attachMediaController();

}

private void attachMediaController() {

Log.i(TAG, &quot;attachMediaController&quot;);

if (mMediaPlayer != null &amp;&amp; mMediaController != null) {

mMediaController.setMediaPlayer(this);

View anchorView = this.getParent() instanceof View ? (View) this

.getParent() : this;

mMediaController.setAnchorView(anchorView);

mMediaController.setEnabled(isInPlaybackState());

}

}

MediaPlayer.OnPreparedListener mPreparedListener = new MediaPlayer.OnPreparedListener() {

public void onPrepared(MediaPlayer mp) {

Log.i(TAG, &quot;onPrepared&quot;);

mCurrentState = STATE_PREPARED;

// Get the capabilities of the player for this stream

// Metadata data = mp.getMetadata(MediaPlayer.METADATA_ALL,

// MediaPlayer.BYPASS_METADATA_FILTER);

//

// if (data != null) {

// mCanPause = !data.has(Metadata.PAUSE_AVAILABLE)

// || data.getBoolean(Metadata.PAUSE_AVAILABLE);

// mCanSeekBack = !data.has(Metadata.SEEK_BACKWARD_AVAILABLE)

// || data.getBoolean(Metadata.SEEK_BACKWARD_AVAILABLE);

// mCanSeekForward = !data.has(Metadata.SEEK_FORWARD_AVAILABLE)

// || data.getBoolean(Metadata.SEEK_FORWARD_AVAILABLE);

// } else {

mCanPause = mCanSeekBack = mCanSeekForward = true;

// }

if (mOnPreparedListener != null) {

mOnPreparedListener.onPrepared(mMediaPlayer);

}

if (mMediaController != null) {

mMediaController.setEnabled(true);

}

// mVideoWidth = mp.getVideoWidth();

// mVideoHeight = mp.getVideoHeight();

int seekToPosition = mSeekWhenPrepared; // mSeekWhenPrepared may be

// changed after seekTo()

// call

if (seekToPosition != 0) {

seekTo(seekToPosition);

}

// if (mVideoWidth != 0 &amp;&amp; mVideoHeight != 0) {

// //Log.i(&quot;@@@@&quot;, &quot;video size: &quot; + mVideoWidth +&quot;/&quot;+ mVideoHeight);

// getHolder().setFixedSize(mVideoWidth, mVideoHeight);

// if (mSurfaceWidth == mVideoWidth &amp;&amp; mSurfaceHeight ==

// mVideoHeight) {

// // We didn&#39;t actually change the size (it was already at the size

// // we need), so we won&#39;t get a &quot;surface changed&quot; callback, so

// // start the video here instead of in the callback.

if (mTargetState == STATE_PLAYING) {

start();

if (mMediaController != null) {

mMediaController.show();

}

} else if (!isPlaying()

&amp;&amp; (seekToPosition != 0 || getCurrentPosition() &gt; 0)) {

if (mMediaController != null) {

// Show the media controls when we&#39;re paused into a video

// and make &#39;em stick.

mMediaController.show(0);

}

}

// }

// } else {

// // We don&#39;t know the video size yet, but should start anyway.

// // The video size might be reported to us later.

// if (mTargetState == STATE_PLAYING) {

// start();

// }

// }

}

};

private MediaPlayer.OnCompletionListener mCompletionListener = new MediaPlayer.OnCompletionListener() {

public void onCompletion(MediaPlayer mp) {

Log.i(TAG, &quot;onCompletion&quot;);

mCurrentState = STATE_PLAYBACK_COMPLETED;

mTargetState = STATE_PLAYBACK_COMPLETED;

if (mMediaController != null) {

mMediaController.hide();

}

if (mOnCompletionListener != null) {

mOnCompletionListener.onCompletion(mMediaPlayer);

}

}

};

private MediaPlayer.OnErrorListener mErrorListener = new MediaPlayer.OnErrorListener() {

public boolean onError(MediaPlayer mp, int framework_err, int impl_err) {

Log.i(TAG, &quot;onError&quot;);

Log.d(TAG, &quot;Error: &quot; + framework_err + &quot;,&quot; + impl_err);

mCurrentState = STATE_ERROR;

mTargetState = STATE_ERROR;

if (mMediaController != null) {

mMediaController.hide();

}

/* If an error handler has been supplied, use it and finish. */

if (mOnErrorListener != null) {

if (mOnErrorListener.onError(mMediaPlayer, framework_err,

impl_err)) {

return true;

}

}

/*

* Otherwise, pop up an error dialog so the user knows that

* something bad has happened. Only try and pop up the dialog if

* we&#39;re attached to a window. When we&#39;re going away and no longer

* have a window, don&#39;t bother showing the user an error.

*/

if (getWindowToken() != null) {

Resources r = mContext.getResources();

int messageId;

if (framework_err == MediaPlayer.MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK) {

messageId = 123;

} else {

messageId = 456;

}

new AlertDialog.Builder(mContext)

.setTitle(&quot;error&quot;)

.setMessage(messageId)

.setPositiveButton(&quot;error&quot;,

new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog,

int whichButton) {

/*

* If we get here, there is no onError

* listener, so at least inform them

* that the video is over.

*/

if (mOnCompletionListener != null) {

mOnCompletionListener

.onCompletion(mMediaPlayer);

}

}

}).setCancelable(false).show();

}

return true;

}

};

private MediaPlayer.OnBufferingUpdateListener mBufferingUpdateListener = new MediaPlayer.OnBufferingUpdateListener() {

public void onBufferingUpdate(MediaPlayer mp, int percent) {

Log.i(TAG, &quot;onBufferingUpdate&quot;);

mCurrentBufferPercentage = percent;

}

};

/**

* Register a callback to be invoked when the media file is loaded and ready

* to go.

*

* @param l

* The callback that will be run

*/

public void setOnPreparedListener(MediaPlayer.OnPreparedListener l) {

Log.i(TAG, &quot;setOnPreparedListener&quot;);

mOnPreparedListener = l;

}

/**

* Register a callback to be invoked when the end of a media file has been

* reached during playback.

*

* @param l

* The callback that will be run

*/

public void setOnCompletionListener(OnCompletionListener l) {

Log.i(TAG, &quot;setOnCompletionListener&quot;);

mOnCompletionListener = l;

}

/**

* Register a callback to be invoked when an error occurs during playback or

* setup. If no listener is specified, or if the listener returned false,

* VideoView will inform the user of any errors.

*

* @param l

* The callback that will be run

*/

public void setOnErrorListener(OnErrorListener l) {

Log.i(TAG, &quot;setOnErrorListener&quot;);

mOnErrorListener = l;

}

/*

* SurfaceHolder.Callback mSHCallback = new SurfaceHolder.Callback() {

* public void surfaceChanged(SurfaceHolder holder, int format, int w, int

* h) { Log.i(TAG, &quot;surfaceChanged&quot;); mSurfaceWidth = w; mSurfaceHeight = h;

* boolean isValidState = (mTargetState == STATE_PLAYING); boolean

* hasValidSize = (mVideoWidth == w &amp;&amp; mVideoHeight == h); if (mMediaPlayer

* != null &amp;&amp; isValidState &amp;&amp; hasValidSize) { if (mSeekWhenPrepared != 0) {

* seekTo(mSeekWhenPrepared); } start(); if (mMediaController != null) {

* mMediaController.show(); } } }

*

* public void surfaceCreated(SurfaceHolder holder) { Log.i(TAG,

* &quot;surfaceCreated&quot;); mSurfaceHolder = holder; //resume() was called before

* surfaceCreated() // if (mMediaPlayer != null &amp;&amp; mCurrentState ==

* STATE_SUSPEND // &amp;&amp; mTargetState == STATE_RESUME) { //

* mMediaPlayer.setDisplay(mSurfaceHolder); // resume(); // } else {

* openVideo(); // } }

*

* public void surfaceDestroyed(SurfaceHolder holder) { Log.i(TAG,

* &quot;surfaceDestroyed&quot;); // after we return from this we can&#39;t use the

* surface any more mSurfaceHolder = null; if (mMediaController != null)

* mMediaController.hide(); if (mCurrentState != STATE_SUSPEND) {

* release(true); } } };

*/

/*

* release the media player in any state

*/

private void release(boolean cleartargetstate) {

Log.i(TAG, &quot;release&quot;);

if (mMediaPlayer != null) {

mMediaPlayer.reset();

mMediaPlayer.release();

mMediaPlayer = null;

mCurrentState = STATE_IDLE;

if (cleartargetstate) {

mTargetState = STATE_IDLE;

}

}

}

@Override

public boolean onTouchEvent(MotionEvent ev) {

Log.i(TAG, &quot;onTouchEvent&quot;);

if (isInPlaybackState() &amp;&amp; mMediaController != null) {

toggleMediaControlsVisiblity();

}

return false;

}

@Override

public boolean onTrackballEvent(MotionEvent ev) {

Log.i(TAG, &quot;onTrackballEvent&quot;);

if (isInPlaybackState() &amp;&amp; mMediaController != null) {

toggleMediaControlsVisiblity();

}

return false;

}

@Override

public boolean onKeyDown(int keyCode, KeyEvent event) {

Log.i(TAG, &quot;onKeyDown&quot;);

boolean isKeyCodeSupported = keyCode != KeyEvent.KEYCODE_BACK

&amp;&amp; keyCode != KeyEvent.KEYCODE_VOLUME_UP

&amp;&amp; keyCode != KeyEvent.KEYCODE_VOLUME_DOWN

&amp;&amp; keyCode != KeyEvent.KEYCODE_MENU

&amp;&amp; keyCode != KeyEvent.KEYCODE_CALL

&amp;&amp; keyCode != KeyEvent.KEYCODE_ENDCALL;

if (isInPlaybackState() &amp;&amp; isKeyCodeSupported

&amp;&amp; mMediaController != null) {

if (keyCode == KeyEvent.KEYCODE_HEADSETHOOK

|| keyCode == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE) {

if (mMediaPlayer.isPlaying()) {

pause();

mMediaController.show();

} else {

start();

mMediaController.hide();

}

return true;

} else if (keyCode == KeyEvent.KEYCODE_MEDIA_STOP

&amp;&amp; mMediaPlayer.isPlaying()) {

pause();

mMediaController.show();

} else {

toggleMediaControlsVisiblity();

}

}

return super.onKeyDown(keyCode, event);

}

private void toggleMediaControlsVisiblity() {

Log.i(TAG, &quot;toggleMediaControlsVisiblity&quot;);

if (mMediaController.isShowing()) {

mMediaController.hide();

} else {

mMediaController.show();

}

}

public void start() {

Log.i(TAG, &quot;start&quot;);

if (isInPlaybackState()) {

mMediaPlayer.start();

mCurrentState = STATE_PLAYING;

}

mTargetState = STATE_PLAYING;

}

public void pause() {

Log.i(TAG, &quot;pause&quot;);

if (isInPlaybackState()) {

if (mMediaPlayer.isPlaying()) {

mMediaPlayer.pause();

mCurrentState = STATE_PAUSED;

}

}

mTargetState = STATE_PAUSED;

}

// cache duration as mDuration for faster access

public int getDuration() {

Log.i(TAG, &quot;getDuration&quot;);

if (isInPlaybackState()) {

if (mDuration &gt; 0) {

return mDuration;

}

mDuration = mMediaPlayer.getDuration();

return mDuration;

}

mDuration = -1;

return mDuration;

}

public int getCurrentPosition() {

Log.i(TAG, &quot;getCurrentPosition&quot;);

if (isInPlaybackState()) {

return mMediaPlayer.getCurrentPosition();

}

return 0;

}

public void seekTo(int msec) {

Log.i(TAG, &quot;seekTo&quot;);

if (isInPlaybackState()) {

mMediaPlayer.seekTo(msec);

mSeekWhenPrepared = 0;

} else {

mSeekWhenPrepared = msec;

}

}

public boolean isPlaying() {

// Log.i(TAG, &quot;isPlaying&quot;);

return isInPlaybackState() &amp;&amp; mMediaPlayer.isPlaying();

}

public int getBufferPercentage() {

Log.i(TAG, &quot;getBufferPercentage&quot;);

if (mMediaPlayer != null) {

return mCurrentBufferPercentage;

}

return 0;

}

private boolean isInPlaybackState() {

// Log.i(TAG, &quot;isInPlaybackState&quot;);

return (mMediaPlayer != null &amp;&amp; mCurrentState != STATE_ERROR

&amp;&amp; mCurrentState != STATE_IDLE &amp;&amp; mCurrentState != STATE_PREPARING);

}

public boolean canPause() {

Log.i(TAG, &quot;canPause&quot;);

return mCanPause;

}

public boolean canSeekBackward() {

Log.i(TAG, &quot;canSeekBackward&quot;);

return mCanSeekBack;

}

public boolean canSeekForward() {

Log.i(TAG, &quot;canSeekForward&quot;);

return mCanSeekForward;

}

}

</string,>

主要的修改就是删除了与视频长宽以及SurfaceView有关的东西,大家可以自行对比找出修改的部分,修改之后就可以像ImageView一样给MyAudioView设置一个图片,同时还可以实现播放音频的功能。

PS. 本文内容仅供参考学习,同时难免有所疏漏,如有问题还请指教。同时给初学者一个建议,如果你的开发环境还不能查看Android的源代码,强烈建议你实现这个功能(具体方法不说了,一搜一大堆),源代码毕竟是那些高手中的高手写的,有许多值得我们学习借鉴的地方,同时也可以用类似本文提到的方法,借用源代码快书实现一些我们想要的功能。

分享到:
评论
1 楼 ningwang_tt 2012-07-31  
您好 请问 你这个VideoView是android哪个版本的啊 我用2.3版本的可以播放rtsp直播流 但是您这个rtsp提示是不正确的视频格式

相关推荐

Global site tag (gtag.js) - Google Analytics