Changed buffering to be asynchronous.

This commit is contained in:
Felix Ableitner 2013-12-23 01:31:35 +01:00
parent 598522701e
commit ca0439bf8f
2 changed files with 16 additions and 16 deletions

View file

@ -14,7 +14,7 @@ import android.support.v7.media.MediaRouteProvider;
import android.support.v7.media.MediaRouter.ControlRequestCallback; import android.support.v7.media.MediaRouter.ControlRequestCallback;
public class Controller extends MediaRouteProvider.RouteController implements public class Controller extends MediaRouteProvider.RouteController implements
MediaPlayer.OnCompletionListener { MediaPlayer.OnCompletionListener, MediaPlayer.OnPreparedListener {
private Context mContext; private Context mContext;
@ -28,13 +28,14 @@ public class Controller extends MediaRouteProvider.RouteController implements
private int mState; private int mState;
MediaPlayer mPlayer = new MediaPlayer(); private MediaPlayer mPlayer = new MediaPlayer();
public Controller(String routeId, Context context) { public Controller(String routeId, Context context) {
mContext = context; mContext = context;
mRouteId = routeId; mRouteId = routeId;
mAudio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); mAudio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mPlayer.setOnPreparedListener(this);
} }
@Override @Override
@ -72,11 +73,10 @@ public class Controller extends MediaRouteProvider.RouteController implements
try { try {
mPlayer.reset(); mPlayer.reset();
mPlayer.setDataSource(mContext, intent.getData()); mPlayer.setDataSource(mContext, intent.getData());
mPlayer.prepare(); mPlayer.prepareAsync();
mPlayer.start();
mItemId = intent.getDataString(); mItemId = intent.getDataString();
mState = MediaItemStatus.PLAYBACK_STATE_PLAYING; mState = MediaItemStatus.PLAYBACK_STATE_BUFFERING;
getStatus(mItemId, mRouteId, callback); getStatus(mItemId, mRouteId, callback);
return true; return true;
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
mState = MediaItemStatus.PLAYBACK_STATE_ERROR; mState = MediaItemStatus.PLAYBACK_STATE_ERROR;
@ -135,11 +135,16 @@ public class Controller extends MediaRouteProvider.RouteController implements
.build().asBundle(); .build().asBundle();
callback.onResult(status); callback.onResult(status);
} }
@Override @Override
public void onCompletion(MediaPlayer mp) { public void onCompletion(MediaPlayer mp) {
mState = MediaItemStatus.PLAYBACK_STATE_FINISHED; mState = MediaItemStatus.PLAYBACK_STATE_FINISHED;
} }
@Override
public void onPrepared(MediaPlayer mp) {
mPlayer.start();
mState = MediaItemStatus.PLAYBACK_STATE_PLAYING;
}
} }

View file

@ -13,11 +13,6 @@ import android.support.v7.media.MediaRouteProvider;
import android.support.v7.media.MediaRouteProviderDescriptor; import android.support.v7.media.MediaRouteProviderDescriptor;
import android.support.v7.media.MediaRouter; import android.support.v7.media.MediaRouter;
/**
* Demonstrates how to create a custom media route provider.
*
* @see SampleMediaRouteProviderService
*/
final class Provider extends MediaRouteProvider { final class Provider extends MediaRouteProvider {
private static final String ROUTE_ID = "local_route"; private static final String ROUTE_ID = "local_route";