<commit_before>package org.prx.android.playerhater;

import java.io.FileDescriptor;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;

public class TransientPlayer {

	public static void play(Context c, String url, boolean isDuckable) {
		new TransientPlayer(c, url, isDuckable).play();
	}

	public static void play(Context c, FileDescriptor file, boolean isDuckable) {
		new TransientPlayer(c, file, isDuckable).play();
	}

	private final Context c;
	private final MediaPlayerWrapper wrapper;
	private final boolean isDuckable;
	private final String url;
	private final FileDescriptor file;
	private final int playType;
	private static final int FILE = 1;
	private static final int URL = 2;

	protected TransientPlayer(Context c, String url, boolean isDuckable) {
		this.c = c;
		this.wrapper = new MediaPlayerWrapper();
		this.file = null;
		this.url = url;
		this.isDuckable = isDuckable;
		this.playType = URL;
	}

	protected TransientPlayer(Context c, FileDescriptor file, boolean isDuckable) {
		this.c = c;
		this.wrapper = new MediaPlayerWrapper();
		this.url = null;
		this.file = file;
		this.isDuckable = isDuckable;
		this.playType = FILE;
	}
	
	private void play() {
		TransientPlayerTask task = new TransientPlayerTask();
		Thread thread = new Thread(task);
		thread.run();
	}
	
	private int getDurationHint() {
		if (isDuckable) {
			return AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK;
		} else  {
			return AudioManager.AUDIOFOCUS_GAIN_TRANSIENT;
		}
	}
	
	private class TransientPlayerTask implements Runnable {
		
		@Override
		public void run() {
			
			wrapper.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);
			
			try {
				switch (playType) {
				case FILE:
					wrapper.setDataSource(file);
					break;
				case URL:
					wrapper.setDataSource(url);
					break;
				}
				wrapper.prepare();
			} catch (Exception e) {
				e.printStackTrace();
				return;
			}
			
			final AudioManager audioManager = (AudioManager)c.getSystemService(Context.AUDIO_SERVICE);
			
			final AudioManager.OnAudioFocusChangeListener audioFocusListener = new AudioManager.OnAudioFocusChangeListener() {
				@Override
				public void onAudioFocusChange(int focusChange) {
					if (focusChange == getDurationHint())
						wrapper.start();
				}
			};
			
			audioManager.requestAudioFocus(audioFocusListener, AudioManager.STREAM_NOTIFICATION, getDurationHint());
			
			wrapper.setOnCompletionListener(new OnCompletionListener() {

				@Override
				public void onCompletion(MediaPlayer mp) {
					audioManager.abandonAudioFocus(audioFocusListener);
					wrapper.release();
				}
				
			});
		}
		
	}
}<commit_msg>I guess I didnt understand the way that requesting focus would work. this should be better<commit_after>package org.prx.android.playerhater;

import java.io.FileDescriptor;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.util.Log;
import android.media.MediaPlayer.OnCompletionListener;

public class TransientPlayer {

	private static final String TAG = "TransientPlayer";

	public static void play(Context c, String url, boolean isDuckable) {
		new TransientPlayer(c, url, isDuckable).play();
	}

	public static void play(Context c, FileDescriptor file, boolean isDuckable) {
		new TransientPlayer(c, file, isDuckable).play();
	}

	private final Context c;
	private final MediaPlayerWrapper wrapper;
	private final boolean isDuckable;
	private final String url;
	private final FileDescriptor file;
	private final int playType;
	private static final int FILE = 1;
	private static final int URL = 2;

	protected TransientPlayer(Context c, String url, boolean isDuckable) {
		this.c = c;
		this.wrapper = new MediaPlayerWrapper();
		this.file = null;
		this.url = url;
		this.isDuckable = isDuckable;
		this.playType = URL;
	}

	protected TransientPlayer(Context c, FileDescriptor file, boolean isDuckable) {
		this.c = c;
		this.wrapper = new MediaPlayerWrapper();
		this.url = null;
		this.file = file;
		this.isDuckable = isDuckable;
		this.playType = FILE;
	}

	private void play() {
		TransientPlayerTask task = new TransientPlayerTask();
		Thread thread = new Thread(task);
		thread.run();
	}

	private int getDurationHint() {
		if (isDuckable) {
			return AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK;
		} else  {
			return AudioManager.AUDIOFOCUS_GAIN_TRANSIENT;
		}
	}

	private class TransientPlayerTask implements Runnable {

		@Override
		public void run() {

			wrapper.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);

			try {
				switch (playType) {
				case FILE:
					wrapper.setDataSource(file);
					break;
				case URL:
					wrapper.setDataSource(url);
					break;
				}
				wrapper.prepare();
			} catch (Exception e) {
				e.printStackTrace();
				return;
			}

			final AudioManager audioManager = (AudioManager)c.getSystemService(Context.AUDIO_SERVICE);

			final AudioManager.OnAudioFocusChangeListener audioFocusListener = new AudioManager.OnAudioFocusChangeListener() {
				@Override
				public void onAudioFocusChange(int focusChange) {
					// I don't think we need to do anything here.
					// generally, we might want to stop the audio in
					// the event that we have lost focus in a non-
					// transient way, I think.
				}
			};

			wrapper.setOnCompletionListener(new OnCompletionListener() {

				@Override
				public void onCompletion(MediaPlayer mp) {
					audioManager.abandonAudioFocus(audioFocusListener);
					wrapper.release();
				}

			});

			int status = audioManager.requestAudioFocus(audioFocusListener, AudioManager.STREAM_NOTIFICATION, getDurationHint());

			if (status == AudioManager.AUDIOFOCUS_REQUEST_GRANTED)
				wrapper.start();
		}

	}
}
{'fraction_non_alphanumeric': 0.08959338387319091, 'fraction_numerical': 0.0006891798759476223, 'mean_word_length': 4.017286084701815, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3134095', 'n_tokens_mistral': 2050, 'n_tokens_neox': 1729, 'n_words': 449}