I'm creating a soundboard android app that will use multiple fragments to display buttons that when clicked will play a sound. The code I have so far uses two instances of MediaPlayer. I have no idea how to use a single instance of MediaPlayer while still having two fragments.
Here is my code:
package com.davidreadiii.android.soundboardexample1;
import android.content.res.AssetFileDescriptor;
import android.content.res.Resources;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import java.io.IOException;
public class MainFragment {
public static class Fragment1 extends Fragment {
int selectedSoundId;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.page1, container, false);
final MediaPlayer player = new MediaPlayer();
final Resources res = getResources();
final int[] buttonIds = { R.id.btn1, R.id.btn2, R.id.btn3 };
final int[] soundIds = {R.raw.giggity_giggity_goo, R.raw.hey_baby, R.raw.hump_day };
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
for (int i = 0; i < buttonIds.length; i++) {
if (v.getId() == buttonIds[i]) {
selectedSoundId = soundIds[i];
AssetFileDescriptor afd = res.openRawResourceFd(soundIds[i]);
player.reset();
try {
player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
player.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
player.start();
break;
}
}
}
};
for (int i = 0; i < buttonIds.length; i++) {
Button soundButton = (Button) rootView.findViewById(buttonIds[i]);
registerForContextMenu(soundButton);
soundButton.setOnClickListener(listener);
}
return rootView;
}
}
public static class Fragment2 extends Fragment {
int selectedSoundId;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.page2, container, false);
final MediaPlayer player = new MediaPlayer();
final Resources res = getResources();
final int[] buttonIds = { R.id.btn4, R.id.btn5, R.id.btn6 };
final int[] soundIds = { R.raw.ios_note, R.raw.old_spice, R.raw.you_suck };
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
for (int i = 0; i < buttonIds.length; i++) {
if (v.getId() == buttonIds[i]) {
selectedSoundId = soundIds[i];
AssetFileDescriptor afd = res.openRawResourceFd(soundIds[i]);
player.reset();
try {
player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
player.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
player.start();
break;
}
}
}
};
for (int i = 0; i < buttonIds.length; i++) {
Button soundButton = (Button) rootView.findViewById(buttonIds[i]);
registerForContextMenu(soundButton);
soundButton.setOnClickListener(listener);
}
return rootView;
}
}
}
To my knowledge, I might need to change the fragments to public classes instead of public static classes. Could someone help me find a solution??? Thank you.
You can use the Singleton Pattern:
public class Singleton {
private static Singleton mInstance = null;
private String mString;
private Singleton(){
mString = "Hello";
}
public static Singleton getInstance(){
if(mInstance == null)
{
mInstance = new Singleton();
}
return mInstance;
}
public String getString(){
return this.mString;
}
public void setString(String value){
mString = value;
}
}
Use this pattern for your MediaPlayer, and call getInstance() whenever you need your MediaPlayer instance.
Related
Hi guys I have a big problem. When I click a button fragment in MoviesFragment, a called start for getting movies information in MovieUtil.
The problem is that the array is load after that the view is already load.
What I can do? This is the code:
MoviesFragment.java
package com.example.msnma.movienotifier;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import com.example.msnma.movienotifier.adapter.MoviesAdapter;
import com.example.msnma.movienotifier.callback.MoviesCallback;
import com.example.msnma.movienotifier.model.Movie;
import com.example.msnma.movienotifier.util.MoviesUtil;
import com.rohit.recycleritemclicksupport.RecyclerItemClickSupport;
//import org.greenrobot.eventbus.EventBus;
//import org.greenrobot.eventbus.Subscribe;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import icepick.Icepick;
import icepick.State;
public class MoviesFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener,
RecyclerItemClickSupport.OnItemClickListener {
private static final String ARG_FRAG_TYPE = "fragType";
public enum Type {
NOTIFY,
SUGGESTED,
WATCHED
}
#State
ArrayList<Movie> movies;
#State
Type fragType;
TextView messageIfEmpty;
#BindView(R.id.refresh)
SwipeRefreshLayout refreshView;
#BindView(R.id.movies)
RecyclerView moviesView;
public static MoviesFragment newInstance(Type fragType) {
MoviesFragment fragment = new MoviesFragment();
Bundle args = new Bundle();
args.putSerializable(ARG_FRAG_TYPE, fragType);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Icepick.restoreInstanceState(this, savedInstanceState);
if (getArguments() != null) {
fragType = (Type) getArguments().getSerializable(ARG_FRAG_TYPE);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_movies_list, container, false);
ButterKnife.bind(this, rootView);
try {
init();
// updateView(rootView);
} catch (ParseException e) {
e.printStackTrace();
}
return rootView;
}
#Override
public void onStart() {
super.onStart();
// EventBus.getDefault().register(this);
}
#Override
public void onStop() {
// EventBus.getDefault().unregister(this);
super.onStop();
}
#Override
public void onRefresh() {
movies = null;
try {
updateMovies();
} catch (ParseException e) {
e.printStackTrace();
}
}
#Override
public void onItemClicked(RecyclerView recyclerView, int position, View v) {
showMovieAtPosition(position);
}
// #Subscribe(sticky = true)
// public void onEvent(UpdateFavoritesEvent event) {
// if (fragType == Type.FAVORITES) {
// EventBus.getDefault().removeStickyEvent(UpdateFavoritesEvent.class);
// onRefresh();
// }
// }
// #Subscribe(sticky = true)
// public void onEvent(TwoPaneEvent event) {
// twoPane = event.twoPane;
// }
#Override
protected void init() throws ParseException {
RecyclerItemClickSupport.addTo(moviesView)
.setOnItemClickListener(this);
moviesView.setLayoutManager(new GridLayoutManager(getContext(), 2));
moviesView.setHasFixedSize(true);
refreshView.setOnRefreshListener(this);
updateMovies();
}
private void updateMovies() throws ParseException {
if (movies == null) {
MoviesCallback callback = new MoviesCallback() {
#Override
public void success(List<Movie> result) {
movies = new ArrayList<>(result);
if (moviesView != null) {
moviesView.setAdapter(new MoviesAdapter(getContext(), movies));
}
refreshView.setRefreshing(false);
}
#Override
public void error(Exception error) {
Toast.makeText(getContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
error.printStackTrace();
refreshView.setRefreshing(false);
}
};
switch (fragType) {
case NOTIFY:
MoviesUtil.getNotifyMeMovies(getActivity(), callback, this);
break;
case SUGGESTED:
MoviesUtil.getSuggestedMovies(getActivity(), callback, this);
break;
case WATCHED:
MoviesUtil.getWatchedMovies(getActivity(), callback, this);
break;
}
} else if (moviesView != null) {
moviesView.setAdapter(new MoviesAdapter(getContext(), movies));
refreshView.setRefreshing(false);
}
}
private void showMovieAtPosition(int position) {
// if (movies != null && position <= movies.size() - 1) {
// Movie movie = movies.get(position);
// EventBus.getDefault().postSticky(new ShowMovieEvent(movie));
// if (twoPane) {
// getFragmentManager().beginTransaction()
// .replace(R.id.movie_detail, new MovieFragment())
// .commit();
// } else {
// startActivity(new Intent(getContext(), MovieActivity.class));
// }
// }
}
// public void updateView(View rootView){
// if(movies == null || movies.isEmpty()){
// switch (fragType) {
// case NOTIFY:
// moviesView = (RecyclerView)rootView.findViewById(R.id.movies);
// messageIfEmpty = (TextView)rootView.findViewById(R.id.empty);
// messageIfEmpty.setText("Search a movie or go on the suggested list for add a movie you want to be notify about!");
// moviesView.setVisibility(View.GONE);
// messageIfEmpty.setVisibility(View.VISIBLE);
// break;
// case SUGGESTED:
// moviesView = (RecyclerView)rootView.findViewById(R.id.movies);
// messageIfEmpty = (TextView)rootView.findViewById(R.id.empty);
// messageIfEmpty.setText("No connection, please try again");
// moviesView.setVisibility(View.GONE);
// messageIfEmpty.setVisibility(View.VISIBLE);
// break;
// case WATCHED:
// moviesView = (RecyclerView)rootView.findViewById(R.id.movies);
// messageIfEmpty = (TextView)rootView.findViewById(R.id.empty);
// messageIfEmpty.setText("Add here movies you have watched!");
// moviesView.setVisibility(View.GONE);
// messageIfEmpty.setVisibility(View.VISIBLE);
// break;
// }
// }
// }
}
MovieUtil.java
package com.example.msnma.movienotifier.util;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.os.AsyncTask;
import android.view.View;
import android.widget.TextView;
import com.example.msnma.movienotifier.MainActivity;
import com.example.msnma.movienotifier.MovieFragment;
import com.example.msnma.movienotifier.MoviesFragment;
import com.example.msnma.movienotifier.R;
import com.example.msnma.movienotifier.callback.MoviesCallback;
import com.example.msnma.movienotifier.database.MovieDatabase;
import com.example.msnma.movienotifier.mapper.MovieMapper;
import com.example.msnma.movienotifier.model.Movie;
import com.example.msnma.movienotifier.provider.MovieContract;
import com.goebl.david.Webb;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
public class MoviesUtil {
private static final Webb WEBB = Webb.create();
private static final String TMDB_API_MOVIES_URL = "http://api.themoviedb.org/3/movie/%s?api_key=%s&page=%s";
private static final String TMDB_API_VIDEOS_URL = "http://api.themoviedb.org/3/movie/%s/videos?api_key=%s";
private static final String TMDB_POSTER_URL = "https://image.tmdb.org/t/p/w185%s";
private static final String TMDB_BACKDROP_URL = "https://image.tmdb.org/t/p/w300%s";
private static final String TMDB_UPCOMING_MOVIES ="http://api.themoviedb.org/3/movie/upcoming?api_key=f329e1bdcc6da3f6ed39da7278144be6";
private static final String TMDB_IN_THEATRES = "http://api.themoviedb.org/3/movie/now_playing?api_key=f329e1bdcc6da3f6ed39da7278144be6";
private static final String TYPE_NOTIFY = "NOTIFY";
private static final String TYPE_WATCHED = "WATCHED";
private static final String TYPE_POPULAR = "popular";
private static final MovieMapper mapper = new MovieMapper();
// public static boolean isFavorite(Context context, Movie movie) {
// Cursor cursor = context.getContentResolver()
// .query(MovieContract.CONTENT_URI,
// null,
// String.format("%s = ? and %s = ?", MovieContract.MOVIE_ID, MovieContract.TYPE),
// new String[]{movie.getId() + "", TYPE_FAVORITES},
// null
// );
// boolean isFavorite = cursor.getCount() > 0;
// cursor.close();
// return isFavorite;
// }
// public static boolean toggleFavorite(Context context, Movie movie) {
// if (isFavorite(context, movie)) {
// deleteMovie(context, TYPE_FAVORITES, movie);
// return false;
// } else {
// saveMovie(context, TYPE_FAVORITES, movie);
// return true;
// }
// }
public static void getNotifyMeMovies(Activity activity, MoviesCallback callback, MoviesFragment view) throws ParseException {
getMovies(activity, TYPE_NOTIFY, callback, view);
// List<Movie> movies = mapper.toMovieList(MainActivity.getMovieDatabase().getAllMovieByType(TYPE_NOTIFY));
}
public static void getSuggestedMovies(Activity activity, MoviesCallback callback, MoviesFragment view) {
getMovies(activity, TYPE_POPULAR, callback, view);
// getMoviesFromApi(activity, TYPE_POPULAR);
}
public static void getWatchedMovies(Activity activity, MoviesCallback callback, MoviesFragment view) throws ParseException {
getMovies(activity, TYPE_WATCHED, callback, view);
// List<Movie> movies = mapper.toMovieList(MainActivity.getMovieDatabase().getAllMovieByType(TYPE_WATCHED));
}
private static void getMovies(final Activity activity, final String type, final MoviesCallback callback, final MoviesFragment view) {
AsyncTask.execute(new Runnable() {
#Override
public void run() {
if (type.equals(TYPE_NOTIFY)) {
try {
mapper.toMovieList(MainActivity.getMovieDatabase().getAllMovieByType(TYPE_NOTIFY));
} catch (ParseException e) {
e.printStackTrace();
}
} else if (type.equals(TYPE_WATCHED)) {
try {
mapper.toMovieList(MainActivity.getMovieDatabase().getAllMovieByType(TYPE_WATCHED));
} catch (ParseException e) {
e.printStackTrace();
}
} else {
if (Util.isConnected(activity, false)) {
getMoviesFromApi(activity, TYPE_POPULAR, view);
}
}
}
});
}
private static void getMoviesFromApi(Activity activity, String type, MoviesFragment view) {
String apiUrl = String.format(TMDB_API_MOVIES_URL, type, activity.getString(R.string.tmdb_api_key), 1);
try {
JSONArray moviesJson = WEBB.get(apiUrl)
.asJsonObject()
.getBody()
.getJSONArray("results");
List<Movie> movies = toMovies(activity, moviesJson);
//proviamo il codice if suggested
if(type.equals("suggested")){
MovieDatabase.saveMoviesOnDB(movies);
}
deleteMovies(activity, type);
saveMovies(activity, type, movies);
// view.updateView(view.getView());
} catch (JSONException e) {
e.printStackTrace();
}
}
// private static void getMoviesFromDb(Activity activity, String type, final MoviesCallback callback) {
// try {
// Cursor cursor = activity.getContentResolver()
// .query(MovieContract.CONTENT_URI,
// null,
// MovieContract.TYPE + " = ?",
// new String[]{type},
// null
// );
// final List<Movie> movies = toMovies(cursor);
// cursor.close();
// activity.runOnUiThread(new Runnable() {
// #Override
// public void run() {
// callback.success(movies);
// }
// });
// } catch (final Exception e) {
// activity.runOnUiThread(new Runnable() {
// #Override
// public void run() {
// callback.error(e);
// }
// });
// }
// }
//provo a sistemarlo
private static void saveMovie(final Context context, final String type, final Movie movie) {
AsyncTask.execute(new Runnable() {
#Override
public void run() {
List<Movie> movies = new ArrayList<>();
movies.add(movie);
saveMovies(context, type, movies);
}
});
}
private static void saveMovies(Context context, String type, List<Movie> movies) {
if (movies != null) {
ContentValues[] moviesValues = new ContentValues[movies.size()];
for (int i = 0; i < movies.size(); i++) {
try {
Movie movie = movies.get(i);
ContentValues movieValues = new ContentValues();
movieValues.put(MovieContract.MOVIE_ID, movie.getId());
movieValues.put(MovieContract.TYPE, type);
movieValues.put(MovieContract.TITLE, movie.getTitle());
movieValues.put(MovieContract.OVERVIEW, movie.getOverview());
movieValues.put(MovieContract.POSTER_URL, movie.getPosterUrl());
movieValues.put(MovieContract.BACKDROP_URL, movie.getBackdropUrl());
movieValues.put(MovieContract.TRAILER_URL, movie.getTrailerUrl());
movieValues.put(MovieContract.RELEASE_DATE, Util.toDbDate(movie.getReleaseDate()));
movieValues.put(MovieContract.RATING, movie.getRating());
movieValues.put(MovieContract.ADULT, movie.isAdult() ? 1 : 0);
moviesValues[i] = movieValues;
} catch (Exception ignore) {
}
}
context.getContentResolver()
.bulkInsert(MovieContract.CONTENT_URI, moviesValues);
}
}
// private static void deleteMovie(final Context context, final String type, final Movie movie) {
// AsyncTask.execute(new Runnable() {
// #Override
// public void run() {
// context.getContentResolver()
// .delete(MovieContract.CONTENT_URI,
// MovieContract.MOVIE_ID + " = ? and " + MovieContract.TYPE + " = ?",
// new String[]{movie.getId() + "", type});
// }
// });
// }
private static void deleteMovies(final Context context, final String type) {
AsyncTask.execute(new Runnable() {
#Override
public void run() {
context.getContentResolver()
.delete(MovieContract.CONTENT_URI,
MovieContract.TYPE + " = ?",
new String[]{type});
}
});
}
private static List<Movie> toMovies(Cursor cursor) throws ParseException {
List<Movie> movies = new ArrayList<>();
while (cursor.moveToNext()) {
Movie movie = new Movie();
movie.setId(cursor.getInt(
cursor.getColumnIndex(MovieContract.MOVIE_ID)));
movie.setTitle(cursor.getString(
cursor.getColumnIndex(MovieContract.TITLE)));
movie.setOverview(cursor.getString(
cursor.getColumnIndex(MovieContract.OVERVIEW)));
movie.setPosterUrl(cursor.getString(
cursor.getColumnIndex(MovieContract.POSTER_URL)));
movie.setBackdropUrl(cursor.getString(
cursor.getColumnIndex(MovieContract.BACKDROP_URL)));
movie.setTrailerUrl(cursor.getString(
cursor.getColumnIndex(MovieContract.TRAILER_URL)));
movie.setReleaseDate(Util.toDate(cursor.getString(
cursor.getColumnIndex(MovieContract.RELEASE_DATE))));
movie.setRating(cursor.getFloat(
cursor.getColumnIndex(MovieContract.RATING)));
movie.setAdult(cursor.getInt(
cursor.getColumnIndex(MovieContract.ADULT)) == 1);
movies.add(movie);
}
return movies;
}
private static List<Movie> toMovies(Context context, JSONArray jsonMovies) {
List<Movie> movies = new ArrayList<>();
if (jsonMovies != null) {
for (int i = 0; i < jsonMovies.length(); i++) {
try {
JSONObject jsonMovie = jsonMovies.getJSONObject(i);
int movieId = jsonMovie.getInt("id");
Movie movie = new Movie();
movie.setId(movieId);
movie.setTitle(jsonMovie.getString("title"));
movie.setOverview(jsonMovie.getString("overview"));
movie.setPosterUrl(String.format(TMDB_POSTER_URL, jsonMovie.getString("poster_path")));
movie.setBackdropUrl(String.format(TMDB_BACKDROP_URL, jsonMovie.getString("backdrop_path")));
movie.setTrailerUrl(getTrailerUrl(context, movieId));
movie.setReleaseDate(Util.toDate(jsonMovie.getString("release_date")));
movie.setRating((float) jsonMovie.getDouble("vote_average"));
movie.setAdult(jsonMovie.getBoolean("adult"));
movies.add(movie);
} catch (Exception e) {
e.printStackTrace();
}
}
}
return movies;
}
private static String getTrailerUrl(Context context, int movieId) {
String apiUrl = String.format(TMDB_API_VIDEOS_URL, movieId, context.getString(R.string.tmdb_api_key));
try {
JSONArray trailersJson = WEBB.get(apiUrl)
.asJsonObject()
.getBody()
.getJSONArray("results");
for (int i = 0; i < trailersJson.length(); i++) {
JSONObject trailerJson = trailersJson.getJSONObject(i);
if (trailerJson.getString("site").toLowerCase().equals("youtube")) {
return "https://youtube.com/watch?v=" + trailerJson.getString("key");
}
}
return "";
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
}
Now, I have put a AsyncTask but it doesn't work how I wish. Help me!
I think you have to send/Call from MainActivity.java.
For example you want to send data on the Fragment, you should create a method at Fragment that send to data MainActivity and than MainActivity call the method and return to the Fragment.
I have been trying to implement next song button in my audio player app. I copied some code from a tutorial but its not working.The button for next song is btnNext and the method is cde(), its the last method in the code. The button gets clicked but next song is not played, current song keeps playing.How do I fix this ?
package com.example.dell_1.myapp3;
import android.app.Activity;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.media.MediaMetadataRetriever;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import static android.R.attr.path;
public class PlayListActivity extends Activity {
private String[] mAudioPath;
private MediaPlayer mMediaPlayer;
private String[] mMusicList;
int currentPosition = 0;
private List<String> songs = new ArrayList<>();
MediaMetadataRetriever metaRetriver;
byte[] art;
ImageView album_art;
TextView album;
TextView artist;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play_list);
mMediaPlayer = new MediaPlayer();
ListView mListView = (ListView) findViewById(R.id.list);
mMusicList = getAudioList();
ArrayAdapter<String> mAdapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, mMusicList);
mListView.setAdapter(mAdapter);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View view, int arg2,
long arg3) {
try {
playSong(mAudioPath[arg2]);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
private String[] getAudioList() {
final Cursor mCursor = getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
new String[]{MediaStore.Audio.Media.DISPLAY_NAME, MediaStore.Audio.Media.DATA}, null, null,
"LOWER(" + MediaStore.Audio.Media.TITLE + ") ASC");
int count = mCursor.getCount();
String[] songs = new String[count];
mAudioPath = new String[count];
int i = 0;
if (mCursor.moveToFirst()) {
do {
songs[i] = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME));
mAudioPath[i] = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
i++;
} while (mCursor.moveToNext());
}
mCursor.close();
return songs;
}
private void playSong(String path) throws IllegalArgumentException,
IllegalStateException, IOException {
setContentView(R.layout.activity_android_building_music_player);
Log.d("ringtone", "playSong :: " + path);
mMediaPlayer.reset();
mMediaPlayer.setDataSource(path);
//mMediaPlayer.setLooping(true);
mMediaPlayer.prepare();
mMediaPlayer.start();
acv(path);
abc();
cde();
}
public void acv(String path) {
getInit();
metaRetriver = new MediaMetadataRetriever();
metaRetriver.setDataSource(path);
try {
art = metaRetriver.getEmbeddedPicture();
Bitmap songImage = BitmapFactory.decodeByteArray(art, 0, art.length);
album_art.setImageBitmap(songImage);
album.setText(metaRetriver
.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM));
artist.setText(metaRetriver
.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST));
} catch (Exception e) {
album_art.setBackgroundColor(Color.GRAY);
album.setText("Unknown Album");
artist.setText("Unknown Artist");
}
}
public void getInit() {
album_art = (ImageView) findViewById(R.id.coverart1);
album = (TextView) findViewById(R.id.Album);
artist = (TextView) findViewById(R.id.artist_name);
}
public void abc() {
ImageButton btnPlay1 = (ImageButton) findViewById(R.id.btnPlay1);
btnPlay1.setBackgroundColor(Color.TRANSPARENT);
btnPlay1.setOnClickListener(
new View.OnClickListener() {
public void onClick(View v) {
if (mMediaPlayer.isPlaying()) {
mMediaPlayer.pause();
} else {
mMediaPlayer.start();
}
}
});
}
public void cde() {
ImageButton btnNext = (ImageButton) findViewById(R.id.btnNext); //this is the button for playing next song.
btnNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
try {
currentPosition=currentPosition+1;
playSong(path + songs.get(currentPosition));
} catch (IOException ex) {
ex.printStackTrace();
}
}
});
}
}
Add this in onCreate method:
Bundle bundle = getIntent().getExtras();
position = bundle.getInt("position");
And change next button listener to
btnNext.setOnClickListener(new View.OnClickListener() //this is the button
#Override
public void onClick(View arg0) {
if (mMediaPlayer!= null && mMediaPlayer.isPlaying()) {
mMediaPlayer.stop();
}
uri = Uri.parse(mAudioPath[position + 1]);
mMediaPlayer.setDataSource(getApplicationContext(), uri);
mMediaPlayer.prepare();
mMediaPlayer.start();
}
});
int currentPosition = 0;
if (++currentPosition >= songs.size()) {
currentPosition = 0;
} else
try {
playSong(path + songs.get(currentPosition));
} catch (IOException ex) {
ex.printStackTrace();
}
}
The above code is your code from the onClick method.
As you can see, you are initializing the currentPosition inside onClick.
So to show you what this implies:
onClick -> position = 0 -> position++ (position = 1) -> playSong(songUri)
When you want:
onClick -> position++ -> playSong(songUri)
So, before setting the onCLickListener, you add:
currentPosition = 0;
currentPosition is declared in the class now, so make sure you add it. It should look like this:
int currentPosition;
..other code
public void cde(){
..code here
currentPosition = 0;
... set onClickListener
}
Remove int currentPosition = 0; from the onClick method.
I assume there is a position 0 as well. Here is the refactored code that would handle that:
try {
playSong(path + songs.get(currentPosition));
if (++currentPosition >= songs.size()) {
currentPosition = 0;
}
} catch (IOException ex) {
ex.printStackTrace();
}
The above code is addressing another issue you would be likely to meet. Song 0 would never play on the first round.
Another thing you want to check for (not giving you the code for it as it is easy) is to not play or allow next song if there are no songs. If songs.size == 0 it would never play but set the position to 0 over and over.
So I'm trying to make a simple application that makes stores group events in a MySQL database then retrieves them for people to join. In this fragment I list all the events by using a JSONParser class to query the database. I use an Async class to do the querying. The fragment will initially query the db on startup or whenever the user decides to limit the scope of the events by selecting something in a spinner or when the user pushes a refresh button. I have been getting messages like
Choreographer﹕ Skipped 95 frames! The application may be doing too much work on its main thread.
while running the program and I'm not sure why. I think it might be because I call the Async class too much, but I'm not sure.
public class mainActivityFragment extends Fragment {
final public String information = "information";
public Spinner specifySubject;
private ArrayList<String> list = new ArrayList<>();
private ArrayList<EventObject> eventList = new ArrayList<>();
JSONParser jsonParser = new JSONParser();
ListView test;
ArrayAdapter adapter;
// url to create new product
private static String url_get_event = "";
private ProgressDialog pDialog;
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_main, container, false);
adapter = new ArrayAdapter(getActivity(),android.R.layout.simple_list_item_1, list);
test = (ListView) v.findViewById(R.id.listView);
new CreateNewProduct().execute();
if(pDialog.isShowing()){
pDialog.dismiss();
}
test.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
Intent in = new Intent(getActivity(), AttendInformation.class);
EventObject clickedEvent = eventList.get(position);
String[] testInformation = {clickedEvent.getTo().toString(), clickedEvent.getLocation(), clickedEvent.getTitle(), clickedEvent.getDurationString(), clickedEvent.getDescription(), clickedEvent.getSubject()};
in.putExtra(information, testInformation);
startActivity(in);
}
});
Button createEventButton = (Button) v.findViewById(R.id.Button2);
createEventButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent in = new Intent(getActivity(), createEvent.class);
startActivity(in);
}
});
specifySubject = (Spinner) v.findViewById(R.id.spinner);
specifySubject.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
AsyncTask task;
task = new CreateNewProduct().execute();
try {
task.get(3000, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
if (position == 0) {
} else {
String selectedSubj = getResources().getStringArray(R.array.class_array)[position];
for (int i = 0; i < eventList.size(); i++) {
if (!eventList.get(i).getSubject().equals(selectedSubj)) {
list.remove(list.indexOf(eventList.get(i).getTitle()));
eventList.remove(i);
i--;
}
}
adapter.notifyDataSetChanged();
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Button refresh = (Button) v.findViewById(R.id.leftButton);
refresh.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
AsyncTask task;
task = new CreateNewProduct().execute();
try {
task.get(3000, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
if (specifySubject.getSelectedItemPosition() == 0) {
} else {
String selectedSubj = getResources().getStringArray(R.array.class_array)[specifySubject.getSelectedItemPosition()];
for (int i = 0; i < eventList.size(); i++) {
if (!eventList.get(i).getSubject().equals(selectedSubj)) {
list.remove(list.indexOf(eventList.get(i).getTitle()));
eventList.remove(i);
i--;
}
}
adapter.notifyDataSetChanged();
}
}
});
return v;
}
class CreateNewProduct extends AsyncTask<String, String, String> {
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Getting Events...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
protected String doInBackground(String... args) {
JSONArray jsonArr = jsonParser.getJSONFromUrl(url_get_event);
for(int n = 0; n < jsonArr.length(); n++)
{
try {
JSONObject object = jsonArr.getJSONObject(n);
if(!list.contains(object.getString("title"))){
String[] time = object.getString("time").split(":");
time[1] = time[1].substring(0, 2);
EventObject tempEven = new EventObject(object.getString("title"), object.getString("location"), object.getString("description"), object.getString("subject"), 0, new TimeObject(Integer.parseInt(time[0]), Integer.parseInt(time[1])));
eventList.add(tempEven);
list.add(object.getString("title"));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
return null;
}
protected void onPostExecute(String file_url) {
// dismiss the dialog once done
test.setAdapter(adapter);
pDialog.dismiss();
}
}
}
I am creating a soundboard app. The app will feature different "pages" (fragments) that the user can switch between. Each fragment has a number of ImageButtons that, when clicked on, will play a sound.
I put the following code first inside of the MainActivity inside the OnCreate method. After that failed I realized that I'd probably have to put that code inside of each fragment inside the fragment's OnCreateView method.
After doing so I come up for an error for the "findViewById" method. I corrected this by using the "getView()" method directly before the "findViewById" method.
Now I have an error on the "final MediaPlayer player = new MediaPlayer();". The error says that this is an unreachable statement.
I have no idea how to fix this... what can I do to correct this error?
FragmentPage1.java
public static class FragmentPage1 extends Fragment {
int selectedSoundId;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_page1, container, false);
return rootView;
final MediaPlayer player = new MediaPlayer();
final Resources res = getResources();
final int[] buttonIds = { R.id.btn1, R.id.btn2, R.id.btn3, R.id.btn4, R.id.btn5, R.id.btn6, R.id.btn7, R.id.btn8, R.id.btn9 };
final int[] soundIds = { R.raw.num_21, R.raw.whats_9_plus_10, R.raw.a_potato_flew_around_my_room, R.raw.a_week_ago, R.raw.aint_nobody_got_time_for_dat, R.raw.awww, R.raw.baby_lip, R.raw.backflip, R.raw.baliegdeh };
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
for (int i = 0; i < buttonIds.length; i++) {
if (v.getId() == buttonIds[i]) {
selectedSoundId = soundIds[i];
AssetFileDescriptor afd = res.openRawResourceFd(soundIds[i]);
player.reset();
try {
player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
player.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
player.start();
break;
}
}
}
};
for (int i = 0; i < buttonIds.length; i++) {
ImageButton soundButton = (ImageButton) getView().findViewById(buttonIds[i]);
registerForContextMenu(soundButton);
soundButton.setOnClickListener(listener);
}
}
}
The statement is unreachable because you unconditionally return before it can be reached. The fix is simple: move return rootView to the end of the method and instead of getView() use rootView.
I am having problem in my code.I am trying to change the layout background of my app every second.I used Thread in this code.I've searched the site but I couldn't find anything useful.Here is the code.
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.LinearLayout;
public class MainActivity extends Activity {
//private Bitmap open, close;
private LinearLayout myL;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myL = (LinearLayout) findViewById(R.id.LinearLayout2);
// myL=(LinearLayout) findViewById(R.id.LinearLayout2);
//close = BitmapFactory.decodeResource(getResources(), R.drawable.kapa);
//open = BitmapFactory.decodeResource(getResources(), R.drawable.ac);
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Thread th = new Thread() {
public void run() {
while (true) {
myL.setBackgroundResource(R.drawable.kapa);
try {
sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
myL.setBackgroundResource(R.drawable.ac);
try {
sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
th.start();
}
}
From the answer to this question Change Layout background continuously...
Try using a Handler. Example...
public class MainActivity extends Activity {
final int CHANGE_BG_RES = 1;
final int RESOURCE_1 = R.drawable.kapa;
final int RESOURCE_2 = R.drawable.ac;
private LinearLayout myL;
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
if (CHANGE_BG_RES == msg.what) {
int res = msg.arg1;
myL.setBackgroundResource(res);
int nextRes;
if (RESOURCE_1 == res)
nextRes = RESOURCE_2;
else
nextRes = RESOURCE_1;
Message m = obtainMessage (CHANGE_BG_RES, nextRes, 0, null);
sendMessageDelayed(m, 1000);
}
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myL = (LinearLayout) findViewById(R.id.LinearLayout2);
}
#Override
protected void onResume() {
super.onResume();
Message m = handler.obtainMessage(CHANGE_BG_RES, RESOURCE_1, 0, null);
handler.sendMessageDelayed(m, 1000);
}
}
Try this:
public class MainActivity extends Activity {
//private Bitmap open, close;
private LinearLayout myL;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myL = (LinearLayout) findViewById(R.id.LinearLayout2);
// myL=(LinearLayout) findViewById(R.id.LinearLayout2);
//close = BitmapFactory.decodeResource(getResources(), R.drawable.kapa);
//open = BitmapFactory.decodeResource(getResources(), R.drawable.ac);
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Runnable runnable = new Runnable() {
#Override
public void run() {
while(true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
MainActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
myL.setBackgroundResource(R.drawable.kapa);
}
});
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
MainActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
myL.setBackgroundResource(R.drawable.ac);
}
});
}
}
};
new Thread(runnable).start();
}
}