Added play/pause functionality.

This commit is contained in:
Felix Ableitner 2013-06-02 18:39:04 +02:00
parent c4fcf1a289
commit a44f88729c
4 changed files with 147 additions and 23 deletions

View file

@ -1,16 +0,0 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity$DummySectionFragment" >
<TextView
android:id="@+id/section_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>

View file

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
</ListView>
<Button
android:id="@+id/playpause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:onClick="playPauseClicked"
android:visibility="gone" />
</LinearLayout>

View file

@ -4,5 +4,7 @@
<string name="app_name">ControlDLNA</string> <string name="app_name">ControlDLNA</string>
<string name="title_renderer">Renderer</string> <string name="title_renderer">Renderer</string>
<string name="title_server">Server</string> <string name="title_server">Server</string>
<string name="play">Play</string>
<string name="pause">Pause</string>
</resources> </resources>

View file

@ -13,6 +13,7 @@ import org.teleal.cling.registry.Registry;
import org.teleal.cling.registry.RegistryListener; import org.teleal.cling.registry.RegistryListener;
import org.teleal.cling.support.avtransport.callback.Play; import org.teleal.cling.support.avtransport.callback.Play;
import org.teleal.cling.support.avtransport.callback.SetAVTransportURI; import org.teleal.cling.support.avtransport.callback.SetAVTransportURI;
import org.teleal.cling.support.avtransport.callback.Stop;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.Context; import android.content.Context;
@ -20,9 +21,15 @@ import android.content.Intent;
import android.content.ServiceConnection; import android.content.ServiceConnection;
import android.os.Bundle; import android.os.Bundle;
import android.os.IBinder; import android.os.IBinder;
import android.support.v4.app.ListFragment; import android.support.v4.app.Fragment;
import android.util.Log; import android.util.Log;
import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView; import android.widget.ListView;
import android.widget.Toast; import android.widget.Toast;
@ -34,10 +41,17 @@ import com.github.nutomic.controldlna.MainActivity.OnBackPressedListener;
* @author Felix * @author Felix
* *
*/ */
public class RendererFragment extends ListFragment implements OnBackPressedListener { public class RendererFragment extends Fragment implements
OnBackPressedListener, OnItemClickListener, OnClickListener {
private final String TAG = "RendererFragment"; private final String TAG = "RendererFragment";
private ListView mListView;
private Button mPlayPause;
private boolean mPlaying = false;
/** /**
* ListView adapter of media renderers. * ListView adapter of media renderers.
*/ */
@ -152,6 +166,12 @@ public class RendererFragment extends ListFragment implements OnBackPressedListe
} }
}; };
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.renderer_fragment, null);
};
/** /**
* Initializes ListView adapters, launches Cling UPNP service. * Initializes ListView adapters, launches Cling UPNP service.
*/ */
@ -159,8 +179,12 @@ public class RendererFragment extends ListFragment implements OnBackPressedListe
public void onActivityCreated(Bundle savedInstanceState) { public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState); super.onActivityCreated(savedInstanceState);
mRendererAdapter = new DeviceArrayAdapter(getActivity()); mRendererAdapter = new DeviceArrayAdapter(getActivity());
mListView = (ListView) getView().findViewById(R.id.listview);
setListAdapter(mRendererAdapter); mListView.setAdapter(mRendererAdapter);
mListView.setOnItemClickListener(this);
mPlayPause = (Button) getView().findViewById(R.id.playpause);
mPlayPause.setOnClickListener(this);
mPlayPause.setText(R.string.play);
getActivity().getApplicationContext().bindService( getActivity().getApplicationContext().bindService(
new Intent(getActivity(), AndroidUpnpServiceImpl.class), new Intent(getActivity(), AndroidUpnpServiceImpl.class),
@ -216,6 +240,10 @@ public class RendererFragment extends ListFragment implements OnBackPressedListe
UpnpResponse operation, String defaultMsg) { UpnpResponse operation, String defaultMsg) {
Log.w(TAG, "Playback failed: " + defaultMsg); Log.w(TAG, "Playback failed: " + defaultMsg);
} }
@Override
public void success(ActionInvocation invocation) {
playbackStarted();
}
}); });
} }
}); });
@ -231,10 +259,11 @@ public class RendererFragment extends ListFragment implements OnBackPressedListe
* Selects a media renderer. * Selects a media renderer.
*/ */
@Override @Override
public void onListItemClick(ListView l, View v, int position, long id) { public void onItemClick(AdapterView<?> a, View v, int position, long id) {
if (mCurrentRenderer == null) { if (mCurrentRenderer == null) {
mCurrentRenderer = mRendererAdapter.getItem(position); mCurrentRenderer = mRendererAdapter.getItem(position);
setListAdapter(null); mListView.setAdapter(null);
mPlayPause.setVisibility(View.VISIBLE);
if (!mCachedPlayUri.equals("")) { if (!mCachedPlayUri.equals("")) {
play(mCachedPlayUri); play(mCachedPlayUri);
mCachedPlayUri = ""; mCachedPlayUri = "";
@ -248,11 +277,98 @@ public class RendererFragment extends ListFragment implements OnBackPressedListe
@Override @Override
public boolean onBackPressed() { public boolean onBackPressed() {
if (mCurrentRenderer != null) { if (mCurrentRenderer != null) {
setListAdapter(mRendererAdapter); mListView.setAdapter(mRendererAdapter);
mCurrentRenderer = null; mCurrentRenderer = null;
mPlayPause.setVisibility(View.GONE);
return true; return true;
} }
return false; return false;
} }
/**
* Plays/pauses playback on button click.
*/
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.playpause:
final Service<?, ?> service = mCurrentRenderer.findService(
new ServiceType("schemas-upnp-org", "AVTransport"));
if (mPlaying) {
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);
}
@Override
public void success(ActionInvocation invocation) {
playbackPaused();
};
});
}
@SuppressWarnings("rawtypes")
@Override
public void success(ActionInvocation invocation) {
playbackPaused();
};
});
} 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);
}
@SuppressWarnings("rawtypes")
@Override
public void success(ActionInvocation invocation) {
playbackStarted();
};
});
}
}
}
/**
* Sets button text and playing attribute.
* Call this after pausing/stopping playback.
*/
private void playbackPaused() {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
mPlaying = false;
mPlayPause.setText(R.string.play);
}
});
}
/**
* Sets button text and playing attribute.
* Call this after starting playback.
*/
private void playbackStarted() {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
mPlaying = true;
mPlayPause.setText(R.string.pause);
}
});
}
} }