As simple as it gets...
MediaPlayer player = MediaPlayer.create(context, R.raw.somemp3file);
player.start();
But nothing happens? From: http://developer.android.com/guide/topics/media/index.html
Android 2.2 @ HTC Desire
MediaPlayerService
06-11 22:11:00.392: VERBOSE/MediaPlayerService(18690): Client(28) constructor
06-11 22:11:00.392: VERBOSE/MediaPlayerService(18690): Create new client(28) from pid 14284, fd=15, offset=26236, length=14760
06-11 22:11:00.392: VERBOSE/MediaPlayerService(18690): setDataSource fd=15, offset=26236, length=14760
06-11 22:11:00.392: VERBOSE/MediaPlayerService(18690): st_dev = 7941
06-11 22:11:00.392: VERBOSE/MediaPlayerService(18690): st_mode = 33188
06-11 22:11:00.392: VERBOSE/MediaPlayerService(18690): st_uid = 1000
06-11 22:11:00.392: VERBOSE/MediaPlayerService(18690): st_gid = 1000
06-11 22:11:00.392: VERBOSE/MediaPlayerService(18690): st_size = 973395
06-11 22:11:00.392: VERBOSE/MediaPlayerService(18690): player type = 4
06-11 22:11:00.392: DEBUG/MediaPlayerService(18690): player type = STAGEFRIGHT_PLAYER
06-11 22:11:00.392: VERBOSE/MediaPlayerService(18690): create StagefrightPlayer
06-11 22:11:00.392: VERBOSE/MediaPlayerService(18690): [28] setAudioStreamType(3)
06-11 22:11:00.402: VERBOSE/MediaPlayerService(18690): [28] prepareAsync
06-11 22:11:00.402: VERBOSE/MediaPlayerService(18690): [28] notify (0x37b60, 5, 0, 0)
06-11 22:11:00.402: VERBOSE/MediaPlayerService(18690): [28] notify (0x37b60, 1, 0, 0)
06-11 22:11:00.402: VERBOSE/MediaPlayerService(18690): [28] setLooping(0)
06-11 22:11:00.402: VERBOSE/MediaPlayerService(18690): [28] setVolume(1.000000, 1.000000)
06-11 22:11:00.402: VERBOSE/MediaPlayerService(18690): [28] start
06-11 22:11:00.592: VERBOSE/MediaPlayerService(18690): Client(29) constructor
06-11 22:11:00.592: VERBOSE/MediaPlayerService(18690): Create new client(29) from pid 14284, fd=19, offset=26236, length=14760
06-11 22:11:00.592: VERBOSE/MediaPlayerService(18690): setDataSource fd=19, offset=26236, length=14760
06-11 22:11:00.592: VERBOSE/MediaPlayerService(18690): st_dev = 7941
06-11 22:11:00.592: VERBOSE/MediaPlayerService(18690): st_mode = 33188
06-11 22:11:00.592: VERBOSE/MediaPlayerService(18690): st_uid = 1000
06-11 22:11:00.592: VERBOSE/MediaPlayerService(18690): st_gid = 1000
06-11 22:11:00.592: VERBOSE/MediaPlayerService(18690): st_size = 973395
06-11 22:11:00.592: VERBOSE/MediaPlayerService(18690): player type = 4
06-11 22:11:00.592: DEBUG/MediaPlayerService(18690): player type = STAGEFRIGHT_PLAYER
06-11 22:11:00.592: VERB开发者_运维问答OSE/MediaPlayerService(18690): create StagefrightPlayer
06-11 22:11:00.592: VERBOSE/MediaPlayerService(18690): [29] setAudioStreamType(3)
06-11 22:11:00.592: VERBOSE/MediaPlayerService(18690): [29] prepareAsync
06-11 22:11:00.592: VERBOSE/MediaPlayerService(18690): [29] notify (0x26f10, 5, 0, 0)
06-11 22:11:00.592: VERBOSE/MediaPlayerService(18690): [29] notify (0x26f10, 1, 0, 0)
06-11 22:11:00.592: VERBOSE/MediaPlayerService(18690): [29] setLooping(0)
06-11 22:11:00.592: VERBOSE/MediaPlayerService(18690): [29] setVolume(1.000000, 1.000000)
06-11 22:11:00.592: VERBOSE/MediaPlayerService(18690): [29] start
06-11 22:11:00.702: VERBOSE/MediaPlayerService(18690): [28] notify (0x37b60, 2, 0, 0)
06-11 22:11:00.792: VERBOSE/MediaPlayerService(18690): [29] notify (0x26f10, 2, 0, 0)
06-11 22:11:12.846: VERBOSE/MediaPlayerService(18690): disconnect(29) from pid 14284
06-11 22:11:12.846: VERBOSE/MediaPlayerService(18690): Client(29) destructor pid = 14284
06-11 22:11:12.846: VERBOSE/MediaPlayerService(18690): disconnect(29) from pid 14284
06-11 22:14:06.450: VERBOSE/MediaPlayerService(18690): Client(28) destructor pid = 14284
06-11 22:14:06.450: VERBOSE/MediaPlayerService(18690): disconnect(28) from pid 14284
06-11 22:22:00.460: VERBOSE/MediaPlayerService(18690): Client(30) constructor
06-11 22:22:00.460: VERBOSE/MediaPlayerService(18690): Create new client(30) from pid 14698, fd=15, offset=26233, length=14760
06-11 22:22:00.460: VERBOSE/MediaPlayerService(18690): setDataSource fd=15, offset=26233, length=14760
06-11 22:22:00.460: VERBOSE/MediaPlayerService(18690): st_dev = 7941
06-11 22:22:00.460: VERBOSE/MediaPlayerService(18690): st_mode = 33188
06-11 22:22:00.460: VERBOSE/MediaPlayerService(18690): st_uid = 1000
06-11 22:22:00.460: VERBOSE/MediaPlayerService(18690): st_gid = 1000
06-11 22:22:00.460: VERBOSE/MediaPlayerService(18690): st_size = 973147
06-11 22:22:00.460: VERBOSE/MediaPlayerService(18690): player type = 4
06-11 22:22:00.460: DEBUG/MediaPlayerService(18690): player type = STAGEFRIGHT_PLAYER
06-11 22:22:00.460: VERBOSE/MediaPlayerService(18690): create StagefrightPlayer
06-11 22:22:00.470: VERBOSE/MediaPlayerService(18690): [30] setAudioStreamType(3)
06-11 22:22:00.470: VERBOSE/MediaPlayerService(18690): [30] prepareAsync
06-11 22:22:00.470: VERBOSE/MediaPlayerService(18690): [30] notify (0x37b60, 5, 0, 0)
06-11 22:22:00.470: VERBOSE/MediaPlayerService(18690): [30] notify (0x37b60, 1, 0, 0)
06-11 22:22:00.470: VERBOSE/MediaPlayerService(18690): [30] setLooping(0)
06-11 22:22:00.470: VERBOSE/MediaPlayerService(18690): [30] setVolume(1.000000, 1.000000)
06-11 22:22:00.470: VERBOSE/MediaPlayerService(18690): [30] start
06-11 22:22:00.711: VERBOSE/MediaPlayerService(18690): disconnect(30) from pid 14698
06-11 22:22:00.711: VERBOSE/MediaPlayerService(18690): Client(30) destructor pid = 14698
06-11 22:22:00.711: VERBOSE/MediaPlayerService(18690): disconnect(30) from pid 14698
06-11 22:22:00.730: VERBOSE/MediaPlayerService(18690): Client(31) constructor
06-11 22:22:00.730: VERBOSE/MediaPlayerService(18690): Create new client(31) from pid 14698, fd=18, offset=26233, length=14760
06-11 22:22:00.730: VERBOSE/MediaPlayerService(18690): setDataSource fd=18, offset=26233, length=14760
06-11 22:22:00.730: VERBOSE/MediaPlayerService(18690): st_dev = 7941
06-11 22:22:00.730: VERBOSE/MediaPlayerService(18690): st_mode = 33188
06-11 22:22:00.730: VERBOSE/MediaPlayerService(18690): st_uid = 1000
06-11 22:22:00.730: VERBOSE/MediaPlayerService(18690): st_gid = 1000
06-11 22:22:00.730: VERBOSE/MediaPlayerService(18690): st_size = 973147
06-11 22:22:00.740: VERBOSE/MediaPlayerService(18690): player type = 4
06-11 22:22:00.740: DEBUG/MediaPlayerService(18690): player type = STAGEFRIGHT_PLAYER
06-11 22:22:00.740: VERBOSE/MediaPlayerService(18690): create StagefrightPlayer
06-11 22:22:00.750: VERBOSE/MediaPlayerService(18690): [31] setAudioStreamType(3)
06-11 22:22:00.750: VERBOSE/MediaPlayerService(18690): [31] prepareAsync
06-11 22:22:00.750: VERBOSE/MediaPlayerService(18690): [31] notify (0x37b60, 5, 0, 0)
06-11 22:22:00.750: VERBOSE/MediaPlayerService(18690): [31] notify (0x37b60, 1, 0, 0)
06-11 22:22:00.750: VERBOSE/MediaPlayerService(18690): [31] setLooping(0)
06-11 22:22:00.750: VERBOSE/MediaPlayerService(18690): [31] setVolume(1.000000, 1.000000)
06-11 22:22:00.750: VERBOSE/MediaPlayerService(18690): [31] start
06-11 22:22:01.060: VERBOSE/MediaPlayerService(18690): [31] notify (0x37b60, 2, 0, 0)
AudioSink
06-11 22:11:00.402: VERBOSE/AudioSink(18690): setVolume(1.000000, 1.000000)
06-11 22:11:00.402: VERBOSE/AudioSink(18690): open(44100, 1, 1, 4)
06-11 22:11:00.402: VERBOSE/AudioSink(18690): setVolume
06-11 22:11:00.402: VERBOSE/AudioSink(18690): start
06-11 22:11:00.592: VERBOSE/AudioSink(18690): setVolume(1.000000, 1.000000)
06-11 22:11:00.592: VERBOSE/AudioSink(18690): open(44100, 1, 1, 4)
06-11 22:11:00.592: VERBOSE/AudioSink(18690): setVolume
06-11 22:11:00.592: VERBOSE/AudioSink(18690): start
06-11 22:11:00.702: VERBOSE/AudioSink(18690): pause
06-11 22:11:00.792: VERBOSE/AudioSink(18690): pause
06-11 22:11:12.846: VERBOSE/AudioSink(18690): stop
06-11 22:11:12.846: VERBOSE/AudioSink(18690): close
06-11 22:11:12.846: VERBOSE/AudioSink(18690): close
06-11 22:14:06.450: VERBOSE/AudioSink(18690): stop
06-11 22:14:06.450: VERBOSE/AudioSink(18690): close
06-11 22:14:06.450: VERBOSE/AudioSink(18690): close
06-11 22:22:00.470: VERBOSE/AudioSink(18690): setVolume(1.000000, 1.000000)
06-11 22:22:00.470: VERBOSE/AudioSink(18690): open(44100, 1, 1, 4)
06-11 22:22:00.470: VERBOSE/AudioSink(18690): setVolume
06-11 22:22:00.470: VERBOSE/AudioSink(18690): start
06-11 22:22:00.711: VERBOSE/AudioSink(18690): stop
06-11 22:22:00.711: VERBOSE/AudioSink(18690): close
06-11 22:22:00.711: VERBOSE/AudioSink(18690): close
06-11 22:22:00.750: VERBOSE/AudioSink(18690): setVolume(1.000000, 1.000000)
06-11 22:22:00.750: VERBOSE/AudioSink(18690): open(44100, 1, 1, 4)
06-11 22:22:00.750: VERBOSE/AudioSink(18690): setVolume
06-11 22:22:00.750: VERBOSE/AudioSink(18690): start
06-11 22:22:01.060: VERBOSE/AudioSink(18690): pause
This is one from plain simple Android Project with just those two lines.
06-11 22:55:40.521: INFO/ActivityManager(96): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.test.test/.Test2Activity }
06-11 22:55:40.531: INFO/AndroidRuntime(15849): NOTE: attach of thread 'Binder Thread #3' failed
06-11 22:55:40.710: DEBUG/ProtoRequestListener(96): requestFailed()
06-11 22:55:40.940: DEBUG/dalvikvm(2564): GC_EXPLICIT freed 43 objects / 2120 bytes in 66ms
06-11 22:55:42.870: DEBUG/SurfaceFlinger(96): Layer::setBuffers(this=0x7a7100), pid=207, w=480, h=800
06-11 22:55:42.870: DEBUG/SurfaceFlinger(96): Layer::setBuffers(this=0x7a7100), pid=207, w=480, h=800
06-11 22:55:42.890: DEBUG/SurfaceFlinger(96): Layer::requestBuffer(this=0x7a7100), index=0, pid=207, w=480, h=800 success
06-11 22:55:43.190: DEBUG/SurfaceFlinger(96): Layer::requestBuffer(this=0x7a7100), index=1, pid=207, w=480, h=800 success
06-11 22:55:45.080: DEBUG/AndroidRuntime(15862): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
06-11 22:55:45.080: DEBUG/AndroidRuntime(15862): CheckJNI is OFF
06-11 22:55:45.080: DEBUG/dalvikvm(15862): creating instr width table
06-11 22:55:45.130: DEBUG/AndroidRuntime(15862): --- registering native functions ---
06-11 22:55:45.390: DEBUG/AndroidRuntime(15862): Shutting down VM
06-11 22:55:45.390: DEBUG/dalvikvm(15862): Debugger has detached; object registry had 1 entries
06-11 22:55:45.400: INFO/AndroidRuntime(15862): NOTE: attach of thread 'Binder Thread #3' failed
06-11 22:55:45.540: DEBUG/AndroidRuntime(15871): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
06-11 22:55:45.540: DEBUG/AndroidRuntime(15871): CheckJNI is OFF
06-11 22:55:45.540: DEBUG/dalvikvm(15871): creating instr width table
06-11 22:55:45.591: DEBUG/AndroidRuntime(15871): --- registering native functions ---
06-11 22:55:45.850: INFO/ActivityManager(96): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.test.test/.Test2Activity }
06-11 22:55:45.860: DEBUG/SurfaceFlinger(96): Layer::setBuffers(this=0x6d7d70), pid=96, w=1, h=1
06-11 22:55:45.860: DEBUG/SurfaceFlinger(96): Layer::setBuffers(this=0x6d7d70), pid=96, w=1, h=1
06-11 22:55:45.880: DEBUG/AndroidRuntime(15871): Shutting down VM
06-11 22:55:45.880: DEBUG/dalvikvm(15871): Debugger has detached; object registry had 1 entries
06-11 22:55:45.890: DEBUG/SurfaceFlinger(96): Layer::requestBuffer(this=0x6d7d70), index=0, pid=96, w=480, h=800 success
06-11 22:55:45.910: INFO/AndroidRuntime(15871): NOTE: attach of thread 'Binder Thread #3' failed
06-11 22:55:45.930: INFO/MediaPlayer(15789): MediaPlayer create(context,resid)
06-11 22:55:45.930: INFO/MediaPlayer(15789): MediaPlayer
06-11 22:55:45.940: VERBOSE/AudioCache(18690): We only support url check function now.
06-11 22:55:45.940: VERBOSE/AudioCache(18690): isQCPFileFormat() url=(null)
06-11 22:55:45.940: DEBUG/AwesomePlayer(18690): [U5B] reset (382)
06-11 22:55:45.940: DEBUG/AwesomePlayer(18690): [U5B] reset_l (388)
06-11 22:55:45.940: DEBUG/AwesomePlayer(18690): [U5B] reset_l (478)
06-11 22:55:45.940: DEBUG/AwesomePlayer(18690): [U5B] setDataSource (319)
06-11 22:55:45.940: DEBUG/AwesomePlayer(18690): [U5B] reset_l (388)
06-11 22:55:45.940: DEBUG/AwesomePlayer(18690): [U5B] reset_l (478)
06-11 22:55:45.940: DEBUG/AwesomePlayer(18690): [U5B] setDataSource_l (339)
06-11 22:55:45.940: DEBUG/MediaExtractor(18690): [U5B] Create (47)
06-11 22:55:45.940: VERBOSE/MediaExtractor(18690): Autodetected media content as 'audio/mpeg' with confidence 0.30
06-11 22:55:45.940: DEBUG/AwesomePlayer(18690): [U5B] setDataSource_l (350)
06-11 22:55:45.950: INFO/MediaPlayer(15789): MediaPlayer start()
06-11 22:55:45.950: DEBUG/AwesomePlayer(18690): [U5B] play (556)
06-11 22:55:45.950: DEBUG/AwesomePlayer(18690): [U5B] play_l (562)
06-11 22:55:45.950: DEBUG/AudioPolicyManagerBase(18690): startOutput() output 1, stream 3
06-11 22:55:45.950: DEBUG/AwesomePlayer(18690): [U5B] play_l (635)
06-11 22:55:45.961: INFO/MediaPlayer(15789): MediaPlayer handleMessage what=5
06-11 22:55:45.961: INFO/MediaPlayer(15789): MediaPlayer handleMessage what=1
06-11 22:55:45.961: DEBUG/SurfaceFlinger(96): Layer::setBuffers(this=0x668610), pid=15789, w=1, h=1
06-11 22:55:45.961: DEBUG/SurfaceFlinger(96): Layer::setBuffers(this=0x668610), pid=15789, w=1, h=1
06-11 22:55:45.990: DEBUG/SurfaceFlinger(96): Layer::requestBuffer(this=0x668610), index=0, pid=15789, w=480, h=800 success
06-11 22:55:46.000: INFO/ActivityManager(96): Displayed activity com.test.test/.Test2Activity: 122 ms (total 122 ms)
06-11 22:55:46.150: INFO/AudioHardwareQSD(18690): AUDIO_START: start kernel pcm_out driver.
06-11 22:55:46.162: WARN/AudioFlinger(18690): write blocked for 204 msecs, 46 delayed writes, thread 0x15440
06-11 22:55:46.251: INFO/MediaPlayer(15789): MediaPlayer handleMessage what=2
06-11 22:55:46.251: DEBUG/AwesomePlayer(18690): [U5B] pause_l (705)
06-11 22:55:49.310: INFO/AudioHardwareQSD(18690): AudioHardware pcm playback is going to standby.
06-11 22:55:50.710: DEBUG/ProtoRequestListener(96): requestFailed()
Problem? You must specify audioStreamType before .prepare()! So this is working.
MediaPlayer player = new MediaPlayer();
AssetFileDescriptor afd = this.getResources().openRawResourceFd(R.raw.alarm);
player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
afd.close();
player.setAudioStreamType(AudioManager.STREAM_ALARM);
player.setLooping(true);
player.prepare();
player.start();
But if you remove
player.setAudioStreamType(AudioManager.STREAM_ALARM);
it won't work. If you use MediaPlayer.create(context, resid) - the static method itself will make prepare before you use
player.setAudioStreamType(AudioManager.STREAM_ALARM);
because body of the method is (see mp.prepare() before return statement)
public static MediaPlayer create(Context context, int resid) {
try {
AssetFileDescriptor afd = context.getResources().openRawResourceFd(resid);
if (afd == null)
return null;
MediaPlayer mp = new MediaPlayer();
mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
afd.close();
mp.prepare();
return mp;
} catch (IOException ex) {
Log.d(TAG, "create failed:", ex);
// fall through
} catch (IllegalArgumentException ex) {
Log.d(TAG, "create failed:", ex);
// fall through
} catch (SecurityException ex) {
Log.d(TAG, "create failed:", ex);
// fall through
}
return null;
}
So use the snippet at the beginning or use own static method
public static MediaPlayer create(Context context, int resid, boolean looping) {
try {
AssetFileDescriptor afd = context.getResources().openRawResourceFd(resid);
MediaPlayer mp = new MediaPlayer();
mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
afd.close();
mp.setAudioStreamType(AudioManager.STREAM_ALARM);
if (looping) mp.setLooping(true);
mp.prepare();
return mp;
} catch (IOException ex) {
Log.d("MediaPlayer", "create failed:", ex);
// fall through
} catch (IllegalArgumentException ex) {
Log.d("MediaPlayer", "create failed:", ex);
// fall through
} catch (SecurityException ex) {
Log.d("MediaPlayer", "create failed:", ex);
// fall through
}
return null;
}
and use it in your activity as (third argument is to loop the sound or not)
MediaPlayer player = MainActivity.create(this, R.raw.alarm, true);
player.start();
精彩评论