Android Media Player 状态机
状态机图例
图源自谷歌官方API
MediaPlayer State具体方法API介绍和使用参看
【Android 多媒体开发】 MediaPlayer 状态机 接口 方法 解析
状态检测
在MediaPlayer 开发过程中有的朋友会遇到过 -38错误,为什么是-38呢, 简单追踪下流程
看一个Nuplayer 示例
frameworks/av/media/libmediaplayerservice/nuplayer/NuPlayerDriver.cpp
616 status_t NuPlayerDriver::reset() {
617 ALOGD("reset(%p) at state %d", this, mState);
618
619 updateMetrics("reset");
620 logMetrics("reset");
621
622 Mutex::Autolock autoLock(mLock);
623
624 switch (mState) {
625 case STATE_IDLE:
626 return OK;
627
628 case STATE_SET_DATASOURCE_PENDING:
629 case STATE_RESET_IN_PROGRESS:
630 return INVALID_OPERATION;
631
632 case STATE_PREPARING:
633 {
634 CHECK(mIsAsyncPrepare);
635
636 notifyListener_l(MEDIA_PREPARED);
637 break;
638 }
639
640 default:
641 break;
642 }
643
644 if (mState != STATE_STOPPED) {
645 notifyListener_l(MEDIA_STOPPED);
646 }
647
648 mState = STATE_RESET_IN_PROGRESS;
649 mPlayer->resetAsync();
650
651 while (mState == STATE_RESET_IN_PROGRESS) {
652 mCondition.wait(mLock);
653 }
654
655 mDurationUs = -1;
656 mPositionUs = -1;
657 mLooping = false;
658 mPlayingTimeUs = 0;
659
660 return OK;
661}
可以看到如果状态不对的话, 630 行返回 return INVALID_OPERATION;
这个定义 INVALID_OPERATION 为android::INVALID_OPERATION, 其文件路径为 system/core/include/utils/Errors.h
45enum {
46 OK = 0, // Everything's swell.
47 NO_ERROR = 0, // No errors.
48
49 UNKNOWN_ERROR = (-2147483647-1), // INT32_MIN value
50
51 NO_MEMORY = -ENOMEM,
52 INVALID_OPERATION = -ENOSYS,
53 BAD_VALUE = -EINVAL,
54 BAD_TYPE = (UNKNOWN_ERROR + 1),
55 NAME_NOT_FOUND = -ENOENT,
56 PERMISSION_DENIED = -EPERM,
57 NO_INIT = -ENODEV,
58 ALREADY_EXISTS = -EEXIST,
59 DEAD_OBJECT = -EPIPE,
60 FAILED_TRANSACTION = (UNKNOWN_ERROR + 2),
61#if !defined(_WIN32)
62 BAD_INDEX = -EOVERFLOW,
63 NOT_ENOUGH_DATA = -ENODATA,
64 WOULD_BLOCK = -EWOULDBLOCK,
65 TIMED_OUT = -ETIMEDOUT,
66 UNKNOWN_TRANSACTION = -EBADMSG,
67#else
68 BAD_INDEX = -E2BIG,
69 NOT_ENOUGH_DATA = (UNKNOWN_ERROR + 3),
70 WOULD_BLOCK = (UNKNOWN_ERROR + 4),
71 TIMED_OUT = (UNKNOWN_ERROR + 5),
72 UNKNOWN_TRANSACTION = (UNKNOWN_ERROR + 6),
73#endif
74 FDS_NOT_ALLOWED = (UNKNOWN_ERROR + 7),
75 UNEXPECTED_NULL = (UNKNOWN_ERROR + 8),
76};
INVALID_OPERATION = -ENOSYS,
bionic/libc/kernel/uapi/asm-generic/errno.h
21#include <asm-generic/errno-base.h>
22#define EDEADLK 35
23#define ENAMETOOLONG 36
24#define ENOLCK 37
25#define ENOSYS 38
26#define ENOTEMPTY 39
27#define ELOOP 40
28#define EWOULDBLOCK EAGAIN
29#define ENOMSG 42
30#define EIDRM 43
31#define ECHRNG 44
32#define EL2NSYNC 45
33#define EL3HLT 46
34#define EL3RST 47
35#define ELNRNG 48
36#define EUNATCH 49
37#define ENOCSI 50
38#define EL2HLT 51
39#define EBADE 52
40#define EBADR 53
41#define EXFULL 54
42#define ENOANO 55
43#define EBADRQC 56
44#define EBADSLT 57
45#define EDEADLOCK EDEADLK
46#define EBFONT 59
47#define ENOSTR 60
48#define ENODATA 61
49#define ETIME 62
50#define ENOSR 63
51#define ENONET 64
52#define ENOPKG 65
53#define EREMOTE 66
54#define ENOLINK 67
55#define EADV 68
56#define ESRMNT 69
57#define ECOMM 70
58#define EPROTO 71
59#define EMULTIHOP 72
60#define EDOTDOT 73
61#define EBADMSG 74
62#define EOVERFLOW 75
63#define ENOTUNIQ 76
64#define EBADFD 77
65#define EREMCHG 78
66#define ELIBACC 79
67#define ELIBBAD 80
68#define ELIBSCN 81
69#define ELIBMAX 82
70#define ELIBEXEC 83
71#define EILSEQ 84
72#define ERESTART 85
73#define ESTRPIPE 86
74#define EUSERS 87
75#define ENOTSOCK 88
76#define EDESTADDRREQ 89
77#define EMSGSIZE 90
78#define EPROTOTYPE 91
79#define ENOPROTOOPT 92
80#define EPROTONOSUPPORT 93
81#define ESOCKTNOSUPPORT 94
82#define EOPNOTSUPP 95
83#define EPFNOSUPPORT 96
84#define EAFNOSUPPORT 97
85#define EADDRINUSE 98
86#define EADDRNOTAVAIL 99
87#define ENETDOWN 100
88#define ENETUNREACH 101
89#define ENETRESET 102
90#define ECONNABORTED 103
91#define ECONNRESET 104
92#define ENOBUFS 105
93#define EISCONN 106
94#define ENOTCONN 107
95#define ESHUTDOWN 108
96#define ETOOMANYREFS 109
97#define ETIMEDOUT 110
98#define ECONNREFUSED 111
99#define EHOSTDOWN 112
100#define EHOSTUNREACH 113
101#define EALREADY 114
102#define EINPROGRESS 115
103#define ESTALE 116
104#define EUCLEAN 117
105#define ENOTNAM 118
106#define ENAVAIL 119
107#define EISNAM 120
108#define EREMOTEIO 121
109#define EDQUOT 122
110#define ENOMEDIUM 123
111#define EMEDIUMTYPE 124
112#define ECANCELED 125
113#define ENOKEY 126
114#define EKEYEXPIRED 127
115#define EKEYREVOKED 128
116#define EKEYREJECTED 129
117#define EOWNERDEAD 130
118#define ENOTRECOVERABLE 131
119#define ERFKILL 132
120#define EHWPOISON 133
121#endif
这个值就是 -38,如果使用MediaPlayer 使用状态不对就会返回这个给上层
网友评论