I want to play an audio file when a Button clicked. But my code generate error code that says:
Cannot resolve method'create(context, int)'
So, my audio file is detected as an integer.
this is my fragment code:
package com.example.suha.belajarhurufhijaiyah;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
/**
* A simple {#link Fragment} subclass.
*/
public class Hijaiyah extends Fragment implements View.OnClickListener {
ImageButton alif;
View view;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_hijaiyah, container, false);
alif = view.findViewById(R.id.alif_sound);
alif.setOnClickListener(this);
return view;
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.alif_sound:
MediaPlayer mp = MediaPlayer.create(this, R.raw.sound_alif);
mp.start();
}
}
}
That R.raw.sound_alif generate the error..
And it's my resource hier
you are trying to create MediaPlayer in Fragment use getActivity() or getContext() instead of this so use below code
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.alif_sound:
MediaPlayer mp = MediaPlayer.create(getActivity(), R.raw.sound_alif);
mp.start();
}
All resources are represented as Integers in the R class that is not your problem the problem is the Context your passing in a Fragment change this to getActivity So change your line to:
MediaPlayer mp = MediaPlayer.create(getActivity, R.raw.sound_alif);
Related
I adding VideoView in fragment but only I get black background nothing more I tried with two codes but both doesn't work can you help me?
PS I don't need play, stop button and anything other just to show the video Here is the code that I add will be good if I can mute the audio of the video and replay
Code 1
package com.Hristijan.Aleksandar.GymAssistant.Exercises;
import android.media.session.MediaController;
import android.net.Uri;
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.VideoView;
import java.net.URL;
/**
* A simple {#link Fragment} subclass.
*/
public class BenchFragment extends Fragment {
private VideoView MyVideoView;
public BenchFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_bench, container, false);
MyVideoView = (VideoView)rootView.findViewById(R.id.video_view);
Uri uri= Uri.parse("android.resource://"+getActivity().getPackageName()+"/"+R.raw.bench);
MyVideoView.setVideoURI(uri);
MyVideoView.start();
return inflater.inflate(R.layout.fragment_bench, container, false);
}
}
Code2
package com.hristijan.aleksandar.gymworkout.myapplication;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.VideoView;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
private VideoView MyVideoView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyVideoView = findViewById(R.id.videoViewId);
Uri uri = Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.bench);
MyVideoView.setVideoURI(uri);
MyVideoView.start();
}
}
Layout.xml
<LinearLayout 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:orientation="vertical"
android:background="#000000"
tools:context="com.Hristijan.Aleksandar.GymAssistant.Exercises.BenchFragment">
<VideoView
android:id="#+id/video_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
Try this to play,replay and identify the error
private void startVideo() {
String path = "android.resource://" + getPackageName() + "/" + R.raw.crop;
// Log.e(TAG,Uri.parse(path) + " ");
video_view.setVideoURI(Uri.parse(path));
video_view.start();
video_view.setOnErrorListener(new MediaPlayer.OnErrorListener() {
#Override
public boolean onError(MediaPlayer mp, int i, int i1) {
Log.e(TAG,String.format("Error: What: %d, Extra: %d",i,i1));
return false;
}
});
video_view.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.start();
}
});
}
If the video doesn't play it should log error in LogCat. You can identify what kind of error using this doc
About Muting the Audio
You can refer to this Question basically you just need to get the media player object when the content is ready then you can mute the audio by setting m.setVolume(0f, 0f);
I am sure that Code 1 and Code 2 can work
First, make sure your video format is Supported Media Formats
Then, make sure your video file bench stored in ...app\src\main\res\raw (And your file name should be bench not bench.xxx)
On the other hand, in Code 2 something is wrong:
MyVideoView = findViewById(R.id.videoViewId);
should be
MyVideoView = findViewById(R.id.video_view);
in order to find a VideoView by correct Id
Try putting videoView.start() inside videoView.setOnPreparedListener() as:
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener(){
#Override
public void onPrepared(MediaPlayer mediaPlayer){
videoView.start();
}
});
There is an error in this line:
addSlide(AppIntroSampleSlider.newInstance(R.layout.app_intro1));
addSlide (android.support.v4.app.Fragment)
In AppIntroBase, it cannot be applied
My code is here:
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
import com.github.paolorotolo.appintro.AppIntro;
/**
* Created by Arvind on 2/6/2017.
*/
public class MyIntro extends AppIntro {
#Override
public void init(Bundle savedInstanceState) {
//adding the three slides for introduction app you can ad as many you needed
addSlide(AppIntroSampleSlider.newInstance(R.layout.app_intro1));
addSlide(AppIntroSampleSlider.newInstance(R.layout.app_intro2));
addSlide(AppIntroSampleSlider.newInstance(R.layout.app_intro3));
// Show and Hide Skip and Done buttons
showStatusBar(false);
showSkipButton(false);
// Turn vibration on and set intensity
// You will need to add VIBRATE permission in Manifest file
setVibrate(true);
setVibrateIntensity(30);
//Add animation to the intro slider
setDepthAnimation();
}
#Override
public void onSkipPressed() {
// Do something here when users click or tap on Skip button.
Toast.makeText(getApplicationContext(),
getString(R.string.app_intro_skip), Toast.LENGTH_SHORT).show();
Intent i = new Intent(getApplicationContext(), MainActivity.class);
startActivity(i);
}
#Override
public void onNextPressed() {
// Do something here when users click or tap on Next button.
}
#Override
public void onDonePressed() {
// Do something here when users click or tap tap on Done button.
finish();
}
#Override
public void onSlideChanged() {
// Do something here when slide is changed
}
}
I created a class i.e. AppIntroSampleSlider.
My AppIntroSampleSlider class is:
package com.example.arvind.appintro1;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by Arvind on 13-Feb-17.
*/
public class AppIntroSampleSlider extends Fragment {
private static final String ARG_LAYOUT_RES_ID = "layoutResId";
public static AppIntroSampleSlider newInstance(int layoutResId) {
AppIntroSampleSlider sampleSlide = new AppIntroSampleSlider();
Bundle bundleArgs = new Bundle();
bundleArgs.putInt(ARG_LAYOUT_RES_ID, layoutResId);
sampleSlide.setArguments(bundleArgs);
return sampleSlide;
}
private int layoutResId;
public AppIntroSampleSlider() {}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(getArguments() != null && getArguments().containsKey(ARG_LAYOUT_RES_ID))
layoutResId = getArguments().getInt(ARG_LAYOUT_RES_ID);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(layoutResId, container, false);
}
}
I want to why it is showing error in the code.So Please help me to solve this error.
I found a better example without error
http://www.androidhive.info/2016/05/android-build-intro-slider-app/
AppIntro library works best with the following import in your AppIntroSampleSlider.java file:
import android.support.v4.app.Fragment;
Instead of:
import android.app.Fragment;
Im trying to use viewPager so I want to change my class from an activity to fragment, but Im getting alot of errors, so can you tell me whats wrong and what I need to do?
Here is my original activity :
package com.pickapp.pachu.pickapp;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.content.Intent;
import java.util.Random;
public class MainScreen extends Activity implements OnClickListener {
private TitlesDB titles;
private Button getPickUpLine;
private TextView pickUpLine;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_screen);
titles = new TitlesDB(this);
initDB();
initialize();
}
public void initialize() {
this.getPickUpLine = (Button) findViewById(R.id.bGetLine);
this.getPickUpLine.setOnClickListener(this);
this.pickUpLine = (TextView) findViewById(R.id.tvLine);
this.pickUpLine.setOnClickListener(this);
}
public void initDB() {
titles.open();
if (!this.titles.isExist()) {
titles.createEntry("The I \n Have Cancer");
titles.createEntry("The Ocean");
}
titles.close();
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bGetLine:
titles.open();
Random rnd = new Random();
int index = rnd.nextInt(titles.getLength()) + 1;
pickUpLine.setText(titles.getTitleById(index));
titles.close();
break;
case R.id.tvLine:
if(!pickUpLine.getText().toString().equals(""))
{
Intent intent = new Intent(this, PickAppLine.class);
intent.putExtra("key", pickUpLine.getText().toString());
startActivity(intent);
}
break;
}
}
}
Here is what I tried :
package com.pickapp.pachu.pickapp;
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 android.widget.TextView;
/**
* Created by Golan on 19/08/2014.
*/
public class MainScreenFragment extends Fragment implements OnClickListener{
private TitlesDB titles;
private Button getPickUpLine;
private TextView pickUpLine;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
titles = new TitlesDB(getActivity());
initDB();
View v = getView();
initialize(v);
return inflater.inflate(R.layout.activity_main_screen, container, false);
}
public void initialize(View v) {
this.getPickUpLine = (Button) v.findViewById(R.id.bGetLine);
this.getPickUpLine.setOnClickListener(this);
this.pickUpLine = (TextView) v.findViewById(R.id.tvLine);
this.pickUpLine.setOnClickListener(this);
}
public void initDB() {
titles.open();
if (!this.titles.isExist()) {
titles.createEntry("The I \n Have Cancer");
titles.createEntry("The Ocean");
}
titles.close();
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bGetLine:
titles.open();
Random rnd = new Random();
int index = rnd.nextInt(titles.getLength()) + 1;
pickUpLine.setText(titles.getTitleById(index));
titles.close();
break;
case R.id.tvLine:
if(!pickUpLine.getText().toString().equals(""))
{
Intent intent = new Intent(this, PickAppLine.class);
intent.putExtra("key", pickUpLine.getText().toString());
startActivity(intent);
}
break;
}
}
}
We won't just fix all your code. You have to do that yourself! Remove everything that you don't need at the beginning and start adding one after the other again. Fix all the errors on the way. It is hard to know what the actual problem is when everything is wrong.
One thing that I see right away that definitely does not work is this:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.activity_main_screen, container, false); // NO, NO, NO!!
titles = new TitlesDB(getActivity());
initDB();
initialize();
}
You have a return statement in the method BEFORE you have more code. That can not work. The return statement has to be at the end!
I want to add a play button in my app (I'm using MediaPlayer) but maybe I'm doing something wrong when I declare my MediaPlayer.
package com.example.tones;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
public class TonosSet extends Activity
{
/**
* #see android.app.Activity#onCreate(Bundle)
*/
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.tonos_set);
Intent i = getIntent();
int position = i.getExtras().getInt("id");
MediaPlayer mp;
ImageView imageView = (ImageView) findViewById(R.id.full_image_view2);
TextView textView1 = (TextView)findViewById(R.id.namesong);
TextView textView2 = (TextView)findViewById(R.id.artist);
ImageButton play = (ImageButton) findViewById(R.id.ButtonPlay);
ImageButton stop = (ImageButton) findViewById(R.id.ButtonStop);
ImageButton setringtone = (ImageButton) findViewById(R.id.ButtonRingtone);
switch (position)
{
case 0:
imageView.setImageResource(R.drawable.picture);
textView1.setText("soundd");
textView2.setText("aut");
break;
case 1:
imageView.setImageResource(R.drawable.picture);
textView1.setText("sound");
textView2.setText("aut");
break;
}
play.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
mp = MediaPlayer.create(TonosSet.this, R.raw.sound1); //Error
mp.start(); //Error again
}
});
}
}
My error is:
Cannot refer to a non-final variable mp inside an inner class defined in a different method
If I change MediaPlayer mp; to final MediaPlayer mp;, eclipse shows this error:
The final local variable cannot be assigned, since it is defined in an enclosing type.
I don't know what's wrong with my app. I've followed a lot of tutorials but no one has the same error as me, even though I think I’m doing the same thing.
I've fixed my problem:
public class TonosSet extends Activity{
private MediaPlayer mp;
=D Thanks and sorry for this noob question.
how do i embed a tab layout within a button. My main layout is a linear layout, but i don't know how to program the main activity.java class. Could anybody help me get started this is what my main.java code looks like right now
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.content.Intent;
public class Remote_DocActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
//private static final String TAG = "Remote_Doc";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View Patient_Button = findViewById(R.id.patientButton);
Patient_Button.setOnClickListener(this);
View Doctor_Button = findViewById(R.id.doctorButton);
Doctor_Button.setOnClickListener(this);
View About_Option = findViewById(R.id.aboutButton);
About_Option.setOnClickListener(this);
View Exit_Option = findViewById(R.id.exit);
Exit_Option.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.aboutButton:
Intent i = new Intent(this, About.class);
startActivity(i);
break;
case R.id.exit:
finish();
break;
}
}
}
you can create tabs with the help of TabActivity class. You can take help from android-wireless-application-development book's unit 3 chapter 8 example.