Changed renderer handling.

This commit is contained in:
Felix Ableitner 2013-06-06 11:10:34 +02:00
parent d8660afaeb
commit e8ce9eb70a
3 changed files with 89 additions and 55 deletions

View file

@ -6,5 +6,6 @@
<string name="title_server">Server</string> <string name="title_server">Server</string>
<string name="play">Play</string> <string name="play">Play</string>
<string name="pause">Pause</string> <string name="pause">Pause</string>
<string name="exit_renderer">Do you really want to exit the renderer? Playback will be stopped.</string>
</resources> </resources>

View file

@ -140,7 +140,7 @@ public class MainActivity extends SherlockFragmentActivity implements
*/ */
public void play(Item[] playlist, int start) { public void play(Item[] playlist, int start) {
getSupportActionBar().selectTab(getSupportActionBar().getTabAt(0)); getSupportActionBar().selectTab(getSupportActionBar().getTabAt(0));
mRendererFragment.play(playlist, start); mRendererFragment.setPlaylist(playlist, start);
} }
} }

View file

@ -25,8 +25,10 @@ import org.teleal.cling.support.avtransport.lastchange.AVTransportVariable;
import org.teleal.cling.support.lastchange.LastChange; import org.teleal.cling.support.lastchange.LastChange;
import org.teleal.cling.support.model.item.Item; import org.teleal.cling.support.model.item.Item;
import android.app.AlertDialog;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.ServiceConnection; import android.content.ServiceConnection;
import android.os.Bundle; import android.os.Bundle;
@ -229,20 +231,24 @@ public class RendererFragment extends Fragment implements
} }
/** /**
* Plays the URIs in playlist to the current renderer, or caches parameters * Sets the new playlist and starts playing it (if a renderer is selected).
* until a renderer is selected.
*
* @param playlist Array of URIs to play.
* @param start Index of the URI which should be played first.
*/ */
public void play(Item[] playlist, final int start) { public void setPlaylist(Item[] playlist, int start) {
mPlaylist = playlist; mPlaylist = playlist;
playTrack(start);
}
/**
* Plays the specified track in the current playlist, caches value if no
* renderer is selected.
*/
private void playTrack(int track) {
if (mCurrentRenderer != null) { if (mCurrentRenderer != null) {
mListView.setAdapter(mPlaylistAdapter); mListView.setAdapter(mPlaylistAdapter);
final Service<?, ?> service = mCurrentRenderer.findService( final Service<?, ?> service = mCurrentRenderer.findService(
new ServiceType("schemas-upnp-org", "AVTransport")); new ServiceType("schemas-upnp-org", "AVTransport"));
mUpnpService.getControlPoint().execute(new SetAVTransportURI(service, mUpnpService.getControlPoint().execute(new SetAVTransportURI(service,
playlist[start].getFirstResource().getValue(), "NO METADATA") { mPlaylist[track].getFirstResource().getValue(), "NO METADATA") {
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
@Override @Override
public void failure(ActionInvocation invocation, public void failure(ActionInvocation invocation,
@ -253,21 +259,14 @@ public class RendererFragment extends Fragment implements
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
@Override @Override
public void success(ActionInvocation invocation) { public void success(ActionInvocation invocation) {
mUpnpService.getControlPoint().execute(new Play(service) { play();
@Override
public void failure(ActionInvocation invocation,
UpnpResponse operation, String defaultMsg) {
Log.w(TAG, "Playback failed: " + defaultMsg);
}
});
} }
}); });
} }
else { else {
Toast.makeText(getActivity(), "Please select a renderer.", Toast.makeText(getActivity(), "Please select a renderer.",
Toast.LENGTH_SHORT).show(); Toast.LENGTH_SHORT).show();
mCachedStart = start; mCachedStart = track;
} }
} }
@ -276,7 +275,7 @@ public class RendererFragment extends Fragment implements
*/ */
@Override @Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) { public void onItemClick(AdapterView<?> a, View v, int position, long id) {
if (mCurrentRenderer == null) { if (mListView.getAdapter() == mRendererAdapter) {
mCurrentRenderer = mRendererAdapter.getItem(position); mCurrentRenderer = mRendererAdapter.getItem(position);
Service<?, ?> service = mCurrentRenderer.findService( Service<?, ?> service = mCurrentRenderer.findService(
new ServiceType("schemas-upnp-org", "AVTransport")); new ServiceType("schemas-upnp-org", "AVTransport"));
@ -312,7 +311,6 @@ public class RendererFragment extends Fragment implements
public void run() { public void run() {
mPlayPause.setText(R.string.pause); mPlayPause.setText(R.string.pause);
mPlaying = true; mPlaying = true;
Log.d(TAG, "play");
} }
}); });
break; break;
@ -325,7 +323,6 @@ public class RendererFragment extends Fragment implements
public void run() { public void run() {
mPlayPause.setText(R.string.play); mPlayPause.setText(R.string.play);
mPlaying = false; mPlaying = false;
Log.d(TAG, "stop");
} }
}); });
break; break;
@ -351,7 +348,7 @@ public class RendererFragment extends Fragment implements
}; };
mUpnpService.getControlPoint().execute(mSubscriptionCallback); mUpnpService.getControlPoint().execute(mSubscriptionCallback);
if (mCachedStart != -1) { if (mCachedStart != -1) {
play(mPlaylist, mCachedStart); setPlaylist(mPlaylist, mCachedStart);
mCachedStart = -1; mCachedStart = -1;
} }
@ -365,17 +362,38 @@ public class RendererFragment extends Fragment implements
*/ */
@Override @Override
public boolean onBackPressed() { public boolean onBackPressed() {
if (mCurrentRenderer != null) { if (mListView.getAdapter() == mPlaylistAdapter) {
mCurrentRenderer = null; if (mPlaying) {
mSubscriptionCallback.end(); new AlertDialog.Builder(getActivity())
.setMessage(R.string.exit_renderer)
.setPositiveButton(android.R.string.yes,
new DialogInterface.OnClickListener() {
mListView.setAdapter(mRendererAdapter); @Override
mPlayPause.setVisibility(View.GONE); public void onClick(DialogInterface dialog,
int which) {
pause();
exitPlaylistMode();
}
})
.setNegativeButton(android.R.string.no, null)
.show();
}
else
exitPlaylistMode();
return true; return true;
} }
return false; return false;
} }
private void exitPlaylistMode() {
mCurrentRenderer = null;
mSubscriptionCallback.end();
mListView.setAdapter(mRendererAdapter);
mPlayPause.setVisibility(View.GONE);
}
/** /**
* Plays/pauses playback on button click. * Plays/pauses playback on button click.
*/ */
@ -383,39 +401,54 @@ public class RendererFragment extends Fragment implements
public void onClick(View v) { public void onClick(View v) {
switch (v.getId()) { switch (v.getId()) {
case R.id.playpause: case R.id.playpause:
final Service<?, ?> service = mCurrentRenderer.findService( if (mPlaying)
new ServiceType("schemas-upnp-org", "AVTransport")); pause();
if (mPlaying) { else
mUpnpService.getControlPoint().execute(new Stop(service) { play();
@SuppressWarnings("rawtypes")
@Override
public void failure(ActionInvocation invocation,
UpnpResponse operation, String defaultMessage) {
Log.w(TAG, "Pause failed, trying stop: " + defaultMessage);
// Sometimes stop works even though pause does not.
mUpnpService.getControlPoint().execute(new Stop(service) {
@Override
public void failure(ActionInvocation invocation,
UpnpResponse operation, String defaultMessage) {
Log.w(TAG, "Stop failed: " + defaultMessage);
}
});
}
});
} else {
mUpnpService.getControlPoint().execute(new Play(service) {
@SuppressWarnings("rawtypes")
@Override
public void failure(ActionInvocation invocation,
UpnpResponse operation, String defaultMessage) {
Log.w(TAG, "Play failed: " + defaultMessage);
}
});
}
} }
} }
/**
* Sends 'pause' signal to current renderer.
*/
private void pause() {
final Service<?, ?> service = mCurrentRenderer.findService(
new ServiceType("schemas-upnp-org", "AVTransport"));
mUpnpService.getControlPoint().execute(new Stop(service) {
@SuppressWarnings("rawtypes")
@Override
public void failure(ActionInvocation invocation,
UpnpResponse operation, String defaultMessage) {
Log.w(TAG, "Pause failed, trying stop: " + defaultMessage);
// Sometimes stop works even though pause does not.
mUpnpService.getControlPoint().execute(new Stop(service) {
@Override
public void failure(ActionInvocation invocation,
UpnpResponse operation, String defaultMessage) {
Log.w(TAG, "Stop failed: " + defaultMessage);
}
});
}
});
}
/**
* Sends 'play' signal to current renderer.
*/
private void play() {
final Service<?, ?> service = mCurrentRenderer.findService(
new ServiceType("schemas-upnp-org", "AVTransport"));
mUpnpService.getControlPoint().execute(new Play(service) {
@SuppressWarnings("rawtypes")
@Override
public void failure(ActionInvocation invocation,
UpnpResponse operation, String defaultMessage) {
Log.w(TAG, "Play failed: " + defaultMessage);
}
});
}
} }