Android: Open a PCM file in a new activity - android

I am currently developing an app for playing PCM files and displaying their resulting line graph (thanks to jjoe64 for the GraphView).
My problem now is how to open a PCM file in a new activity. Basically, the app must display the line graph for the selected file. This certain file is a list entry, so, when the user clicks the filename from the list, it will be directed to a new activity within the app for graph display.
Here is the code for the opening of a file in a list, a.k.a. the first part of my problem (from DBActivity.java):
public final static String EXTRA_FILE = "com.example.ecg_to_go.FILENAME";
getListView().setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
final int sFilePosition = position;
sFile = files[sFilePosition];
Intent openFile = new Intent(DBActivity.this, ContentActivity.class);
Uri uri = Uri.fromFile(sFile);
openFile.putExtra(EXTRA_FILE, uri);
startActivity(openFile);
}
});
And here is the second part of my problem (from ContentActivity.java):
private File ecgFile;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_content);
ecgFile = new File(DBActivity.EXTRA_FILE);
playEcg(ecgFile); // display the PCM file
}

Look here for how to get the extras you gave to the intent, in the started activity. You just have to call getIntent().getExtras() which returns you a bundle and the documentation is here :http://developer.android.com/reference/android/os/Bundle.html
Good luck :)

Related

Get item playing from MediaBrowserService

I have an audio app with the home Activity containing a list of items. The user selects an item and I pass an ID to another Activity which has the controls (play/pause/volume, etc). The audio playback is handed in a MediaBrowserService. I need to detect if the item the user selects is currently playing but I can't figure out how outside of saving the ID in local storage (SharedPrefs or SQlite).
I pass the ID of the item from the second Activity to the MediaBrowserService though a Bundle. I thought I could then retrieve the ID in the second Activity using getExtras() but it always returns 0 or null, depending on which code I use (see below).
I'm not opposed to using local storage but seem like there should be a better way. This is what I have so far:
public class EpisodeActivity extends Activity {
private MediaBrowserCompat mMediaBrowserCompat;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Bundle extras = new Bundle();
extras.putInt("episodeid", getIntent().getExtras().getInt("episodeid")); //passed in from Home Activity
mMediaBrowserCompat = new MediaBrowserCompat(
this,
new ComponentName(this, MediaPlayerService.class),
mMediaBrowserCompatConnectionCallback,
extras
);
mPlayButton.setOnClickListener(view -> {
final Bundle extras = new Bundle();
extras.putInt("episodeid", getIntent().getExtras().getInt("episodeid")); //passed in from Home Activity
String url = "http://www.example.com/media.mp3"
MediaControllerCompat.getMediaController(mActivity).getTransportControls().playFromUri(Uri.parse(uri), extras);
});
if (MediaControllerCompat.getMediaController(mActivity).getPlaybackState() != null &&
MediaControllerCompat.getMediaController(mActivity).getPlaybackState().getState() == PlaybackStateCompat.STATE_PLAYING) {
int episodeID = mMediaBrowserCompat.getExtras().getInt("episodeid"); //always returns 0
//also tried this but getExtras is null
int episodeID = MediaControllerCompat.getMediaController(mActivity).getExtras().getInt("episodeid");
}
}
}
public class MediaPlayerService extends MediaBrowserServiceCompat {
private MediaSessionCompat mMediaSessionCompat;
#Override
public void onCreate() {
super.onCreate();
final ComponentName mediaButtonReceiver = new ComponentName(getApplicationContext(), MediaButtonReceiver.class);
mMediaSessionCompat = new MediaSessionCompat(getApplicationContext(), getString(R.string.app_name), mediaButtonReceiver, null);
mMediaSessionCompat.setCallback(mMediaSessionCallback);
...
}
private MediaSessionCompat.Callback mMediaSessionCallback = new MediaSessionCompat.Callback() {
#Override
public void onPlayFromUri(final Uri uri, final Bundle extras) {
super.onPlayFromUri(uri, extras);
int episodeId = extras.getInt("episodeid");
String url = GetUrl(episodeId);
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setDataSource(uri);
mMediaPlayer.prepareAsync();
...
}
}
}
media_controller.getMetadata().getDescription().getMediaId() may be what you're looking for. If not, maybe try using MediaMetadataCompat.Builder() to set some metadata to each of your episodes
MediaMetadataCompat.Builder().putString(MediaMetadataCompat.METADATA_KEY_MEDIA_ID, episodeID).build()
something like that, I don't think you need to use the METADATA_KEYs either, you can probably replace that with "episode id" or whatever string you want to use as a key.
edit: if you go the metadata route, you may want to use that to build mediaitems.
MediaMetadataCompat episode_meta = new MediaMetadataCompat.Builder()
.putString(MediaMetadataCompat.METADATA_KEY_MEDIA_ID, episodeID)
.build();
episode_mediaitem = new MediaBrowserCompat.MediaItem(episode_meta.getDescription(), MediaBrowserCompat.MediaItem.FLAG_PLAYABLE));
Hope i helped
edit: using the media items enables you to use the media_controller.getMetadata().getDescription().getMediaId() from above, I think
mPlayButton.setOnClickListener(view -> {
final Bundle extras = new Bundle();
extras.putInt("episodeid", getIntent().getExtras().getInt("episodeid")); //passed in from Home Activity
String url = "http://www.example.com/media.mp3"
MediaControllerCompat.getMediaController(mActivity).getTransportControls().playFromUri(Uri.parse(uri), extras);
});
here you should put url instead uri when you parse Uri.

Assign image resource in an image view with string

(1) I am using recyleview to fetch image, text & audio file from API. For text I used volley & for image use picasso, what I will use for audio file.
(2) after fetch the image & text when apply onclicklistener pass the info to another activity. Can pass only text not images.
What I need to do to solve this problem?
this is adapter part
viewHolder.textViewStory.setText(banglaItem.getStoryName());
viewHolder.textViewWritter.setText(banglaItem.getWritterName());
Picasso.get().load(banglaItem.getStoryImage()).fit().into(viewHolder.imageView);
viewHolder.linear.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context,AudioActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("storyName",banglaItem.getStoryName());
intent.putExtra("storyImage",banglaItem.getStoryImage());
context.startActivity(intent);
}
});
this is the new activity part
public class AudioActivity extends AppCompatActivity {
TextView name;
ImageView image;
String nameStory;
String imageStory;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_audio);
name = findViewById(R.id.audioName);
image = findViewById(R.id.audioImage);
nameStory = getIntent().getStringExtra("storyName");
imageStory = getIntent().getStringExtra("storyImage");
name.setText(nameStory);
int resourceId = getResources().getIdentifier(imageStory,"drawable", getPackageName());
image.setImageResource(resourceId);
}
}
(1.) if you have complete url of your audio file then you can directly stream audio file using media player classes provided by android.
(2.) you are passing storyImage in next activity ,which is a url that you get from api. so you have to find same on next activity i.e
TextView name;
ImageView image;
String nameStory;
String imageStory;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_audio);
name = findViewById(R.id.audioName);
image = findViewById(R.id.audioImage);
nameStory = getIntent().getStringExtra("storyName");
imageStory = getIntent().getStringExtra("storyImage");
name.setText(nameStory);
Picasso.get().load(imageStory).fit().into(image);
//here image story contain url that you sent through first activity. As picasso uses cache, it will load (already loaded image in first activity) very smoothly from it cache.
}

Android app crashes when retrieving int from an intent in another activity

I am trying to make a button in one activity (SetupMenu) that, when pressed, puts an int into the intent and carries that over to the next activity (IntroActivity) where a textView will retrieve the int and display it.
Problem is, when the app runs and I get to the activity and press the button, the app crashes and my emulator tells me that "Unfortunately [my app] has stopped working."
I feel like I've tested every possible angle to get this to work. I should note that the button has worked fine, the textview has worked fine, everything else is working smoothly - I only run into issues when I try retrieving the intent and displaying it in textView. I tried passing through a String instead of an Int and also had issues (my string would not appear). Any pointers?
SetupMenu activity (here I put an int into my intent):
public class SetupMenu extends Activity {
public final static String extra_progress_key = "com.example.angelsanddemons.track_players";
public int track_players = 0;
public void to_intro(View view) {
Intent intent = new Intent(this, IntroActivity.class);
intent.putExtra(extra_progress_key, track_players);
startActivity(intent);
}
IntroActivity activity (here I try to retrieve the int from the intent):
public class IntroActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
int temp = intent.getIntExtra(SetupMenu.extra_progress_key, 0 );
TextView textView = new TextView(this);
textView.setText(temp);
setContentView(textView);
}
}
One problem is that you can't set a TextView's text to an int; you'll need to first convert it to an string. It's also not a good idea to be manipulating views before you've inflated them, so perhaps your onCreate() should be:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
int temp = intent.getIntExtra(SetupMenu.extra_progress_key, 0 );
TextView textView = new TextView(this);
setContentView(textView);
textView.setText(String.valueof(temp));
}
I see nothing that ensure that SetupMenu activity is created and in memory when IntroActivity is launched. To make sure, don't pass the variable, but the string itself and check if it work:
int temp = intent.getIntExtra("com.example.angelsanddemons.track_players", 0 );

Received Intent doesn't contain Extras

When I'm trying to pass data to another activity it for some reason gets lost. My sending activity looke like:
protected OnItemClickListener onArtistItemClick = new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter, View arg1, int cursor, long arg3) {
Intent artistCardIntent = new Intent(getBaseContext(), ArtistCardActivity.class);
Artist artist = (Artist) adapter.getItemAtPosition(cursor);
artistCardIntent.putExtra("artist_id", artist.getId());
artistCardIntent.putExtra("tt", "tt");
startActivity(artistCardIntent);
};
};
And while debugging I can see that artistCardIntent gets populated, however in the receiving activity Intent i doesn't contain any extra inforamation:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.artist_card);
Intent i = getIntent();
What am I doing wrong?
PS both activities extends FragmentActivity.
Thanks.
To receive the data use getextras() method. Find the following code. i think it helps you
Intent i = getIntent();
Bundle b=i.getExtras();
if(b!=null)
{
String artist= ("artist_id");
}
I don't see that you are getting the extra information.
Check out this answer How do I get extra data from intent on Android?
convert those values to string and try it , It will work
startActivity(new Intent(Home.this,galmenu.class).putExtra("page","home"));
Intent myIntent = getIntent();
test=myIntent.getExtras().getString("page");
it is working perfectly

Android Development: pass information and fail

I got 2 classes in my project. The first class(main) have a listview and this is the onclick():
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
// Get the item that was clicked
Object o = this.getListAdapter().getItem(position);
String keyword = o.toString();
class2 sec = new Class2();
Intent intent = new Intent(this, Class2.class);
startActivity(intent) ;
if (keyword == "hello"){
sec.setInfo(keyword);
}
}
so and then in my other class which have a defferent layout.xml. The code is:
public class det extends Activity {
static WebView map;
public TextView header;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.details);
}
public void setInfo(String mystring){
header = (TextView) findViewById(R.id.text01);
header.setText(mystring);
//Toast.makeText(this, map, Toast.LENGTH_LONG).show();
//return;
}
Ye, i keep getting force close on my android phone. The App i meant to change the header text to text that ive tapped on the listview. But when i click a item it pop up a window with FC.
ive try to comment away the:
header = (TextView) findViewById(R.id.text01);
header.setText(mystring);
and it worked without a FC however the headertext is stil null.
Thank you!
Your Friend!
First of all, you need to post the stack trace for us to have any idea how to help.
Second, I'm assuming Class2 extends Activity or else Intent intent = new Intent(this, Class2.class); doesn't make any sense. That being the case, class2 sec = new Class2(); is ALWAYS wrong. You never ever call new on a class that extends Activity.
You can't call methods on another activity like that. Your only real option is to send the keyword in the intent by using putExtra, and then getting retrieving it in Class2

Categories

Resources